2014-02-05 64 views
-2

所以我在我的SpriteKit遊戲中使用了這段代碼來產生一個圖像,但是我希望它不是來自左側(橫向),而是來自縱向的頂部(我的場景已經是縱向) 。將此更改爲從X軸產生?

謝謝。

- (void)addBalloon { 

// Create sprite 
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:@"balloon"]; 

// Determine where to spawn the monster along the Y axis 
int minY = monster.size.height/2; 
int maxY = self.frame.size.height - monster.size.height/2; 
int rangeY = maxY - minY; 
int actualY = (arc4random() % rangeY) + minY; 

// Create the monster slightly off-screen along the right edge, 
// and along a random position along the Y axis as calculated above 
monster.position = CGPointMake(self.frame.size.width + monster.size.width/2, actualY); 
[self addChild:monster]; 

// Determine speed of the monster 
int minDuration = 2.0; 
int maxDuration = 4.0; 
int rangeDuration = maxDuration - minDuration; 
int actualDuration = (arc4random() % rangeDuration) + minDuration; 

// Create the actions 
SKAction * actionMove = [SKAction moveTo:CGPointMake(-monster.size.width/2, actualY) duration:actualDuration]; 
SKAction * actionMoveDone = [SKAction removeFromParent]; 
[monster runAction:[SKAction sequence:@[actionMove, actionMoveDone]]]; 

} 
+0

至於我明白你的問題,你想在縱向屏幕上方產卵的對象,與隨機放置在X軸上(Y軸是高度)。 您應該做的只是計算隨機X並將其設置爲Y yourScene.size.height。 –

+0

嗯,我想到了,但我會如何改變這個代碼來做到這一點。是的,那正是我想要得到的。 –

+0

剛用我的完整方法更新了我的答案。 –

回答

0

只需用x代替Y和高度而不是寬度,反之亦然

- (void)addBalloon { 

      // Create sprite 
      SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:@"balloon"]; 

      // Determine where to spawn the monster along the X axis 
      int minX = monster.size.width/2; 
      int maxX = self.frame.size.width - monster.size.width/2; 
      int rangeX = maxX - minX; 
      int actualX = (arc4random_uniform(rangeX)) + minX; 

      // Create the monster slightly off-screen along the top, 
      // and along a random position along the X axis as calculated above 
      monster.position = CGPointMake(actualX, self.frame.size.height + monster.size.height/2); 
      [self addChild:monster]; 

      // Determine speed of the monster 
      int minDuration = 2.0; 
      int maxDuration = 4.0; 
      int rangeDuration = maxDuration - minDuration; 
      int actualDuration = (arc4random() % rangeDuration) + minDuration; 

      // Create the actions 
      SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -monster.size.height/2) duration:actualDuration]; 
      SKAction * actionMoveDone = [SKAction removeFromParent]; 
      [monster runAction:[SKAction sequence:@[actionMove, actionMoveDone]]]; 

    } 
+0

我可否收到一封電子郵件,以便我們能夠提供未來的問題? –