ios - 检测 Sprite 纹理内的触摸而不是整个框架 iOS Swift SpriteKit?

标签 ios swift sprite-kit skspritenode

我现在正在使用 SpriteKit,我遇到了一个看似简单但在互联网上找不到任何问题的问题。我有三个形状像平行四边形的按钮,它们堆叠在一起,看起来像这样: Screenshot of buttons

let button = SKSpriteNode(imageNamed: "playbutton")
let leaderButton = SKSpriteNode(imageNamed: "leaderbutton")
let homeButton = SKSpriteNode(imageNamed: "homebutton")

    button.position = CGPoint(x: size.width/2, y: size.height/2)
    addChild(button)

    leaderButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height/2)
    addChild(leaderButton)

    homeButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height)
    addChild(homeButton)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if button.containsPoint(location) {
            button.runAction(SKAction.scaleTo(0.8, duration: 0.1))
        }
        else if leaderButton.containsPoint(location) {
            leaderButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
        }
        else if homeButton.containsPoint(location) {
            homeButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
        }
    }
}

这就是我检测触摸的方式。问题是它们重叠,因为 Sprite 实际上是一个矩形,所以当我尝试点击第二个按钮的左上角时,顶部按钮会检测到它。我想知道有一种方法可以只在纹理中检测触摸,比如如何将物理体设置为纹理。 感谢您能给我的任何帮助!

链接现在有效。

所以我尝试了这个:

    button.position = CGPoint(x: size.width/2, y: size.height/2)
    button.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "playbutton"), size: button.size)
    button.physicsBody?.dynamic = false
    addChild(button)

    leaderButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height/2)
    leaderButton.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "leaderbutton"), size: leaderButton.size)
    leaderButton.physicsBody?.dynamic = false
    addChild(leaderButton)

    homeButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height)
    homeButton.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "homebutton"), size: homeButton.size)
    homeButton.physicsBody?.dynamic = false
    addChild(homeButton)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if physicsWorld.bodyAtPoint(location)?.node == button {
            button.runAction(SKAction.scaleTo(0.8, duration: 0.1))
            print("play")
        }
        if physicsWorld.bodyAtPoint(location)?.node == leaderButton {
            leaderButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
            print("leader")
        }
        if physicsWorld.bodyAtPoint(location)?.node == homeButton {
            homeButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
        }
    }
}

它仍然记录了完整的帧,而不仅仅是物理体。查看链接以查看按钮以及它们的坐标如何相交。

最佳答案

如果您将一个物理体附加到每个按钮,您可以检测到您的触摸落在哪个物理体上。

您可以使用 SKPhysicsBody(texture:size:)SKPhysicsBody(texture :alphaThreshold:size:),或者您可以创建描述按钮形状的 CGPath 并使用 SKPhysicsBody(polygonFromPath:)。将主体分配给按钮的 physicsBody 属性。假设您实际上不希望物理模拟器移动您的按钮,请将每个主体的 dynamic 属性设置为 false

然后您可以使用 physicsWorld.bodyAtPoint(_:) 获取触摸落在的物体之一(其中 physicsWorld 是您的 的属性SKScene).使用正文的 node 属性返回到按钮节点。如果物体重叠,bodyAtPoint 返回任意物体。

如果您需要触摸所触及的所有物体,您可以使用 physicsWorld.enumerateBodiesAtPoint(_:usingBlock:)

如果您可以创建描述按钮形状的 CGPath,则完全不同的方法是使用 SKScene.convertPointFromView(_:),然后使用 SKNode。 convertPoint(_:fromNode:_) 将点转换成按钮的坐标系,然后使用CGPathContainsPoint(全局函数)检测点是否在描述按钮的路径中形状。

关于ios - 检测 Sprite 纹理内的触摸而不是整个框架 iOS Swift SpriteKit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33007014/

相关文章:

ios - 我如何提供 Stripe 客户的卡片列表?

ios - 将项目添加到 Storyboard-IB 中的原型(prototype)单元格时出现奇怪错误

ios - 如何从照片库加载动画 GIF

swift - 'AnyObject ?' does not have a member named ' 计数'

swift - 将 SKSpriteNode 移动到触摸方向

ios - 单击文本字段后按钮移动

java - 通过通用代码向各种设备推送通知

ios - FIRInstanceID 在 FIRInstanceIDAPNSTokenType.Sandbox 中为零

swift - 基于播放器的滚动视差背景

swift - 我在哪里可以编写持续运行的代码?