gpt4 book ai didi

ios - Sprite 帧动画 Cocos2d 3.0

转载 作者:可可西里 更新时间:2023-11-01 05:12:43 28 4
gpt4 key购买 nike

我一直在尝试制作一个动画 Sprite ,它们有很多教程,但它们都是针对 Cocos2d 2.x 的。我的 Sprite 表名为 flappbird.png,.plist 名为 flappbird.plist

我有这段代码,但每次我启动场景时它都会崩溃,这是在我的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];

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

最佳答案

在 Cocos2d 3.0 中使用 spritesheet 动画 Sprite

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

同时在 self.userInteractionEnabled = YES 之后添加 sprite sheet;在初始化

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

不要在 Sprite 所在的地方添加所有这些

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

关于ios - Sprite 帧动画 Cocos2d 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com