我目前正在为iPhone编写游戏,有时会因标题错误而随机崩溃。
我已经研究了很多,这可能与内存泄漏问题有关。我问的人告诉我,这可能与添加nil对象有关。我设置了异常断点和NSZombies,但是它们都不识别崩溃,也没有给出崩溃发生的确切代码行。
经过一些崩溃测试后,我注意到在大多数时候,当touchesBegan方法或didBeganContact方法处于 Activity 状态时,就会发生这种情况。
这是touchesBegan方法和didBeginContact方法的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchEnabled == YES){
firstTouch++;
if (firstTouch == 1){
SKAction* removeFade = [SKAction removeFromParent];
SKAction* startFade = [SKAction fadeAlphaTo:0 duration:0.5];
SKAction* fadeSeq = [SKAction sequence:@[startFade, removeFade]];
[startScreen runAction:fadeSeq completion:^{
startScreenRemoved = YES;
[self gameCountdown];
touchToShoot = YES;
}];
}
if (firstTouch == 2){
weaponActivated = YES;
}
if (gameOver == YES){
[self removeAllActions];
[self removeAllChildren];
[bgPlayer stop];
touchToShoot = NO;
SKScene* gameScene = [[GameScene alloc] initWithSize:self.size];
SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:1];
[self.view presentScene:gameScene transition:doors];
gameOver = NO;
}
}
if (touchToShoot == YES) {
[self weaponParticle];
touchToShoot = NO;
[self performSelector:@selector(enableTouchToShoot) withObject:nil afterDelay:0.3]; //-enableTouchToShoot{} = touchToShoot = YES;
}
//Pause Button
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"pauseButton"]) {
[self pauseMenu];
}
else if ([node.name isEqualToString:@"continue"]) {
self.paused = NO;
pauseBG.hidden = YES;
pauseText.hidden = YES;
backm.hidden = YES;
cont.hidden = YES;
//Damit unsichtbare Buttons nicht während dem Spiel gedrückt werden
backm.zPosition = -100;
cont.zPosition = -100;
}
else if ([node.name isEqualToString:@"backtomenu"]) {
self.paused = NO;
[self displayAlert];
}
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
if (contact.bodyA.categoryBitMask == shipCategory) {
//Explosion Animation
SKAction* wait =[SKAction waitForDuration:0.5];
SKAction* fadeOut = [SKAction scaleTo:0.0 duration:1];
remove = [SKAction removeFromParent];
explodeSequenceGO =[SKAction sequence:@[fadeOut,wait,remove]];
ExplosionPath = [[NSBundle mainBundle] pathForResource:@"Explosion" ofType:@"sks"];
Explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:ExplosionPath];
Explosion.position = CGPointMake(contact.bodyA.node.position.x, contact.bodyA.node.position.y);
[self addChild:Explosion];
[Explosion runAction:explodeSequenceGO];
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent];
[shipSmoke removeFromParent];
[player1 removeFromParent];
touchEnabled = NO;
[self gameOver];
}
if (contact.bodyA.categoryBitMask == enemyCategory || contact.bodyB.categoryBitMask == enemyCategory) {
//Explosion Animation
SKAction* wait =[SKAction waitForDuration:0.5];
SKAction* fadeOut = [SKAction scaleTo:0.0 duration:1];
remove = [SKAction removeFromParent];
explodeSequenceGO =[SKAction sequence:@[fadeOut,wait,remove]];
ExplosionPath = [[NSBundle mainBundle] pathForResource:@"Explosion" ofType:@"sks"];
Explosion = [NSKeyedUnarchiver unarchiveObjectWithFile:ExplosionPath];
Explosion.position = CGPointMake(contact.bodyA.node.position.x, contact.bodyA.node.position.y);
[Explosion runAction:explodeSequenceGO];
[self addChild:Explosion];
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent];
hitCount++;
[self scoreChange:100];
}
if (hitCount>39) {
[self eLevel2];
}
}
有人看到故障吗?我非常感谢您提供任何提示,因为我一直在寻找此错误数周...。
编辑:崩溃仅指向“主要”功能,这根本没有帮助
在每个线程“操作”中,它仅指向Assembly(?)代码:
就像我说的,我试图通过各种调试工具(NSZombies,MemoryTool,Exceptional Breakout等)来分析崩溃,但是它们都没有给我有用的信息。当应用程序崩溃时,调试工具将停止记录,但它们不会显示任何故障或崩溃结果。
最佳答案
我知道这已经几个月了,但是当从一个场景过渡到另一个场景时,我也遇到了EXC_BAD_ACCESS错误。在阅读了多篇文章并注释掉了我游戏中几乎所有的代码行之后,对我来说解决的问题是从方法返回之前删除了现有场景中的所有对象,并且爆炸错误消失了。
- (void)returnToMenuScene {
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
MenuScene *menuScene = [MenuScene sceneWithSize:self.scene.size];
[self.scene.view presentScene:menuScene transition:reveal];
[self.gameScene removeAllChildren];
return;
}
- (GameScene *)gameScene {
return (GameScene *)self.scene;
}
我希望这对以后的人有帮助
关于objective-c - SpriteKit中的EXC_BAD_ACCESS(代码= 1,地址= 0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26556841/