【问题标题】:Can We change the players images dynamically in cocos2d game?cocos2d游戏中可以动态改变玩家形象吗?
【发布时间】:2013-07-16 04:55:40
【问题描述】:

我想从图库中选择玩家图像,或者我可以从 cam 中拍照,并且我需要在玩家移动时为这些图像设置动画。我是 cocos2d 的新手,任何人都可以帮助我吗?

【问题讨论】:

  • [playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerWithShield.png"]];
  • 我需要动态拍照,应该应用到玩家图像
  • [playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:[UIImage imageNamed:[NSString stringwithformat@"%@",yourimage]]]];
  • 谢谢大佬,但我需要在玩家移动时显示 3-4 个动画......
  • 你可以使用 nstimer 来解决这个问题@Raj

标签: ios iphone cocos2d-iphone cocos2d-x


【解决方案1】:

要更改播放器动画,您需要对其运行CCAnimate 操作。

// 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]];

因此,如果您想更改播放器图像,只需使用您想要的新图像创建一个新的动画动作。

PS.:如果你只是想改变播放器图像,没有动画,使用:

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

【讨论】:

【解决方案2】:

在 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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多