2014-02-17 977 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