【问题标题】:cocos2d sprite repeat animation forevercocos2d sprite 永远重复动画
【发布时间】:2014-08-09 18:07:35
【问题描述】:

在我的 iOS 游戏中,我希望英雄继续奔跑,直到屏幕被触摸并且英雄应该跳跃。所以我写:
在 .h 文件中:

@interface Hero : CCSprite {
    CCSprite *_hero;
    id _keepRunning;
}
@property(nonatomic,retain) id keepRunning;

在.m文件中:

@synthesize keepRunning = _keepRunning;
-(id) init {
    _keepRunning = [CCRepeatForever actionWithAction:[CCAnimate actionWithSpriteSequence:@"heroRun%04d.png"
                                                                               numFrames:30
                                                                                   delay:0.02f
                                                                    restoreOriginalFrame:NO]];        
}

然后当游戏开始时,我调用run()方法:

-(void) run {
    [_hero stopAllActions];
    [_hero runAction:_keepRunning];
    _heroState = RUNNING;
}

然后我发现CCAnimate actionWithSpriteSequence: numFrames: delay: restoreOriginalFrame: 在 cocos2d v2.0 中被弃用了。所以我的问题是,在 cocos2d v2.0 中,我该如何实现这个动画呢?也就是说,让我的英雄继续奔跑?谢谢!
编辑:
我试过这个:

-(CCAnimation*) getMyAnimationWithFramesName:(NSString*)nameFormat numFrames:(int)numFrames delay:(float)delay {
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    for (int i = 1; i <= numFrames; i++) {
        NSString *frameName = [NSString stringWithFormat:nameFormat,i];
        [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName]];
    }
    CCAnimation *ani = [CCAnimation animationWithSpriteFrames:frames delay:delay];
    return ani;
}

然后在init()中:

_keepRunning = [self getMyAnimationWithFramesName:@"heroRun%04d.png" numFrames:30 delay:0.02f];

并在运行中():

[_hero runAction:[CCAnimate actionWithAnimation:_keepRunning]];

但它仍然不起作用。我该怎么办?

【问题讨论】:

  • 尝试更大的延迟(即 0.1)。尝试记录框架名称和/或框架数组以验证名称是否正确。此外,如果您不使用 ARC,则会泄漏帧数组。
  • @LearnCocos2D 我检查并发现我没有使用 ARC。泄漏帧数组的原因是什么?如何纠正它?谢谢!
  • 泄漏位于getMyAnimationWithFramesName:numFrames:delay: 方法的第一行。您不会破坏任何创建的数组frames
  • 您分配/初始化对象,而不是使用返回自动释放对象的 [NSMutableArray 数组]。规则是使用 alloc/init 获得对象的所有权。但是您不释放或自动释放它,因此它会泄漏。如果您不知道这一点,请检查您的其余代码是否有此类实例,最迟在下一个项目中使用 ARC。

标签: ios animation cocos2d-iphone sprite repeat


【解决方案1】:

首先你从'http://www.codeandweb.com/texturepacker/download'下载Texture 并制作 p-List 并在代码下方使用它

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

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimatedMan.png"];

[self addChild:spriteSheet];

收集帧列表(精灵)

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i=1; i<=6; i++) {
    [walkAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"step0%d.png",i]]];
}

将动作赋予精灵

CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.1f];

        manSprite=[CCSprite spriteWithSpriteFrameName:@"step01.png"];
        manSprite.position=ccp(winsize.width/2, winsize.height/2-40);

Sprite RepeaetForever for manSprite

id first=[CCSequence actions:[CCRepeatForever actionWithAction:
                                      [CCAnimate actionWithAnimation:walkAnim]],nil];

[manSprite runAction:first];

【讨论】:

    猜你喜欢
    • 2014-03-17
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多