我试图通过触摸屏幕在屏幕上拖动SKSpirteNode。但是我希望能够不断移动Sprite,目前我的代码只将精灵移动到我的触摸位置但是如果我按住并移动精灵则不会跟随。此外,我不想"有"要触摸SKSpriteNode以激活移动,我想触摸屏幕上的任何位置并从该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
}
感谢任何帮助。
答案 0 :(得分:1)
我通过使用func touchesMoved而不是touchesBegan解决了这个问题,并且工作得非常顺畅。这是最终的代码:
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
}