【问题标题】:run more than one CCAction with same CCSprite使用相同的 CCSprite 运行多个 CCAction
【发布时间】:2013-04-03 11:49:19
【问题描述】:

我目前正在使用 Kobold2d (cocos2d) 开发一款手机游戏,但是我在使用多个动画制作一个 CCSprite 时遇到了一些麻烦。我的问题是:使用相同的 CCSprite(播放器)如何使用两种不同的动画对其进行动画处理。我正在使用 Kobold2D 2.0.4 和 Xcode 4.6,对于动画,我有一个用 pvr.ccz 压缩的 textureAtlas。

我创建了一个动画助手类:

+(CCAnimation *) startFrames:(int) startFrame
                 endFrame:(int) endFrame
                frameName:(NSString *) frameName
                    delay:(float) delay
{
    NSMutableArray *frames = [NSMutableArray arrayWithCapacity:endFrame];
    CCSpriteFrameCache *sharedFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    for (int i = startFrame; i <= endFrame; i++) {
        CCSpriteFrame *frame = [sharedFrameCache spriteFrameByName:[NSString stringWithFormat:@"%@%i.png",frameName,i]];
        [frames addObject:frame];
    }

    return [CCAnimation animationWithSpriteFrames:frames delay:delay];
}

+(CCAnimation *) jumpPoints:(CGPoint[]) points
                startFrames:(int) startFrame
                   endFrame:(int) endFrame
                  frameName:(NSString *) frameName
                      delay:(float) delay
{
    NSMutableArray *frames = [NSMutableArray arrayWithCapacity:endFrame];
    CCSpriteFrameCache *sharedFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    for (int i = startFrame; i <= endFrame; i++) {
        CCSpriteFrame *frame = [sharedFrameCache spriteFrameByName:[NSString stringWithFormat:@"%@%i.png",frameName,i]];
        [frame setOffsetInPixels:points[i]];
        [frames addObject:frame];
    }

    return [CCAnimation animationWithSpriteFrames:frames delay:delay];
}

我有一个播放器类,我在其中初始化播放器并创建两个 CCAnimation。在头文件中,我存储了两个 CCAnimate 类型变量:

CCAnimate *jump;
CCAnimate *walk;

在.m文件中

-(id) init
{
    if (self = [super init])
    {        
        sprite = [[Loading shareLoading] loadTempCcsprite];
        CGSize screenSize = [[CCDirector sharedDirector] winSize];

        localPostion = CGPointMake(screenSize.width, screenSize.height);

        CGPoint points[] = {CGPointMake(0, 10),CGPointMake(0, 50),CGPointMake(0, 80),
            CGPointMake(0, 10),CGPointMake(0, 0)};
        CCAnimation *animationJump = [CCAnimationHelper jumpPoints:points startFrames:0 endFrame:4 frameName:@"cactus" delay:0.10f];

        jump = [CCAnimate actionWithAnimation:animationJump];

        CCAnimation *animationWalk = [CCAnimationHelper startFrames:0 endFrame:4 frameName:@"cactus" delay:0.10f];

        walk = [CCAnimate actionWithAnimation:animationWalk];

        [self addChild:sprite];
    }
    return self;
}

最后,我有输入层,用户有两个跳跃按钮和攻击和操纵杆来移动玩家。

代码如下:

-(void) update:(ccTime)delta
{
    if ([jump active])
    {
        CCAnimate *animation = [player jump];
        [player runAction:animation];
    }
    else if (joystick.velocity.x > 0)
    {
        float velocity = [joystick velocity].x * 700 * delta;

        CGPoint playerPosition = CGPointMake([player localPostion].x + velocity * delta, [player localPostion].y    );

        id animation = [player walk];
        [player runAction:animation];
        [player setLocalPostion:playerPosition];
    }
}

如果我编译这段代码,在输入层当我按下跳转按钮时游戏崩溃并且输出控制台显示:

* -[CCActionManager addAction:target:paused:] 中的断言失败

【问题讨论】:

  • 断言错误信息是什么?
  • 当编译器继续执行此指令时 [player runAction:animation];控制台输出是这样的:2013-04-03 13:08:35.258 CactusJourney-iOS[920:c07] dealloc: 2013-04-03 13:08:36.454 CactusJourney-iOS[920:c07] *** -[CCActionManager addAction:target:paused:] 中的断言失败,/Users/User/Kobold2D/Kobold2D-2.0 .4/__Kobold2D__/libs/cocos2d-iphone/cocos2d/CCActionManager.m:171

标签: cocos2d-iphone ccsprite kobold2d ccaction


【解决方案1】:

您正在尝试在同一操作仍在运行时再次运行它。

为避免这种情况,请始终在 runAction 之前调用 stopAction:

[player stopAction:animation];
[player runAction:animation];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多