[英]How to move a sprite using swipe gesture?
How can i move a sprite to the left by swiping to the left, move a sprite to the right by swiping to the right and so on? 如何通过向左滑动将精灵向左移动,向右滑动以将精灵向右移动,依此类推?
Like if I swipe to the left, my sprite moves 20 points to the left..I am using SpriteKit and Swift. 就像我向左滑动一样,我的精灵会向左移动20点。.我正在使用SpriteKit和Swift。
Thank You! 谢谢! @jonogilmour
@jonogilmour
I have resolved it in this way: 我已经以这种方式解决了:
let right = SKAction.moveByX(64, y: 0, duration: 0.6)
let left = SKAction.moveByX(-64, y: 0, duration: 0.6)
let up = SKAction.moveByX(0, y: 64, duration: 0.6)
let down = SKAction.moveByX(0, y: -64, duration: 0.6)
func swipedRight(sender:UISwipeGestureRecognizer){
player.runAction(right)
}
func swipedLeft(sender:UISwipeGestureRecognizer){
player.runAction(left)
}
func swipedUp(sender:UISwipeGestureRecognizer){
player.runAction(up)
}
func swipedDown(sender:UISwipeGestureRecognizer){
player.runAction(down)
}
override func didMoveToView(view: SKView) {
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
............
For swipes, you can use UIGestureRecognizer
, specifically UISwipeGestureRecognizer . 对于滑动,可以使用
UIGestureRecognizer
,特别是UISwipeGestureRecognizer 。 These are well documented so a simple Google search will give you what you need. 这些都有据可查,因此简单的Google搜索将为您提供所需的信息。 Then, you will need to use either
applyForce()
or applyImpulse()
on your sprite. 然后,您将需要在Sprite上使用
applyForce()
或applyImpulse()
。 Again, these are well documented and trivial to implement. 同样,这些都有充分的文档记录,并且实现起来很简单。
The difference between the two is that an impulse is like hitting a ball with a bat; 两者之间的区别在于,冲动就像用蝙蝠击打球一样。 the impulse is applied once to the object and tapers off over time.
脉冲一次施加到对象上,并随着时间逐渐减小。 A force is applied constantly over time, like a car accelerating.
随着时间的推移,不断施加力,就像汽车在加速。 In this case you could apply an impulse on the swipe and be done with it, or you could apply a force and continue applying it whilst
x_distance < 20
in your update()
method. 在这种情况下,您可以在滑动上施加一个脉冲并完成该操作,或者可以在
update()
方法中x_distance < 20
施加一个力并继续施加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.