-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]]];
}
至於我明白你的問題,你想在縱向屏幕上方產卵的對象,與隨機放置在X軸上(Y軸是高度)。 您應該做的只是計算隨機X並將其設置爲Y yourScene.size.height。 –
嗯,我想到了,但我會如何改變這個代碼來做到這一點。是的,那正是我想要得到的。 –
剛用我的完整方法更新了我的答案。 –