Ios 我们可以在cocos2d游戏中动态更改玩家的图像吗?

Ios 我们可以在cocos2d游戏中动态更改玩家的图像吗?,ios,iphone,cocos2d-iphone,cocos2d-x,Ios,Iphone,Cocos2d Iphone,Cocos2d X,我想从gallery中选择玩家图像,或者我可以从cam中拍照,我需要在玩家移动时设置这些图像的动画。我是cocos2d新手,有人能帮我吗?要更改玩家动画,你需要对其运行cAnimate操作 // Add sprite frames to sprite frame cache (if you are using a spritesheet) [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"NinjaPre

我想从gallery中选择玩家图像,或者我可以从cam中拍照,我需要在玩家移动时设置这些图像的动画。我是cocos2d新手,有人能帮我吗?

要更改玩家动画,你需要对其运行
cAnimate
操作

// Add sprite frames to sprite frame cache (if you are using a spritesheet)
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"NinjaPreto.plist"];

// Create animation
CCAnimation* animation = [CCAnimation animation];

// Create sprite frames
CCSpriteFrame *spriteFrame1 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"animation1.png"];
CCSpriteFrame *spriteFrame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"animation2.png"];
CCSpriteFrame *spriteFrame3 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"animation3.png"];

// Add sprite frames to animation
[animation addSpriteFrame:spriteFrame1];
[animation addSpriteFrame:spriteFrame2];
[animation addSpriteFrame:spriteFrame3];

animation.delayPerUnit = 0.1;
animation.restoreOriginalFrame = NO;

// Run action
[player runAction:[CCRepeatForever actionWithAction:animation]];
因此,如果要更改播放器图像,只需使用所需的新图像创建一个新的动画动作

注:如果您只想更改播放器图像,而不需要动画,请使用:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"image.png"]];

在COCOS2D-X中,您可以通过以下方式执行此操作

CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage("xyz.png");
sprit_name->setTexture(tex);
如果你想改变精灵的大小,那么也写这行

sprit_name->setTextureRect(CCRectMake(0,0, tex->getContentSize().width, tex->getContentSize().height));

[PlayerPrite setTexture:[CCTextureCache sharedTextureCache]添加图像:@“playerWithShield.png”];我需要动态拍摄照片,这应该应用于玩家图像[PlayerPrite setTexture:[[CCTextureCache sharedTextureCache]addImage:[UIImage ImageName:[NSString stringwithformat@“%@”,yourimage]]];谢谢你,伙计,但有时我需要在播放器移动时显示3-4个动画……你可以使用nstimer来解决这个问题@Raja看看苹果的文档。它也有一些例子: