【问题标题】:Increase Sprite ContainsPoint Size增加 Sprite ContainsPoint 大小
【发布时间】:2016-02-23 05:44:06
【问题描述】:

我有一个尺寸减小到 0.8 的精灵。当我使用 sprite.ContainsPoint(touchLocation) 时,触摸只在原始大小精灵的 0.8 范围内注册(这是有道理的)。然而,即使精灵已经缩小,我仍然希望触摸注册,就好像精灵仍然是全尺寸(1.0)。以下是我当前的代码:

var button: SKSpriteNode!

func createButton() {

    button = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 100))
    addChild(button)
    button.runAction(SKAction.scaleTo(0.8, duration: 1))

}

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

    let touch = touches.first as UITouch!
    let touchLocation = touch.locationInNode(self)

    if button.containsPoint(touchLocation) {

        print("touching within the button")

    }

}

我尝试过哪些不起作用,但您可能会看到我正在尝试做的事情:

var button: SKSpriteNode!

func createButton() {

    button = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 100))
    addChild(button)
    button.runAction(SKAction.scaleTo(0.8, duration: 1))

}

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

    let touch = touches.first as UITouch!
    let touchLocation = touch.locationInNode(self)

    let myButton = CGRectMake(button.position.x, button.position.y, button.size.width / 0.8, button.size.width / 0.8)
    //// the button.size / 0.8 should bring the size back to 1.0

    if myButton.containsPoint(touchLocation) {

        print("touching within the button")

    }

}

任何帮助都会很棒!

【问题讨论】:

    标签: swift sprite-kit sprite contains touches


    【解决方案1】:

    您可以添加另一个SKSpriteNode 作为按钮的子元素,作为按钮的实际精灵,而按钮本身没有纹理或颜色。这样,您可以只减少子精灵节点,并在按钮本身内进行命中测试。

    例如:

    var button: SKSpriteNode!
    
    func createButton() {
        button = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(100, 100))
        addChild(button)
    
        buttonSprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 100))
        button.addChild(buttonSprite)
        buttonSprite.runAction(SKAction.scaleTo(0.8, duration: 1))
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first as UITouch!
        let touchLocation = touch.locationInNode(self)
    
        if button.containsPoint(touchLocation) {
            print("touching within the button")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 2016-04-23
      • 2020-12-02
      • 2014-10-24
      • 2012-12-07
      • 2013-01-26
      • 2014-05-13
      • 2015-11-22
      • 2015-12-08
      相关资源
      最近更新 更多