我的sprite kit游戏一直存在内存问题,我已经尝试了所有我知道的修复它但仍然没有。关于这个问题的一点背景;第一次玩游戏时,内存很好,它保持完全稳定。但是当你死了并再次打出来的时候,那就是记忆开始爬起来的时候。似乎你再次玩的越多,记忆越快越好。你可以在任何场景中,所使用的内存仍然会爬升。这是一张显示乐器的图片,http://postimg.org/image/5olu8h7tp/。
这是我的代码:
创建对象:
-(void)createObstacle0 {
int yMin = (CGRectGetMidY(self.frame)+190);
int yMax = (CGRectGetMidY(self.frame)+270);
CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uniform(yMax - yMin));
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImagedNamed:@"obstacle"];
obstacle.position = CGPointMake(startPoint.x, startPoint.y);
obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21];
obstacle.physicsBody.categoryBitMask = enemyCategory;
obstacle.physicsBody.contactTestBitMask = playerCategory;
obstacle.physicsBody.dynamic = NO;
obstacle.name = @"obstacle";
[self addChild:obstacle];
SKAction *move = [SKAction moveTo:CGPointMake(340, startPoint.y) duration:minTime +arc4random_uniform(maxTime - minTime)];
SKAction* removeObstacle = [SKAction removeFromParent];
[obstacle runAction:[SKAction sequence:@[move, removeObstacle]] completion:^{
score++;
scorelabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)score];
}];
float randomNum = arc4random_uniform(3.0) + 0.2;
[self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum];
}
碰撞探测器和场景转换:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (playerCategory | enemyCategory)) {
playerAlive = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:@"playerScore"];
[defaults synchronize];
int highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"High Score"] integerValue];
if (highScore){
if (highScore < score){
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"High Score"];
}
}
else{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"High Score"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
SKTransition *reveal = [SKTransition fadeWithDuration:0.75];
MGLCreateGameOverScene *scene = [MGLCreateGameOverScene sceneWithSize:self.view.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:scene transition:reveal];
}
}
删除剩余节点:
-(void)update:(CFTimeInterval)currentTime {
if (playerAlive == NO) {
[self enumerateChildNodesWithName:@"obstacle" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
}
}
正如我之前所说,你第一次玩,记忆保持完全稳定。只有在第一次记忆开始上升之后。看起来你重新访问主游戏场景越多,内存爬得越快。我真的需要帮助解决这个问题,我的游戏已经停止了两个星期了,我真的想要它备份。我感谢所有的帮助。
答案 0 :(得分:3)
你的问题在于:
[self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum];
此方法实际上保留self
(通过创建计时器),直到执行选择器。您需要做的是在更改场景之前添加方法didBeginContact:
以下行:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(createObstacle0) object:nil];
这会破坏保留self的计时器并无休止地调用该方法。
您可以通过在场景的NSLog(@"%s", __PRETTY_FUNCTION__);
方法中添加对dealloc
的调用来检查是否有效。