Cocos2d - Change CCNode Image Programmatically

浪子不回头ぞ 提交于 2019-12-08 02:59:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!