【发布时间】:2017-05-12 12:19:20
【问题描述】:
我试图移动精灵在屏幕上随机移动,但精灵移动一次到随机位置并停止移动
我在这里调用以使形状与计时器一起使用
//Make shape Timer
func makeshapetimer () {
maketimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(makerandomShape), userInfo: nil, repeats: true)
}
//Make random shape
func makerandomShape () {
//Check if have more than 12 shapes
if shapesamount <= 12 {
let sprite = shape.copy() as! SKShapeNode
sprite.name = "Shpae \(shapesamount)"
sprite.position = CGPoint(x: frame.minX - sprite.frame.width, y: frame.maxY)
shapes.addChild(sprite)
shapesamount += 1
moveRandom(node: sprite)
}
}
在这里我做了一个随机的位置动作并永远重复这个动作,但每个形状只运行一次
//Move shape radmonly
func moveRandom (node: SKShapeNode) {
move = SKAction.move(to: CGPoint(x: CGFloat.random(min: frame.minX, max: frame.maxX), y: CGFloat.random(min: frame.minY, max: frame.maxY)), duration: shapespeed)
node.run(SKAction.repeatForever(move))
}
【问题讨论】:
-
不要在 sprite-kit 中使用
Timer- 改用 SKAction。计时器不会遵守 sprite-kit 游戏循环,不会;如果场景暂停等,则不会停止等 -
SKAction.wait 代替计时器
标签: ios random sprite-kit action