Jude Michael Murphy
Jude Michael Murphy

Reputation: 1198

Cocos2d - Change CCNode Image Programmatically

Does anyone know how to change a CCNode's image programmatically? I'm using SpriteBuilder to make a simple game.

Upvotes: 2

Views: 844

Answers (2)

PWiggin
PWiggin

Reputation: 966

Assuming you are using a CCNode object in your scene, you'll need to create a method in the object's implementation file an call it out when you want to change the image.

In the Scene code:

CustomObject *blahblah;

[blahblah ChangeNodeImage:"FrameName.png"];

In the CCNode implementation file:

-(void) ChangeNodeImage: (NSString *) theImageFrameName;
{
    CCSpriteFrame* imageframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:theImageFrameName];
    [CustomObject setDisplayFrame:imageframe];        
}

Upvotes: 1

Ben-G
Ben-G

Reputation: 5026

A CCNode does not have an image. Only CCSprites have images. You can change the image of a CCSprite using the spriteFrame property.

Upvotes: 2

Related Questions