2014-04-28 81 views
0

目前正在設法找到解決,自己目前是傷害我的應用程序問題的SKNode。我想看看我是否可以改變下面的代碼來減少內存。現在,我有五個方法,所有做到以下幾點,Attemped添加已經有一個父

-(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 spriteNodeWithImageNamed:@"obstacle"]; 
    obstacle.position = CGPointMake(startPoint.x, startPoint.y); 
    obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5]; 
    obstacle.physicsBody.categoryBitMask = enemyCategory; 
    obstacle.physicsBody.contactTestBitMask = playerCategory; 
    obstacle.physicsBody.dynamic = NO; 
    obstacle.name = @"obstacle0"; 
    [self addChild:obstacle]; 

    [obstacle runAction:[SKAction moveTo:CGPointMake(340, startPoint.y) duration:minTime + arc4random_uniform(maxTime - minTime)]]; 
    float randomNum = arc4random_uniform(3.0) + 0.1; 
    [self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum]; 
} 

我想聲明我的.h文件SKSpriteNode,並有代替SKSpriteNode他們的內聲明的每個方法的使用,但我得到上述錯誤。有人可以告訴我如何改變我的代碼,所以圖像「障礙」只加載一次。

回答

0

從代碼位您發佈它似乎正在運行一個永無止境的循環創造了障礙對象。

嘗試刪除代碼行[self performSelector:@selector(createObstacle0) withObject:nil afterDelay:randomNum];

添加一個定時器功能是這樣的:

- (void)startTimer 
{ 
[NSTimer scheduledTimerWithTimeInterval:1 
           target:self 
           selector:@selector(timerAction) 
           userInfo:nil 
           repeats:YES]; 
} 

-(void)timerAction 
{ 
    NSLog(@"do something"); 
} 

您可以通過調用它像這樣[self startTimer];

您可以做到這一點開始startTimer方法要麼你的init方法或你需要的任何地方。

+0

是的,我知道,物體會一直產卵,直到玩家擊中他們中的一個,場景轉換,這是故意的。 – user3552678

+0

@ user3552678 - 刪除線後是否有效? – sangony

+0

它有效,沒有崩潰或任何事情,但我的遊戲有很多來自側面的物體,沒有這條線,它們不會繼續來,只有一個會。 – user3552678

0

這裏有可能是多一點「SpriteKit」之類的答案。您還可以將創建過程的大部分放入後臺線程的塊中,然後根據應用程序中其他內容的強度,在主線程中執行添加和操作。下面例子的關鍵在於它使用的是動作,而序列有一個關鍵。

-(void)createObstacleRepeatingMethod { 

int yMin = (CGRectGetMidY(thingShowingObstacles.frame)+190); 
int yMax = (CGRectGetMidY(thingShowingObstacles.frame)+270); 

CGPoint startPoint = CGPointMake(-20, yMin + arc4random_uniform(yMax - yMin)); 
SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"obstacle"]; 

obstacle.position = CGPointMake(startPoint.x, startPoint.y); 

obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5]; 
obstacle.physicsBody.categoryBitMask = obstacleCategory; 
obstacle.physicsBody.contactTestBitMask = playerCategory; 
obstacle.physicsBody.dynamic = NO; 
obstacle.name = @"obstacle0"; 

[thingShowingObstacles addChild:obstacle]; 
[obstacle runAction:[SKAction moveTo:CGPointMake(340, startPoint.y) duration:minTime + arc4random_uniform(maxTime - minTime)]]; 

SKAction *waitAction = [SKAction waitForDuration:1.5 withRange:1.4]; 
SKAction *callCreateObstacleAgain = [SKAction runBlock:^{ 
    [thingShowingObstacles createObstacleRepeatingMethod]; 
}]; 

SKAction *theSequenceForWaitThenCall = [SKAction sequence:@[waitAction, callCreateObstacleAgain]]; 

[thingShowingObstacles runAction:theSequenceForWaitThenCall withKey:@"createObstacleSequence"]; 

//the sequence having a key also ensures that there will only ever be one of the sequence actions running 
//it also ensures that you have fine grained control over removing the action by calling 
[thingShowingObstacles removeActionForKey:@"createObstacleSequence"]; } 
相關問題