我试图随机移动我的精灵(正方形),但所有精灵都走到同一个位置并“振动”。 什么是永久移动精灵和随机位置的最佳方法?
//Make random shape
func makeShape () {
//Check if have more than 12 shapes
if shapesamount <= 4 {
sprite = shape.copy() as! SKShapeNode
sprite.name = "Shpae \(shapesamount)"
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(move)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if istouching && !isOver {
let dt:CGFloat = 1.0/50.0
let distance = CGVector(dx: touchpoint.x - circle.position.x, dy: touchpoint.y - circle.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
self.circle.physicsBody!.velocity = velocity
}
if isOver == false {
for nodee in shapes.children {
moveRandom(node: nodee as! SKShapeNode)
}
}
}
}
形状是SKNode
答案 0 :(得分:0)
您不断向节点添加移动操作,这就是振动的原因。你确实让它们在每一帧都向各个方向移动。
相反,添加一个检查以查看是否有任何操作正在运行,如果没有,则更改位置:
if isOver == false {
for nodee in shapes.children {
guard !nodee.hasActions else {continue}
moveRandom(node: nodee as! SKShapeNode)
}
}