【问题标题】:Animating a Sprite in Cocos2D and Spritebuilder在 Cocos2D 和 Spritebuilder 中为 Sprite 设置动画
【发布时间】:2014-02-21 02:49:28
【问题描述】:

在我正在处理的项目中,我有多个可以扮演的角色(在这种情况下,我将它们称为无人机)。在我的游戏场景中,我通过 _drone 进行了所有物理设置。使用 spritebuilder 创建的 .CCB 文件。以前我通过 spritebuilder 为这个文件设置动画,但现在添加了多个无人机可供选择,我需要将其设置为显示并循环显示适当的帧。我整天都在寻找与此相关的东西,我看到的大多数答案都是针对 cocos2d v2.0 的,或者当我得到在 Xcode 中没有显示错误的东西时,它不适用于我的 _drone 类。我想做的是这样的:[_drone setSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"DefectiveDroneSpriteSheet/Drone1.png"]]; 因为这将 _drone 的框架设置为我想要的。但是我不知道如何在帧之间进行这个循环。

我最近在这里找到了一个答案,显示了如何做到这一点:

NSMutableArray *animationFrames = [NSMutableArray array];

for(int i = 1; i <= FRAMES; ++i)
{
    CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
}

//Create an animation from the set of frames you created earlier
CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:delay];

//Create an action with the animation that can then be assigned to a sprite
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[self runAction:repeatingAnimation];

不幸的是,这对我不起作用,但也许我做错了什么。我似乎对 for 循环/警告说变量 spriteFrame 从未使用过,或类似的东西有最大的问题。这段代码的问题在于,经过数小时的修改并试图找到更新的文档后,我无法弄清楚如何执行我的第一个示例所做的操作,将其直接应用到 _drone。所以说了这么多......我应该怎么做才能解决这个问题?我忽略了另一种更简单的方法吗?

感谢您的宝贵时间,非常感谢!

为大师编辑:

您好,感谢您的回复。这就是我的 for 循环目前的样子,它向我抛出了 4 个警告。

for(int i = 1; i <= 2; ++i)
{
    CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DefectiveDroneSpriteSheet/Drone1.png", i]]; //
    CCSpriteFrame *frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DefectiveDroneSpriteSheet/Drone1.png", i]]; //
}

我收到警告“第 1 帧和第 2 帧的未使用变量”和“i”的“格式字符串未使用的数据参数”。同样这样做,如何使此动画应用于我的 _drone 对象?由于对象已经通过 Spritebuilder 放置在场景中?


第二次编辑:

- (void)didLoadFromCCB {

    NSMutableArray *animationFrames = [NSMutableArray array];

    for(int i = 1; i <= 2; ++i)
    {
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DroneSpriteSheets%d.png", i]]; //
        [animationFrames addObject: spriteFrame];
    }

    //Create an animation from the set of frames you created earlier
    CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:1.0f];

    //Create an action with the animation that can then be assigned to a sprite
    CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

    CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
    [_drone runAction:repeatingAnimation];

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

这是我代码的相关部分,我收到此错误:

* 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

【问题讨论】:

    标签: ios animation cocos2d-iphone spritebuilder


    【解决方案1】:

    您没有在数组中添加帧:

    for(int i = 1; i <= FRAMES; ++i)
    {
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
        [animationFrames addObject: spriteFrame];
    
    }
    

    【讨论】:

    • 两个错误,一个错误的精灵帧,没有添加到数组中。检查您的精灵表以获取正确的精灵名称
    • 不知道为什么我错过了它,我修复了它,我的 spritesheet 名称实际上称为“DroneSpriteSheets.png”,我在 Xcode 中检查了它,两个帧是 Drone1 和 Drone2 .png。我在 xcode 中没有错误,但是在第二次编辑中,您可以看到我的完整动画代码以及引发的错误。我不确定是什么原因造成的。
    • 我解决了这个问题:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 2014-03-17
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多