2014-02-17 4 views
4

Я пытаюсь сделать анимированный спрайт, у них много учебников, но все они для Cocos2d 2.x. Мой спрайт лист называется flappbird.png и .plist назван flappbird.plistSprite Frame Animation 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

возможного дубликату [Как не анимировать CCSprite в cocos2d 3.x? ] (http://stackoverflow.com/questions/21645953/how-to-animate-ccsprite-in-cocos2d-3-x) –

+0

См., что я попытался выполнить ответ этого вопроса на мин, но он потерпит крах и ничего не произойдет – Crazycriss

+0

Что он говорит вам, когда он падает? – connor

ответ

8

Animate спрайт с spritesheet в Cocos2d 3.0

Убедитесь, что добавить #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