2014-02-17 1032 views
4

我一直在嘗試做一個動畫雪碧,他們是很多教程,但他們都是爲Cocos2d 2.x.我的精靈表被命名爲flappbird.png和的.plist名爲flappbird.plist雪碧框架動畫Cocos2d 3.0

我有這樣的代碼,但每次我開始它只是崩潰的情景的時候,這是我初始化方法

// ----------------------------------------------------------------------- 

_player = [CCSprite spriteWithImageNamed:@"monster1.png"]; // comes from your .plist file 
_player.position = ccp(self.contentSize.width/28,self.contentSize.height/2); 
_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1 
_player.physicsBody.collisionGroup = @"playerGroup"; 
_player.physicsBody.type = CCPhysicsBodyTypeStatic; 
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"monster1.png"]; 
[batchNode addChild:_player]; 
[self addChild:batchNode]; 

NSMutableArray *animFrames = [NSMutableArray array]; 
for(int i = 1; i < 5; i++) 
{ 
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"flapbird%d.png",i]]; 
    [animFrames addObject:frame]; 
} 

CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f]; 
[_player runAction:[CCActionRepeatForever actionWithAction:[CCActionAnimate actionWithAnimation:animation]]]; 

[_physicsWorld addChild:_player]; 

// ----------------------------------------------------------------------- 
+2

的可能重複[如何動畫在cocos2d 3.X CCSprite? ](http://stackoverflow.com/questions/21645953/how-to-animate-ccsprite-in-cocos2d-3x) –

+0

看到我試圖執行這個問題的答案分鐘,但它會崩潰,什麼都不會發生 – Crazycriss

+0

當它崩潰時它會告訴你什麼? – connor

回答

8

動畫精靈與在cocos2d 3.0 spritesheet

確保你的代碼的開頭添加#import "CCAnimation.h"

的self.userInteractionEnabled後還要添加精靈表=是;在init

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"your.plist"]; 

沒有添加這一切在精靈會

//The sprite animation 
NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
for(int i = 1; i <= 7; ++i) 
{ 
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"monster%d.png", i]]]; 
} 
CCAnimation *walkAnim = [CCAnimation 
         animationWithSpriteFrames:walkAnimFrames delay:0.1f]; //Speed in which the frames will go at 

//Adding png to sprite 
monstertest = [CCSprite spriteWithImageNamed:@"monster1.png"]; 

//Positioning the sprite 
monstertest.position = ccp(self.contentSize.width/2,self.contentSize.height/2); 

//Repeating the sprite animation 
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim]; 
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction]; 

//Animation continuously repeating 
[monstertest runAction:repeatingAnimation]; 

//Adding the Sprite to the Scene 
[self addChild:monstertest]; 

希望這有助於人:d乾杯

+0

如果您已經將所有幀添加到緩存中作爲[CCSpriteBatchNode batchNodeWithFile:]調用的一部分,則行:monstertest = [...];可以讀取monstertest = [CCSprite spriteWithSpriteFrameName:]並傳入第一幀的名稱。這樣,精靈的初始圖像將從緩存中提取出來,而不是從.png單獨加載到內存中。 – PKCLsoft

+0

@PKCLsoft這是在以前的版本。在3.x中,它們都使用spriteWithImageNamed,它的工作方式是,如果它存在於緩存中,則將使用緩存。 – agro1986

+0

@ agro1986謝謝,我錯過了關鍵信息。 – PKCLsoft