无法选择我添加到屏幕的暂停按钮

时间:2023-01-22 22:35:02

My game is setup with a background, a road in the middle of it, then a bar at the top which displays the score and pause button. zPositon of the road and background are 1. The bar at the top zPosition is 4, and the pause button zPosition is 4 as well.

我的游戏设置有背景,中间有一条道路,然后是顶部的一个栏,显示得分和暂停按钮。道路和背景的zPositon是1.顶部zPosition的条形图是4,暂停按钮zPosition也是4。

Code for road and background.

道路和背景的代码。

    road.position = CGPoint(x: CGFloat(0), y: CGFloat(0))
    road.zPosition = 1
    road.name = roadName
    background.position = CGPoint(x: CGFloat(0), y: CGFloat(0))
    background.zPosition = 1
    background.name = backgroundName

    addChild(background)
    addChild(road)

Code for the bar at the top.

顶部栏的代码。

    topBar = SKSpriteNode(color: .black, size: CGSize(width: screenWidth, height: CGFloat(150)))
    topBar.position = CGPoint(x: 0, y: (screenHeight/2) + topBar.size.height/2)
    topBar.zPosition = 4
    topBar.name = topBarName

    addChild(topBar)

Code for the pause button.

暂停按钮的代码。

    pauseButton.position = CGPoint(x: (-topBar.size.width/2) + (pauseButton.size.width + (pauseButton.size.width/2)), y: 0)
    pauseButton.zPosition = 4
    pauseButton.name = pauseButtonName

    topBar.addChild(pauseButton)

In my touches began function, I attempted to search for the nodes at the position of the touch, but it only returns the names of the road and background. It doesn't even pick up the topBar or pause button.

在我的触摸开始功能中,我试图在触摸位置搜索节点,但它只返回道路和背景的名称。它甚至没有拿起topBar或暂停按钮。

for node in nodes
            {
                print(node.name)

                if node.name == pauseButtonName
                {
                    print("paused")
                }
            }

This will output

这将输出

Optional("Road")
Optional("Background")

I also tried this method, but "paused" was never returned.

我也试过这种方法,但“暂停”从未返回过。

 if pauseButton.contains(touch.location(in: self))
            {
                print("paused")
            }

I think it may be important to note that the top bar is positioned off the screen initially. When you start the game, it runs an SKAction to move the bar on screen.

我认为重要的是要注意顶部条最初位于屏幕之外。当你开始游戏时,它运行一个SKAction来移动屏幕上的栏。

topBar.run(SKAction.moveTo(y: (screenHeight/2) - (topBar.size.height/2), duration: 0.5))

Why am I not able to select the pause button, and how can this be resolved?

为什么我无法选择暂停按钮,如何解决?

1 个解决方案

#1


0  

I resolved the issue by using this code. Instead of checking for the location in the game scene, I checked for it in the topBar.

我使用此代码解决了该问题。我没有检查游戏场景中的位置,而是在topBar中检查了它。

if pauseButton.contains(touch.location(in: topBar))
{
    print("paused")
}

#1


0  

I resolved the issue by using this code. Instead of checking for the location in the game scene, I checked for it in the topBar.

我使用此代码解决了该问题。我没有检查游戏场景中的位置,而是在topBar中检查了它。

if pauseButton.contains(touch.location(in: topBar))
{
    print("paused")
}