2

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

James Webster
  • 30,976
  • 11
  • 64
  • 113
Jude Michael Murphy
  • 1,184
  • 2
  • 11
  • 22

2 Answers2

2

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

Ben-G
  • 4,896
  • 25
  • 33
1

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];        
}
PWiggin
  • 906
  • 2
  • 12
  • 24
  • Thank you. I just wanted to change a background image, and the background was a CCNode. I'll let you know if I have any issues, when I go to try this later. – Jude Michael Murphy Feb 26 '14 at 17:34