在Xcode中,单击时如何使SkSpriteNode向上移动



单击时如何使SkSpriteNode向上移动?-感谢

https://i.stack.imgur.com/fcOvr.png

假设您的节点称为box

使用触摸开始功能

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches{
let touch:UITouch = touches.first!
let touchLocation = touch.location(in: self)
box.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
box.physicsBody?.applyImpulse(CGVector(dx: 0, dy:300))
}
}

applyImpusle方法取x值沿x轴(向左或向右(移动,取y值沿y轴(向上或向下(移动。相应地设置dy。

剩下的取决于你的场景属性,比如重力、摩擦力、盒子的质量等等

在不使用physicsBody的情况下,您可以使用此代码,只需更改向上移动的方式(我手动操作只是为了保持简单(。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)

if sprite.contains(location){

sprite.position = CGPoint(x: 0, y: 10)
}

最新更新