[英]How would I move my image with a touch and hold on the left and right side of the screen? - Swift
I need help with the movement in Swift. 我在Swift的运动中需要帮助。 I have a
SKSpriteNode
and I need help with touch and drag movement left or right to move and the SKSpriteNode
. 我有一个
SKSpriteNode
,我需要有关左右滑动和SKSpriteNode
触摸和拖动运动的SKSpriteNode
。 But only the left and right. 但是只有左右。 When I click on the other side than the x position of the picture is to just run over smoothly.
当我单击图片的x位置以外的另一侧时,它将平滑地越过。 Could anyone help how to do it?
有人可以帮忙吗? Thanks.
谢谢。
import SpriteKit
class PlayScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
rocket.position = CGPoint(x: location.x, y: self.frame.height * 0.8)
}
}
In this, the rocket animates for 1 second to the location of the touch and then moves with the touch if the user moves their finger. 在这种情况下,火箭会在触摸位置动画1秒钟,然后如果用户移动手指,则随触摸一起移动。
class GameScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
rocket.position = location
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let moveAction = SKAction.moveTo(location, duration: 1/*Or as long as you want it to move*/)
rocket.runAction(moveAction)
}
}
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.