为什么 SKNode 在 SpriteKit 游戏中对触摸没有反应? [英] Why SKNode isn't reacting to touch in SpriteKit game?

查看:26
本文介绍了为什么 SKNode 在 SpriteKit 游戏中对触摸没有反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在 SpriteKit 游戏中暂停,我做了一个暂停按钮.当我想制作另一个场景以暂停并响应触摸正在过渡到一个场景时,它就像一个魅力.然后我的函数看起来 像这样(PS pause = 暂停按钮)但是它还有其他问题,我决定在我的主场景中暂停:playscene所以我添加了一个 pauseBackgroundresumeButton 附加到暂停背景并隐藏它(声明为 resumeButton.hidden = true)现在我的函数看起来像这样:

In order to make a pause in a SpriteKit game I made a pause button. It worker like a charm when I wanted to make an another scene for pause and respond to touch was transitioning to a scene. My function then looked like this (P.S. pause = pause button) But there were other problems with it and I decided to make pause within my main scene: playscene So I added a pauseBackground and resumeButton that is attached to pause background and hided it (declared resumeButton.hidden = true) Now my function looks like this:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    if self.onGround {
        self.speedY = -20.0
        self.onGround = false
    }
    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
        self.scene?.view?.paused = true
        self.pauseBackground.hidden = false
        self.resumeButton.hidden = false
        if self.nodeAtPoint(location) == self.resumeButton {
        self.pauseBackground.hidden = true
        self.resumeButton.hidden = true
        self.scene?.view?.paused = false
            }
        }
    }
}

这很有趣,因为它适用于场景转换,但现在 Xcode 无法检测或忽略我在暂停按钮位置的点击.我真的不知道为什么.请你帮助我好吗?更新:我试过 switch 语句:

It's interesting because it worked with scene transitions, but now Xcode can't detect or ignore my tap in pause button location. I don't really know why. Could you please help me? Update: I tried switch statement:

for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        switch self.nodeAtPoint(location) {
        case self.pause:
        self.scene?.view?.paused = true
        self.pauseBackground.hidden = false
        self.resumeButton.hidden = false
        case self.resumeButton:
        self.pauseBackground.hidden = true
        self.resumeButton.hidden = true
        self.scene?.view?.paused = false
        default:
        println()

    }
}

遗憾的是,它也不起作用!我真的不知道为什么它没有检测到该位置的触摸.

Sadly, it's not working either! I really have no idea why it's not detecting a touch in that location.

推荐答案

  1. 为暂停按钮命名.通过打印触摸节点的名称来控制是否在 touchesBegan 中触摸了暂停按钮.

  1. Give a name to your pause button. Control if your pause button is touched in touchesBegan by printing the name of the touched node.

控制触摸节点的zPosition和暂停按钮.将暂停按钮的 zPosition 设置为 1000 并尝试触摸它.

Control the zPosition of the touched node and the pause button. Set the zPosition of the pause button = 1000 and try touching it.

假设节点 node1 是 pauseButton 的父节点.

Let's say, the node node1 is the parent node of the pauseButton.

node1.zPosition = 100.0f;
pauseButton.zPosition = 200.0f;

continueButton 位于场景上方

the continueButton is above the scene

continueButton.zPosition = 250.0f;

这意味着,pauseButton 的 zPosition 比父节点 node1 高 200.continueButton 的 zPosition (250) 小于场景上方 pauseButton 的 zPosition,尽管 zPosition 似乎更小 (200).这是因为 pauseButton 的 zPosition 在场景上方 300.父节点的 zPosition 对于控制其子节点和场景其他节点的 zPosition 非常重要.

That means, that the zPosition of the pauseButton is 200 above the parent node node1. The zPosition of the continueButton (250) is less than the zPosition of the pauseButton above the scene, although the zPosition seems to be less (200). That's because the zPosition of the pauseButton is 300 above the scene. The zPosition of the parent node is very important to control the zPositions of its children and the other nodes of the scene.

节点的 zPosition 总是相对于它的父节点.您的两个按钮(暂停和恢复按钮)是否具有相同的父节点?控制触摸节点的最佳方法是给每个节点一个名称和一个 zPosition,并在 touchesBegan 中打印这个(name 和 zPosition with NSLog()/println()).有了这个,您就可以确切地知道哪个节点被触摸了,并且您知道了它的 zPosition.因此,您可以更正每个节点的 zPosition,使其按您的需要工作.

zPosition of a node is always relative to its parent node. Have your both buttons (pause and resume button) the same parent node? The best way to control the touched node, is to give each node a name and a zPosition, and to print this (name and zPosition with NSLog() / println()) in the touchesBegan. With this you know exactly which node is touched and you know its zPosition. And so you can correct the zPosition of each node, so that it works as you need.

这篇关于为什么 SKNode 在 SpriteKit 游戏中对触摸没有反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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