2013-07-16 26 views
0

我想从画廊中选择播放器图像,或者我可以从凸轮拍摄照片,并且需要在播放器移动时为这些图像制作动画。我对cocos2d很陌生,身体能帮助我吗?我们可以在cocos2d游戏中动态更改玩家图片吗?

+0

[playerSprite的SetTexture:[[CCTextureCache sharedTextureCache] addImage:@“playerWithS hield.png“]]; – Anjaneyulu

+0

我需要动态地拍照,并且应该应用于玩家图片 – Raj

+1

[playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:[UIImage imageNamed:[NSString stringwithformat @“%@”,yourimage]]]]; – Anjaneyulu

回答

0

要更改播放器动画,您需要对其执行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"]]; 
+0

如何从玩家图片相机图片.. – Raj

+0

看看苹果的文档。它也有例子:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010405-SW1 – Bivis

0

在cocos2d-X,您可以通过以下方式

CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage("xyz.png"); 
sprit_name->setTexture(tex); 

做到这一点,如果你想CHANGE THE SPRITE SIZE然后还要在此线

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