精灵的随机运动

时间:2015-02-15 20:54:47

标签: swift random sprite-kit skspritenode skaction

我是新来的,所以如果我错误地输入了我的问题,我会道歉。也就是说,我有一个问题让我的精灵移动到屏幕周围的随机位置。这是我的代码

func random() ->CGFloat{
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat, max:CGFloat) ->CGFloat{
    return random()*(max-min)+min
}

dino.position = CGPoint(x: size.width + dino.size.width/2, y: actualY)
    // Add the monster to the scene
    addChild(dino)
    // Determine speed of the monster
    let actualDuration = random(min: CGFloat(3.0), max: CGFloat(4.0))
    let randomNum = CGPoint(x:Int (arc4random()%1000), y:Int (arc4random()%1000))
    // Create the actions
    var actionMove = SKAction:CGPoint(CGPoint(x:randomNum, y:randomNum)), duration:NSTimeInterval;(actualDuration)
    let actionMoveDone = SKAction.removeFromParent()
    dino.runAction(SKAction.sequence([actionMove, actionMoveDone]))

谢谢你的帮助,此时有什么帮助

1 个答案:

答案 0 :(得分:0)

您使用的功能无效。请改用SKAction.moveTo

dino.position = CGPoint(x: size.width + dino.size.width/2, y: 10)
// Add the monster to the scene
addChild(dino)
// Determine speed of the monster
let actualDuration = NSTimeInterval(random(min: CGFloat(3.0), max: CGFloat(4.0)))
let randomNum = CGPoint(x:Int (arc4random() % 1000), y:Int (arc4random() % 1000))

var actionMove =  SKAction.moveTo(randomNum, duration: actualDuration) // Changed line

let actionMoveDone = SKAction.removeFromParent()
dino.runAction(SKAction.sequence([actionMove, actionMoveDone]))