【问题标题】:Removed Sprite Still Clickable删除的精灵仍然可以点击
【发布时间】:2017-03-05 14:01:42
【问题描述】:

在我的游戏菜单中:

  1. 有一个用户可以单击的框。在第一阶段,这个盒子里只有一个“向导”精灵

  2. 当点击/单击框或图像时,向导将被移除并替换为 2 个按钮,一个用于返回,一个用于确认。

  3. 我已经设置好了,所以我有一个变量 (beenClicked2) 在框处于阶段 1 时设置为 0(或仅当向导精灵在其中时)

  4. 当框被按下时,beforeClicked2 变量变为 1,这样在第 2 阶段就不能再次点击框并且不会崩溃。

  5. 但是,当该框仅显示向导 (stage1) 时,来自第 2 阶段的后退按钮仍然是可点击的,这意味着该框不显示 2 个按钮而仅停留在第 1 阶段

图片显示: https://www.dropbox.com/s/st5fgv25fp3rz30/Image.png?dl=0

这是我的代码:

//PURCHASING
        if atPoint(location) == customBack2 || atPoint(location) == twoLivesWizard {
            if lock1 == 0 && beenClicked2 == 0 {
                twoLivesWizard.removeFromParent()
                locked.removeFromParent()
                self.addChild(purchaseText1)
                self.addChild(purchaseTick1)
                self.addChild(purchaseBack1)

                beenClicked2 = 1
                print("\(beenClicked2)")
            }
        }

//Cancel Purchase
        if atPoint(location) == purchaseBack1 {
            beenClicked2 = 0
            self.addChild(locked)
            self.addChild(twoLivesWizard)
            purchaseText1.removeFromParent()
            purchaseTick1.removeFromParent()
            purchaseBack1.removeFromParent()

            print("\(beenClicked2)")
        }

【问题讨论】:

  • 这里缺少信息。 1)哪个精灵仍然可以点击(你在标题中提到过)? 2)尝试定义“点击错误的地方”? 3) 你说的 box 是什么?发布问题时尽量准确。帮助人们帮助你。
  • @Whirlwind 嗨,很抱歉不清楚。我已经更新了这个问题。希望这会有所帮助!

标签: swift sprite-kit sprite


【解决方案1】:

这是一个简单可行的例子,它可能会给你一个线索,你可能会朝哪个方向前进:

import SpriteKit

class GameScene: SKScene {

    private let box = SKSpriteNode(color: .yellow, size: CGSize(width: 200, height: 300))

    private let wizard = SKSpriteNode(color: .purple, size: CGSize(width: 150, height: 250))
    private let back = SKSpriteNode(color: .gray, size: CGSize(width: 75, height: 75))
    private let confirm = SKSpriteNode(color: .lightGray, size: CGSize(width: 75, height: 75))

    private var stage = 0

    override func didMove(to view: SKView) {

        addChild(box)
        box.addChild(wizard)
        wizard.zPosition = 1
        back.zPosition = 1
        confirm.zPosition = 1
        back.position.x = -50
        confirm.position.x = 50
    }

    private func toggleStage(){

        if stage == 0 {

            wizard.removeFromParent()
            box.addChild(confirm)
            box.addChild(back)
            stage = 1
        }else{

            confirm.removeFromParent()
            back.removeFromParent()
            box.addChild(wizard)
            stage = 0
        }
    }

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

        if let touch = touches.first {

            let location = touch.location(in: self)

            if stage == 0 {

                if atPoint(location) == wizard {
                     toggleStage()
                }
            }else{
                if atPoint(location) == back {
                    print("Back button tapped")
                    toggleStage()
                }else if atPoint(location) == confirm {
                    print("Confirm button tapped")
                }
            }
        }
    }
}

基本上,您需要做的是根据舞台交换精灵。就个人而言,我会创建自己的名为 Menu 和 Button 的类或类似的类,并在需要时实现委托。但这只是我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多