SKSpriteNode通过触摸拖动运动 [英] SKSpriteNode drag movement by touch
问题描述
我正在尝试通过触摸屏幕在屏幕周围拖动SKSpirteNode.但是我希望能够不断移动Sprite,目前,我的代码仅将Sprite移动到触摸的位置,但是如果我按住并移动Sprite,则不会跟随.此外,我不想必须"触摸SKSpriteNode来激活运动,我不想触摸屏幕上的任何位置,并希望从该SKSpriteNode获得运动响应.
I'm trying to drag a SKSpirteNode around the screen by touching the screen. But I want to be able to do a constant movement of the Sprite, currently my code only moves the sprite to the location of my touch but if I hold and move the sprite will not follow. Moreover I don't want to "have" to touch the SKSpriteNode to activate the movement, I want to touch anywhere on the screen and to have a movement response from that SKSpriteNode.
这是我当前的代码:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
// SpriteNode I want to drag around
basket = SKSpriteNode(texture: basketTexture)
self.addChild(basket)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
var nodeTouched = SKNode()
var currentNodeTouched = SKNode()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
nodeTouched = self.nodeAtPoint(location)
basket.position = location
}
谢谢您的帮助.
推荐答案
我通过使用func touchesMoved而不是touchesBegan解决了这一问题,并且可以完美流畅地工作.这是最终代码:
I solved this by using the func touchesMoved instead of touchesBegan and works perfectly and smoothly. here is the final code:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
// SpriteNode I want to drag around
basket = SKSpriteNode(texture: basketTexture)
self.addChild(basket)
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
var nodeTouched = SKNode()
var currentNodeTouched = SKNode()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
nodeTouched = self.nodeAtPoint(location)
basket.position = location
}
这篇关于SKSpriteNode通过触摸拖动运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!