Does anyone know how to change a CCNode's image programmatically? I'm using SpriteBuilder to make a simple game.
Asked
Active
Viewed 842 times
2

James Webster
- 31,873
- 11
- 70
- 114

Jude Michael Murphy
- 1,198
- 2
- 12
- 23
-
did you mean CCSprite? – CodeSmile Feb 26 '14 at 08:18
2 Answers
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,996
- 27
- 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
- 956
- 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