-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。 –
嗯,我想到了,但我会如何改变这个代码来做到这一点。是的,那正是我想要得到的。 –
刚用我的完整方法更新了我的答案。 –