2
votes

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

2
did you mean CCSprite? - LearnCocos2D

2 Answers

2
votes

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

1
votes

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];        
}