【问题标题】:swift - detecting touch in a pathswift - 检测路径中的触摸
【发布时间】:2016-08-28 16:19:31
【问题描述】:

我的场景中有一些等距图块。有框架重叠,所以我使用了一个等距平铺形状的路径,并检查触摸是否在里面。

我的问题是我检测到触摸,但仅在路径上,而不是在路径内。我的路径或我使用它的方式一定有问题。

这是“touchesEnded”方法中的代码:

for actualTouch in touches
        {
            let SKnode = self.nodeAtPoint(actualTouch.locationInNode(self))
            let spriteNode = SKnode as! SKSpriteNode
            if spriteNode.name != nil{
                let tileSize = CGSizeMake(75, 38)
                let locationInNode = actualTouch.locationInNode(SKnode)

                let isometricPath = CGPathCreateMutable()
                CGPathMoveToPoint(isometricPath, nil, 0, -(tileSize.height / 2))
                CGPathAddLineToPoint(isometricPath, nil, (tileSize.width / 2), 0)
                CGPathAddLineToPoint(isometricPath, nil, 0, (tileSize.height / 2))
                CGPathAddLineToPoint(isometricPath, nil, -(tileSize.width / 2), 0)
                CGPathCloseSubpath(isometricPath)
                CGPathMoveToPoint(isometricPath, nil, 0, 0)
                let isometricPathRef = isometricPath as CGPathRef

                if CGPathContainsPoint(isometricPathRef, nil, locationInNode, true) == true
                {
                    print("touchedTile:\(spriteNode.name!)")
                    spriteNode.alpha = 1
                }

            }
        }

你能帮我看看我的错误吗? 谢谢。

【问题讨论】:

  • "let isometricPathRef = isometricPath as CGPathRef" 也许我无法将其转换为不透明的路径。或者:“CGPathContainsPoint(isometricPathRef, nil, locationInNode, true) == true”这个函数没有给我路径内的点..这两件事。错误必须存在,因为其余的工作正常

标签: swift core-graphics cgpath


【解决方案1】:

解决方案是遍历所有被触摸的节点,而不是“触摸”

let nodes = self.nodesAtPoint(touch.locationInNode(self))
        for node in nodes
        {

            let spriteNode = node as! SKSpriteNode
            if spriteNode.name != nil{
                let tileSize = CGSizeMake(75, 38)
                let locationInNode = touch.locationInNode(node)

                let isometricPath = CGPathCreateMutable()
                CGPathMoveToPoint(isometricPath, nil, 0, -(tileSize.height / 2))
                CGPathAddLineToPoint(isometricPath, nil, (tileSize.width / 2), 0)
                CGPathAddLineToPoint(isometricPath, nil, 0, (tileSize.height / 2))
                CGPathAddLineToPoint(isometricPath, nil, -(tileSize.width / 2), 0)
                CGPathCloseSubpath(isometricPath)
                let isometricPathRef = isometricPath as CGPathRef

                print("element")
                if CGPathContainsPoint(isometricPathRef, nil, locationInNode, true) == true
                {
                    print("touchedTile:\(spriteNode.name!)")
                    spriteNode.alpha = 1
                }

            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多