首页 > 代码库 > 诡异的SpriteKit 游戏查错

诡异的SpriteKit 游戏查错

在Endless Runner 游戏中,做了一些atlas后,发现有个问题,当player跳跃起来的时候,发现他没有动画了,被默认的X图片代替。
原来的图像是这样的。

在增加了一些动画后,我的效果就成这样了。


 
这个不知道为何?
当时碰到这个问题,我去查看了我的player.h,而且去看了的一些提供action的一些方法,结果还是不对,最后发现这个动画是在初始化的时候遍历我们的atlas来提供的,所以去查了一下,终于找到了答案。
原因:
在我们的player sprite相关的类,player.m中,在我们初始化的时候,我们调用了一一个方法来初始换我们的动画效果。代码如下
 

- (void) setupAnimations{    self.runFrames = [[NSMutableArray alloc]init];    SKTextureAtlas *runAtlas = [SKTextureAtlas atlasNamed:@"run"];        for (int i = 0; i < [runAtlas.textureNames count]; i++){        NSString *tempName = [NSString stringWithFormat:@"run%.3d", i];        SKTexture *tempTexture = [runAtlas textureNamed:tempName];        if (tempTexture) {            [self.runFrames addObject:tempTexture];        }    }        self.jumpFrames = [[NSMutableArray alloc] init];    SKTextureAtlas *jumpAtlas = [SKTextureAtlas atlasNamed:@"jump"];    for (int i = 0; i < [runAtlas.textureNames count]; i++){        NSString *tempName = [NSString stringWithFormat:@"jump%.3d", i];        SKTexture *tempTexture = [jumpAtlas textureNamed:tempName];        if (tempTexture) {            [self.jumpFrames addObject:tempTexture];        }    }

 


错误就是在这里,我当时是拷贝的函数过去去修改,结果漏了这一个地方,这个地方应该改成jumpAtlas,因为这个方法需要遍历我们的atlas,我们错误的让他去遍历我们的run.atlas文件夹,然后去寻找jump001, jump002等,所以他是找不到相关的文件的,所以导致了会出现默认的那个X来代替我的sprite.

总结,在我们写代码时,可能会出现各种的问题,在差错时会花费我们大量的时间。所以
1) 在拷贝相类似的代码时,需要再三确认是否需要修改。
2) 在我们的游戏出现行为不对的时候,需要先去找到初始化动画的代码,从源头查起,才不会有所疏漏。