我有一个游戏,我似乎无法让节点在x位置随机产生,它会随机产生,但通常会离开屏幕,然后游戏无用而且必须重新启动它:
func addEnemy () {
//enemy
let minValue = self.size.width / 8;
let maxValue = self.size.width-20;
let spawnPoint = UInt32(maxValue - minValue);
Enemy = SKSpriteNode(imageNamed: "Enemy")
Enemy.size = CGSize(width: 150, height: 200)
Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(spawnPoint)), y: self.size.height)
self.addChild(Enemy)
}
答案 0 :(得分:1)
let min: CGFloat = 15.0
let max: CGFloat = 200.0
let randomCGFloatBetweenMinAndMax = CGFloat(rand())/CGFloat(RAND_MAX)*(max-min)+min
或者如果您愿意
let randomCGFloatBetweenMinAndMax2 = CGFloat(arc4random_uniform(UInt32(max-min)) + UInt32(min))