2014-02-21 38 views
0

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

我发现这里的答案最近,展示了如何做到这一点:

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 can not be nil'

回答

0

您不在数组中添加帧:

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

} 
+0

检查编辑请:) – BasedRebel

+0

两个错误,一个错误的精灵帧,并没有添加到数组。检查你的spritesheet是否有正确的精灵名字 – Guru

+0

不知道为什么我错过了,我修正了这个问题,并为我的spritesheet名称,它实际上被称为“DroneSpriteSheets.png”我在Xcode中检查它,两个帧是Drone1和Drone2.png。我在xcode中没有错误,但在第二次编辑中,您可以看到我的完整动画代码以及所引发的错误。我不确定可能导致它的原因。 – BasedRebel