2014-02-05 63 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

我可否收到一封电子邮件,以便我们能够提供未来的问题? –