gpt4 book ai didi

swift - 在屏幕上以随机位置永远移动 Sprite

转载 作者:行者123 更新时间:2023-11-30 12:21:38 24 4
gpt4 key购买 nike

我尝试随机移动我的 Sprite (方 block ),但所有 Sprite 都转到相同的位置并“振动”。以随机位置永久移动 Sprite 的最佳方法是什么?

//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

最佳答案

您不断向节点添加移动操作,这就是它振动的原因。您实际上是在每一帧都让它们向各个方向移动。

相反,添加检查以查看是否有任何操作正在运行,如果没有,则更改位置:

  if isOver == false {
for nodee in shapes.children {
guard !nodee.hasActions else {continue}
moveRandom(node: nodee as! SKShapeNode)
}
}

关于swift - 在屏幕上以随机位置永远移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44722970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com