删除的精灵仍然可点击

时间:2017-03-05 14:01:43

标签: swift sprite-kit sprite

在我的游戏菜单中:

  1. 用户可以点击一个框。在第一阶段,这个框中只有一个“向导”精灵

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

  3. 我已经设置了这个,所以我有一个变量(beenClicked2)设置为0,当盒子在第1阶段时(或者当精灵精灵在其中时)

  4. 按下此框后,wasClicked2变量将更改为1,以便在第2阶段时不能再次单击该框并且不会崩溃。

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

  6. 图片显示: 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 个答案:

答案 0 :(得分: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或类似的东西,并在需要时实现委托。但那只是我。