在直线的SKShapeNode上检测触摸 [英] Detecting Touch on SKShapeNode that is a Line

查看:103
本文介绍了在直线的SKShapeNode上检测触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个SKShapeNode并为其指定了CGPath.这是在我的GameScene.swift中的didMoveToView中:

I have an SKShapeNode that I have created and given a CGPath to. This is in my GameScene.swift in didMoveToView:

let myNode = SKShapeNode()
let lineToDraw = CGPathCreateMutable()
CGPathMoveToPoint(lineToDraw, nil, 0, 0)
CGPathAddLineToPoint(lineToDraw, nil, 87, 120)
myNode.path = lineToDraw
myNode.strokeColor = SKColor.redColor()
myNode.lineWidth = 20
myNode.name = "My Node!"
world.addChild(myNode) // World is a higher-level node in my scene

这又是我在场景中检测触摸的方式:

And this is how I'm detecting touches, again in the Scene:

override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
    if let touch = touches.anyObject() as? UITouch {
        if let shapeNode = nodeAtPoint(touch.locationInNode(self)) as? SKShapeNode {
            println("Touched \(shapeNode.name)")
        } else {
            println("Nothing here")
        }
    }
}

该行节点显示没有问题.但是,它根本没有注册touchesEnded.

The line node is showing up with no issues. However, it is not registering touchesEnded at all.

真的,我有三个疑问与此无关:

Really, I have three questions nebulous to this:

  1. 是否有更好的方法来创建在两点之间的直线的SKShapeNode而不调用便捷初始化器?我计划对SKShapeNode进行子类化,以向该节点添加其他逻辑,并且该子类无权访问超类的便捷初始化程序.

  1. Is there a better way to create an SKShapeNode that is a line between two points without calling a convenience initializer? I plan on subclassing SKShapeNode to add additional logic to the node, and the subclass doesn't have access to convenience initializers of the superclass.

更重要的是,如何让场景识别那里的线节点并触发touchesEnded?

More importantly, how do I get the scene to recognize my line node there and trigger touchesEnded?

有没有办法使用这种机制,使它更加模糊",因此它可以处理靠近线条而不是线条的触摸?我最终的计划是使其更薄,但我仍然希望有一个较大的触摸区.我知道如果没有别的,我可以创建两个节点:一个透明/厚节点,另一个红色/薄节点,并在透明节点上处理触摸事件,但是我想知道是否有更好的方法.

Is there a way, using that mechanism, I can make it a bit more "fuzzy", so it handles touches close to the line, instead of on the line? My eventual plan is to make it thinner, but I'd still like to have a large touch zone. I figure if nothing else, I can create two nodes: one clear/thick and the other red/thin, and handle touch events on the clear one, but I would like to know if there's a better way.

推荐答案

在回答您的主要问题时,touchesEnded方法存在问题. apple显示的模板建议以下布局:

In answer to your main question, there is a problem in your touchesEnded method. The template shown by apple recommends the following layout:

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
    }
}

然后您可以使用此方法查看location值是否在行框架内(为此,我在didMoveToView外部创建了"myNode"变量,以便可以从其余部分访问它功能):

You can then use this method to see if the location value is inside the lines frame (in order to do this, i created the "myNode" variable outside of the didMoveToView so that i could access it from the rest of the functions):

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(world)

            if CGRectContainsPoint(myNode.frame, location) {
                println("Touched \(myNode.name)")

            } else {

                println("Nothing here")
        }

    }
}

对于模糊"行,您可以简单地检查较大的CGRect.在上面的代码中,我检查了myNode.frame,但是您可以创建一个CGRect变量,该变量比该行稍大,以便您可以检测到没有直接击中它的触摸.

As for a "fuzzier" line, you can simply check a larger CGRect. In the code above, i check myNode.frame but you could create a variable with a CGRect which is slightly larger than the line so that you can detect touches which don't directly hit it.

对于更简洁的代码,我目前无法想到任何东西,但这并不是说没有办法.但是,我不太了解您对子类无法访问便捷方法的含义,因为只要您正确导入,子类就可以访问超类所做的任何事情.

As for more concise code, i cannot think of any at the moment but that isn't to say that there isn't a way. However, I don't quite understand what you mean about subclasses not having access to convenience methods as they can have access to whatever the superclass does so long as you import correctly.

我希望这会有所帮助.

这篇关于在直线的SKShapeNode上检测触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆