精灵帧动画Cocos2d 3.0

4
我一直在尝试制作一个动画精灵,有很多教程,但它们都是针对Cocos2d 2.x的。我的精灵表被命名为flappbird.png,.plist被命名为flappbird.plist。 我有这段代码,但每次我启动场景时它都崩溃了,这是在我的< strong >init方法中。
// -----------------------------------------------------------------------

_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添加动画?的重复问题。 - Ben-G
我尝试将那个问题的答案应用到min函数中,但程序会崩溃并且没有任何反应。 - Crazycriss
当程序崩溃时,它告诉你什么? - Connor
2014-02-17 16:13:09.875 Bye Flappy[53793:70b] -[CCFileUtils fullPathForFilename:contentScale:] : cocos2d: 警告: 找不到文件: monster1.png。我认为问题可能是我的.plist没有链接。但是我该如何链接它呢?monster1.png在我的flappbird.plist中已定义。@connor - Crazycriss
1个回答

8

在Cocos2d 3.0中使用精灵表格进行动画绘制

请确保在代码开头添加#import "CCAnimation.h"

还需要在init中的self.userInteractionEnabled = YES;之后添加精灵表格。

[[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 干杯

如果您已经在[CCSpriteBatchNode batchNodeWithFile:]调用的一部分中将所有帧添加到缓存中,则该行:monstertest = [...]; 可以读取monstertest = [CCSprite spriteWithSpriteFrameName:],并传递第一个帧的名称。这样,精灵的初始图像就是从缓存中提取的,而不是单独从.png加载到内存中。 - PKCLsoft
@PKCLsoft 那是在之前的版本中。在3.x版本中,它们都使用spriteWithImageNamed,它的工作方式是如果在缓存中存在,则会使用缓存。 - august7cbfa7b7
@agro1986 谢谢,我错过了那个关键信息。 - PKCLsoft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接