0

I want to select the player image from gallery or I can take picture from cam and I need to animate those images while player movement. I am new to cocos2d, can any body help me ?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Raj
  • 11
  • 5
  • [playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerWithShield.png"]]; – Anjaneyulu Jul 16 '13 at 04:59
  • I need to take the picture dynamically and that should be applied to players image – Raj Jul 16 '13 at 05:10
  • 1
    [playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:[UIImage imageNamed:[NSString stringwithformat@"%@",yourimage]]]]; – Anjaneyulu Jul 16 '13 at 05:16
  • Thanks man,but at a time i need to show 3-4 animations while player moving.... – Raj Jul 16 '13 at 05:19
  • u can use nstimer to achieve that problem @Raj – Anjaneyulu Jul 16 '13 at 05:22
  • actually in my game i need to take picture from cam for player and than u need to animate that picture for players images dynamically in cocos2d game – Raj Jul 16 '13 at 05:25
  • sorry dear...i don't have much knowledge in cocos2d game @Raj....... – Anjaneyulu Jul 16 '13 at 05:28
  • http://stackoverflow.com/questions/6896075/changing-the-image-of-a-ccsprite – iPatel Jul 16 '13 at 08:20
  • http://www.learn-cocos2d.com/api-ref/latest/cocos2d-iphone/html/interface_c_c_sprite.html#ae41c556a12991defd766670eae6ba700 – iPatel Jul 16 '13 at 08:20

2 Answers2

0

To change player animations, you need to run a CCAnimate action on it.

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

So, if you want to change the player image, just create a new animation action with the new images that you want.

PS.: If you just want to change player image, without animation, use:

[player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"image.png"]];
Bivis
  • 1,320
  • 1
  • 10
  • 24
  • Take a look at Apple documentation. It also have examples: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010405-SW1 – Bivis Jul 17 '13 at 13:56
0

In COCOS2D-X you can do this by following way

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

iF YOU WANT TO CHANGE THE SPRITE SIZE THEN ALSO WRITE THIS LINE

sprit_name->setTextureRect(CCRectMake(0,0, tex->getContentSize().width, tex->getContentSize().height));
Singhak
  • 8,508
  • 2
  • 31
  • 34