0

我在我的场景/视图中设置了一个 UIButton,当 ViewController 调用场景时,该按钮会显示。问题是当我单击按钮并调用我的 GameScene 时,按钮仍然存在。我想我以错误的方式设置了按钮。
我想问题是我不能removeFromSuperview()在我的函数中调用 button1 上的startGame函数。

我怎样才能解决这个问题?任何帮助表示赞赏!

import Foundation
import SpriteKit
import UIKit

class MenuScene: SKScene {


    override init(size: CGSize) {

        super.init(size: size)

        backgroundColor = SKColor.grayColor()

        let label = SKLabelNode(fontNamed: "CourierNewPS-BoldMT")
        label.text = "Start Game"
        label.fontSize = 40
        label.fontColor = SKColor.blackColor()
        label.position = CGPoint(x: size.width/2, y: size.height/2)
        addChild(label)
    }


    override func didMoveToView(view: SKView) {

        let button1=UIButton(frame: CGRectMake(size.width/2, size.height/2, 300, 100))
        button1.backgroundColor = UIColor.greenColor()
        button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button1.setTitle("Unfocused", forState: .Normal)
        button1.setTitle("Start", forState: .Focused)
        button1.addTarget(self, action: "startGame:", forControlEvents: UIControlEvents.PrimaryActionTriggered)

        self.view?.addSubview(button1)
    }



    func startGame(sender:UIButton) {
        let gameView = view! as SKView
        gameView.ignoresSiblingOrder = true

        let reveal = SKTransition.flipHorizontalWithDuration(0.2)
        let scene = GameScene(size: self.size)
        gameView.presentScene(scene, transition:reveal)

    }


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
4

1 回答 1

1

您可以使用from方法访问您的button1对象。这是在这种情况下的参考。您可以从中调用方法:senderstartGame(sender:UIButton)button1removeFromSuperview

func startGame(sender:UIButton) {
    let gameView = view! as SKView
    gameView.ignoresSiblingOrder = true

    let reveal = SKTransition.flipHorizontalWithDuration(0.2)
    let scene = GameScene(size: self.size)
    gameView.presentScene(scene, transition:reveal)

    sender.removeFromSuperview()
}
于 2015-11-09T19:03:46.540 回答