问题
Does anyone know how to change a CCNode's image programmatically? I'm using SpriteBuilder to make a simple game.
回答1:
A CCNode does not have an image. Only CCSprites have images.
You can change the image of a CCSprite using the spriteFrame
property.
回答2:
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];
}
来源:https://stackoverflow.com/questions/22029250/cocos2d-change-ccnode-image-programmatically