首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SKScene + TouchBegan Bug

SKScene + TouchBegan Bug
EN

Stack Overflow用户
提问于 2015-08-26 20:18:32
回答 1查看 81关注 0票数 0

我的SKScene有一个空节点(buttonsGroup)。在我的GameView类中,我创建了一个节点(ball1),它是空节点的父节点。

代码语言:javascript
复制
    buttonsGroup = childNodeWithName("ball1")


    var ball1 = SKSpriteNode(imageNamed: "ball")
    ball1.name == "ball1"
    buttonsGroup.addChild(ball1)

当我试图使用TouchesBegan移动节点时,我的问题出现了:

代码语言:javascript
复制
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch in (touches as! Set<UITouch>) {

        let location = touch.locationInNode(self)
        let touchNode = nodeAtPoint(location)

        if touchNode.name != "ground" {
            touchNode.position = location
        }
    }
} 

我已经知道,如果使用SKScene和touchNode位置,它们将返回不同的值(至少看起来是这样)。在这种情况下,节点就会跳到任何地方。我的SKScene的尺寸是480x320。

有人知道解决办法吗?如果我用手做(没有SKScene),它没有任何问题。在尝试使用moveBy操作或任何其他需要我在视图中的位置的操作时,我已经多次遇到了这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-26 21:24:59

如果您试图移动球,您将需要将触摸位置从场景坐标转换为使用转换点:toNode转换点:从节点的球的父坐标。

例如,

代码语言:javascript
复制
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let node = nodeAtPoint(location)
        if (node.name != "ground") {
            let locationInNode = convertPoint(location, toNode:node.parent!)
            node.position = locationInNode
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32235959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档