2014-09-02 133 views
1

我有一個敵人節點,它通過更新它在更新方法中的位置而移動,我在一個設定的時間間隔內添加新的節點,當兩個相同的節點出現時,最近的節點更新它的位置並且最後一個節點不更新它的位置,我怎麼寫相應的代碼來更新我的敵人節點的所有位置,當我增加更多?在添加新節點的同時更新節點的位置?

敵人方法

-(void)Enemies { 

SKTexture *pM1 = [SKTexture textureWithImageNamed:@"enemy-1"]; 

enemy = [SKSpriteNode spriteNodeWithTexture:pM1]; 
enemy.zPosition = 8; 
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size]; 
enemy.physicsBody.categoryBitMask = fEnemyCategory; 
enemy.physicsBody.contactTestBitMask = fPlatformCategory | fMainPlatformCategory | fPlayerCategory | fPitOfCertainDoomCategory; 
enemy.physicsBody.collisionBitMask = fPlatformCategory | fPlayerCategory | fEnemyCategory; 
enemy.physicsBody.allowsRotation = NO; 
enemy.physicsBody.friction = 0.3; 
enemy.physicsBody.linearDamping = 0.7; 
enemy.physicsBody.dynamic = YES; 
enemy.physicsBody.affectedByGravity = YES; 
enemy.physicsBody.usesPreciseCollisionDetection = YES; 
enemy.position = CGPointMake(CGRectGetMidX(self.frame) + 30, CGRectGetMidY(self.frame)); 
enemy.position = CGPointMake(730, enemy.position.y - 129); 
NSLog(@"Spawned"); 

[self addChild:enemy]; 
[enemy runAction:runAnimation]; 
} 

更新方法

-(void)update:(CFTimeInterval)currentTime { 

if (gameStart == YES & gameOver == NO) { 

if (enemyExists == YES) { 

     if (switchMovement == NO) { 
      enemy.position = CGPointMake(enemy.position.x - 3.66, enemy.position.y); 
     } 
     else if (switchMovement == YES) { 
      enemy.position = CGPointMake(enemy.position.x + 2.83, enemy.position.y); 
     } 

} 
+1

你需要使用一個NSMutableArray,現在你有一個名爲敵人的單個伊娃,可以持有對一個敵人的引用 – LearnCocos2D 2014-09-03 07:15:19

+0

@ LearnCocos2D可能我問如何實現它一個NSMutableArray?因爲這個特殊的敵人會一遍又一遍地產卵,同樣的確切的一個,我不是那麼熟悉NSMutableArray – Blank 2014-09-03 19:47:29

回答

0

這是你將如何實現@ LearnCocos2D提到什麼。我做任何承諾在適當的鑄造對象作爲spritenode;)

定義數組:

NSMutableArray *array = [NSMutableArray alloc] init]; 

添加敵人陣:

[array addObject:myObject]; //Directly after [self addChild:enemy]; 

新更新:

-(void)update:(CFTimeInterval)currentTime { 

    if (gameStart == YES & gameOver == NO) 
    {  
      for (NSObject* o in array1) 
      { 
       SKSpriteNode enemyTemp = (SKSpriteNode*)o; 

       if (switchMovement == NO) 
       { 
         enemyTemp.position = CGPointMake(enemyTemp.position.x - 3.66, enemy.position.y); 
        } 
        else if (switchMovement == YES) 
        { 
         enemyTemp.position = CGPointMake(enemyTemp.position.x + 2.83, enemy.position.y); 
        } 
       } 
     } 
+0

謝謝!真的很感激它。 – Blank 2014-09-05 18:51:08

+0

爲了避免Exists檢查,將陣列中的敵人從場景中移除時將其移出。這保證了每個數組對象的存在並將有助於提高效率。否則,當只有15個左右的敵人被移動時,你的陣列可能會結束持有數千個敵人(需要更多時間穿過陣列)。 – meisenman 2014-09-05 18:58:34