gpt4 book ai didi

swift - Xcode SpriteKit SKAction 不改变 SKSpriteNode 的属性?

转载 作者:可可西里 更新时间:2023-11-01 00:35:51 34 4
gpt4 key购买 nike

如果我创建一个SKSpriteNode,并在该节点上运行一个SKAction, Action 会在模拟器上运行,但节点的属性没有改变。例如。在以下示例中,在模拟器中,节点从 (50,50) 开始并在 1 秒内移动到 (0,0)。但是,末尾的 print 语句打印的是 (50,50) 而不是新位置 (0,0)

let test = SKSpriteNode()

test.position = CGPoint(x: 50.0, y: 50.0)
test.size = CGSize(width: 20.0, height: 20.0)
test.color = UIColor.black
addChild(test)

test.run(SKAction.move(to: CGPoint(x: 0, y: 0), duration: 1.0))
print("(\(test.position.x), \(test.position.y))")

我能想到的唯一原因是 print 语句在执行 SKAction 之前执行。如果是这种情况,那么稍后我该如何执行依赖于获取 SKSpriteNode 位置值的代码?

最佳答案

在 SpriteKit 中,SKAction 类有很多实例方法,但是你不需要添加更多代码,比如 SKAction.sequence 和另一个 SKAction 来打印你的位置。

事实上你有 run(_:completion) ,你可以找到官方文档 here那个

have a completion block called when the action completes

因此您的代码已按原样完成,只需添加如下示例的完成语法:

test.run(SKAction.move(to: CGPoint(x: 0, y: 0), duration: 1.0), completion:{ 
print("(\(test.position.x), \(test.position.y))")
})

关于swift - Xcode SpriteKit SKAction 不改变 SKSpriteNode 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42220245/

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