如何使按钮和标签出现和消失

How to make buttons and labels appear and disappear

我想知道的:

我想知道如何让buttons/labels出现和消失。当我的角色与一个物体发生碰撞时,buttons/labels 将显示在视图上方并且游戏视图将不再工作,只能与出现的 buttons/labels 进行交互。

我尝试过的:

我已经尝试了 .hidden = false.hidden = true 但它没有用,也许我没有正确使用它。

代码:我已经删除了不需要的代码!

import Foundation

import AVFoundation

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var movingGround: PPMovingGround!
var square1: PPSquare1!
var square2: PPSquare2!
var wallGen: PPWallGen!

var isStarted = false
var isGameOver = false


override func didMoveToView(view: SKView) {


    addMovingGround()
    addSquare1()
    addWallGen()
    start()


}

func addSquare1() {
    square1 = PPSquare1()
    square1.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + square1.frame.size.height/2)
    square1.zPosition = 1
    playerNode.addChild(square1)
}

func addWallGen() {
    wallGen = PPWallGen(color: UIColor.clearColor(), size: view!.frame.size)
    wallGen.position = view!.center
    addChild(wallGen)
}


func start() {
    isStarted = true

    //square2.stop()
    square1.stop()
    movingGround.start()
    wallGen.startGenWallsEvery(1)
}


// MARK - Game Lifecycle


func gameOver() {
    isGameOver = true

    // everything stops

    //square2.fall()
    square1.fall()
    wallGen.stopWalls()
    diamondGen.stopDiamonds()
    movingGround.stop()
    square1.stop()
    //square2.stop()


    // create game over label
    let gameOverLabel = SKLabelNode(text: "Game Over!")
    gameOverLabel.fontColor = UIColor.whiteColor()
    gameOverLabel.fontName = "Helvetica"
    gameOverLabel.position.x = view!.center.x
    gameOverLabel.position.y = view!.center.y + 80
    gameOverLabel.fontSize = 22.0
    addChild(gameOverLabel)

func restart() {

    let newScence = GameScene(size: view!.bounds.size)
    newScence.scaleMode = .AspectFill

    view!.presentScene(newScence)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    if isGameOver {
        restart()
    } else {
        square1.flip()
    }

}

override func update(currentTime: CFTimeInterval) {


// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {


    if !isGameOver {
        gameOver()
    } else {
        println("error, not game over!"        
}

没有看到您的代码,这有点难以确定,但我建议如下:

  1. 确保已将按钮连接到 Outlet 变量。这很关键。如果不连接它们,您可以使用隐藏的布尔值,但它不会对实际按钮产生影响。

  2. 确保您没有以某种方式撤消您自己的更改。例如,在代码的更下方,即使将 hidden 设置为 true 之后,您可能也会将其设置为 false,依此类推。

  3. 在某些情况下,您可能希望将出口变量设置为强而不是弱。这可能会保留因视图切换而丢失的更改。

  4. 也可以使用"alpha"如:

    myButton.alpha = 0

作为控制可见性的替代方法。 0 会将 alpha 设置为 none(这将使按钮不可见),1 会将 alpha 设置为完整(这将使按钮再次可见。)

  1. 设置隐藏(或 alpha)后立即输入: println("i hid the button!") 只是为了确保您认为正在执行的代码确实正在执行。有时我们认为不起作用的代码实际上什至没有被调用。

请提供更多信息,我很乐意为您解决这个问题。