Ios 精灵帧动画COCOS2D3.0
我一直在尝试制作一个动画精灵,它们有很多教程,但都是针对CoCoS2D2.x的。我的精灵表名为flappbird.png,.plist名为flappbird.plist 我有这段代码,但每次我启动场景它都会崩溃,这是在我的init方法中Ios 精灵帧动画COCOS2D3.0,ios,objective-c,cocos2d-iphone,sprite-sheet,Ios,Objective C,Cocos2d Iphone,Sprite Sheet,我一直在尝试制作一个动画精灵,它们有很多教程,但都是针对CoCoS2D2.x的。我的精灵表名为flappbird.png,.plist名为flappbird.plist 我有这段代码,但每次我启动场景它都会崩溃,这是在我的init方法中 // ----------------------------------------------------------------------- _player = [CCSprite spriteWithImageNamed:@"monster1.png
// -----------------------------------------------------------------------
_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];
// -----------------------------------------------------------------------
//-----------------------------------------------------------------------
_player=[CCSprite spriteWithImageNamed:@“monster1.png”];//来自您的.plist文件
_player.position=ccp(self.contentSize.width/28,self.contentSize.height/2);
_player.physicsBody=[ccphysisbody body withrect:(CGRect){CGPointZero,_player.contentSize}cornerRadius:0];//1.
_player.physicsBody.collisionGroup=@“playerGroup”;
_player.physicsBody.type=ccphysisbodytypestatic;
CCSpriteBatchNode*batchNode=[CCSpriteBatchNode batchNodeWithFile:@“monster1.png”];
[batchNode addChild:_player];
[self addChild:batchNode];
NSMutableArray*animFrames=[NSMutableArray];
对于(int i=1;i<5;i++)
{
CCSpriteFrame*frame=[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:[NSString stringWithFormat:@“flapbird%d.png”,i]];
[动画帧添加对象:帧];
}
CCAnimation*animation=[CCAnimation animation with priteframes:animFrames delay:0.2f];
[_播放器运行动作:[CCActionRepeatForever actionWithAction:[CCActionAnimate actionWithAnimation:animation]];
[[u physicsWorld addChild:[u player];
// -----------------------------------------------------------------------
在COCOS2D3.0中使用精灵表设置精灵动画
确保在代码开头添加#导入“CCAnimation.h”
在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];
//精灵动画
NSMutableArray*walkAnimFrames=[NSMutableArray];
对于(int i=1;我可能重复看到我尝试将该问题的答案实现为min,但它会崩溃,什么也不会发生它崩溃时会告诉您什么?2014-02-17 16:13:09.875 Bye Flappy[53793:70b]-[CCFileUtils fullPathForFilename:contentScale:]:cocos2d:Warning:File not found:monster1.png
我想问题在于我的.plist没有链接。但是我该如何链接它呢?monster1.png是在我的flappbird.plist@connori中定义的。如果您已经将所有帧作为[CCSpriteBatchNode batchNodeWithFile:]调用的一部分添加到缓存中,那么行:monstertest=[…];无法读取monstertest=[CCSprite SpriteWithPriteFrameName:]并传入第一帧的名称。这样,精灵的初始图像将从缓存中提取,而不是单独从.png加载到内存中。@PKCLsoft,这是以前的版本。在3.x中,它们都使用spriteWithImageNamed,其工作方式是,如果它存在于缓存中,则将使用缓存。@agro1986,谢谢,我错过了那条关键信息。