ios - 如何在 Swift Spritekit 中向我的节点添加触摸和按住手势?

标签 ios xcode swift touch

我有这个游戏,我的节点在屏幕中间,如果我按住屏幕的左侧,节点将向左移动,如果我按住屏幕的右侧,节点将移动到正确的。我尝试了一切,但似乎无法让它发挥作用。谢谢! (我有一些代码,但它没有做我想让它做的事情。如果你想看它和它做了什么,我会把它贴出来。)

编辑代码:

    var location = touch.locationInNode(self)

    if location.x < self.size.width/2 {
        // left code
        let moveTOLeft = SKAction.moveByX(-300, y: 0, duration: 0.6)
        hero.runAction(moveTOLeft)
    }
    else {
        // right code
        let moveTORight = SKAction.moveByX(300, y: 0, duration: 0.6)
        hero.runAction(moveTORight)


    }

最佳答案

您必须在每次更新中检查触摸的位置,以确定您希望角色移动的方向。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch = touches.first as! UITouch
    var point = touch.locationInView(self)
    touchXPosition = point.x
    touchingScreen = true
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesEnded(touches, withEvent: event)
    touchingScreen = false
}

override func update(currentTime: CFTimeInterval) {
    if touchingScreen {
        if touchXPosition > CGRectGetMidX(self.frame) {
            // move character to the right.
        }
        else {
            // move character to the left. 
        }
    }
    else { // if no touches.
        // move character back to middle of screen. Or just do nothing.
    }
}

关于ios - 如何在 Swift Spritekit 中向我的节点添加触摸和按住手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956893/

相关文章:

ios - swift 中的循环进度

ios - iphone:尝试将图像导出为电影时,电影已损坏

xcode - 我可以找出主动登录的 GameCenter 用户名吗?

ios - 如何更改 SwiftUI 中 NavigationView 的背景颜色?

ios - ScrollView 内的 UITableView 无限滚动

ios - CAKeyframeAnimation : contents change back to first image when stopped

iPhone/iPad 应用程序到网络服务。使用什么样的授权???基本的?验证?

iphone - objective-c : setting an object attribute ; attribute not updating

iphone - UIPopOverController的UIBarButton函数的错误- objective-c

swift - 在绘图应用程序swift中合并 ImageView