Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap the sprite in the CCSprite object in Cocos2d-X

I have an object that inherited from CCSprite. I want from inside this object to change the image.

How do I change the image (sprite) without creating a new CCSprite object in Cocos2d-X?

Thanks, Adrian.

like image 742
azelez Avatar asked May 04 '12 20:05

azelez


People also ask

How to use a Texture2D object for a sprite?

You can use a Texture2D object for many sprites. Only the contents inside rect of this texture will be applied for this sprite. True if the sprite is initialized properly, false otherwise. Initializes a sprite with a texture and a rect in points, optionally rotated. After initialization, the offset will be (0,0). This is the designated initializer.

What is a spriteframe object?

A SpriteFrame object. It should includes a valid texture and a rect. True if the sprite is initialized properly, false otherwise. Initializes a sprite with an sprite frame name. A SpriteFrame will be fetched from the SpriteFrameCache by name.

Is it possible to set the blending function of a sprite?

The Blending function property belongs to SpriteBatchNode, so you can't individually set the blending function property. ParallaxNode is not supported, but can be simulated with a "proxy" sprite. Sprites can only have other Sprites (or subclasses of Sprite) as children.

What are the basic components of Cocos2d-x?

Basic Cocos2d-x Concepts Main Components Director Scenes and the Scene graph Sprites Actions Sequences and Spawns


2 Answers

mySprite->setTexture(CCTextureCache::sharedTextureCache()->addImage("newImage.png"));

No need to alter your custom class.. Hope this helps.. :)

like image 121
Nikhil Aneja Avatar answered Sep 30 '22 07:09

Nikhil Aneja


Works for me:

mySprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("frame_name"));

Before you need to load you sprites in cache:

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite_atlas.plist");
like image 21
Vladimir Vovk Avatar answered Sep 30 '22 08:09

Vladimir Vovk