我遇到了一个非常奇怪的错误导致我的应用崩溃。我创建了一个名为heroSpriteNode的自定义SKSpriteNode。这是我的初始化:
(id) init
{
if (self = [super init])
{
[self loadAnimations];
SKTexture *temp = self.gorillaStandingFrames[0];
self = [heroSpriteNode spriteNodeWithTexture:temp];
[self setUpHeroDetails];
}
return self;
}
尝试在" self"上运行操作时发生错误自定义SkSpriteNode,如下所示:
-(void)jump
{
NSLog(@"Jump Tap!");
if(self.isOnTheGround)
{
self.physicsBody.velocity = CGVectorMake(0, 0);
self.physicsBody.angularVelocity = 0;
self.timedJump=NO;
[self.physicsBody applyImpulse:CGVectorMake(0, 350)];
[self removeActionForKey:@"walkingInPlaceBear"];
[self jumpingGorilla2]; // HERE IS THE ERROR
self.isOnTheGround=NO;
self.isRunning=NO;
self.isStanding=NO;
self.jumpFixTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(flipTimedJumpBoolean) userInfo:nil repeats:NO];
}
}
我知道这个动画(以及我运行的其他动画)是个问题,因为当我注释掉那条线时游戏运行正常。
以下是我如何加载纹理和创建动作:
-(void)loadJumpAnimation2
{
NSMutableArray *jumpFrames = [NSMutableArray array];
SKTextureAtlas *gorillaAnimatedAtlas = [SKTextureAtlas atlasNamed:@"mainCharJump"];
int numImages = gorillaAnimatedAtlas.textureNames.count;
for (int i=2; i <= numImages; i++) {
NSString *textureName = [NSString stringWithFormat:@"j%d", i];
SKTexture *temp = [gorillaAnimatedAtlas textureNamed:textureName];
[jumpFrames addObject:temp];
}
self.gorillaFlyingJump = jumpFrames;
}
-(void)jumpingGorilla2
{
[self runAction:[SKAction repeatActionForever:
[SKAction animateWithTextures:self.gorillaFlyingJump
timePerFrame:0.25f
resize:NO
restore:YES]] withKey:@"jump"];
return;
}
当它崩溃时,记录器中没有崩溃报告,只有Xcode中的弹出消息类型为:9 SIGKILL。看起来这可能是与动画帧存储方式有关的内存问题?动画在标准的SkSpriteNode上运行良好,然后在将大部分代码重构为自定义SkSpriteNode之后,我再也无法应用动画了。有任何想法吗?谢谢。
更新:
我已经创建了另一个类来尝试隔离我遇到的问题,我将在下面发布评论代码。
这是我创建的整个测试类:
#import "heroTestNode.h"
@implementation heroTestNode
- (id) init
{
if (self = [super init])
{
[self loadAnimations];
SKTexture *temp = self.gorillaWalkingFrames[0];
self = [heroTestNode spriteNodeWithTexture:temp];
}
return self;
}
-(void)loadAnimations
{
NSMutableArray *walkFrames = [NSMutableArray array];
SKTextureAtlas *gorillaAnimatedAtlas = [SKTextureAtlas atlasNamed:@"mainCharRun2"];
int numImages = gorillaAnimatedAtlas.textureNames.count;
NSLog(@"Here are how many animation frames: %d", numImages);
for (int i=1; i <= numImages; i++) {
NSString *textureName = [NSString stringWithFormat:@"r%d", i];
SKTexture *temp = [gorillaAnimatedAtlas textureNamed:textureName];
[walkFrames addObject:temp];
}
self.gorillaWalkingFrames = walkFrames;
}
-(void)walkingGorilla // running this is causing the SIGKILL 9 crash
{
[self runAction:[SKAction repeatActionForever:
[SKAction animateWithTextures:self.gorillaWalkingFrames
timePerFrame:0.1
resize:NO
restore:YES]] withKey:@"test"];
return;
}
@end
这是界面:
#import <SpriteKit/SpriteKit.h>
@interface heroTestNode : SKSpriteNode
@property NSMutableArray *gorillaWalkingFrames;
-(void)walkingGorilla;
@end
最后,这是我如何实例化它:
heroTestNode *test = [[heroTestNode alloc] init];
test.position = CGPointMake((self.frame.size.width/2)+30,self.frame.size.height/3);
[self addChild:test];
[test walkingGorilla]; // this is causing the SIGKILL 9 crash
这导致它冻结&#34;压缩&#34;我的iPad左下角的视图并没有反应。我不知道会造成什么。