【发布时间】:2015-09-14 16:26:04
【问题描述】:
我想知道的:
我想知道如何让按钮/标签出现和消失。当我的角色与对象碰撞时,按钮/标签将显示在视图上,并且游戏视图将不再起作用,只有出现的按钮/标签可以与之交互。
我尝试过的:
我尝试了.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!"
}
【问题讨论】:
-
所以您的意思是当您将
.hidden设置为false/true时按钮并没有消失和重新出现? -
@Kametrixom 是的,但我必须没有正确使用该命令,你能给我举个例子吗?由于我的按钮/标签总是出现在我的游戏中,所以...idk。我将我的按钮放在 Main.storyboard 中,这样它在玩游戏时就会与游戏重叠。但我不希望它只是在玩家与某物碰撞时出现。我已经为此设置了一些东西,我只需要知道如何使用这些命令。
-
那你得把右边属性检查器中的“hidden”属性设置为false,那么开头就不显示了
-
@Kametrixom 可以隐藏它们,但是现在如何让它们在与我的对象碰撞时出现?
-
只需将
hidden属性设置为true。我对 SpriteKit 中的碰撞没有任何经验,因此我无法为您提供帮助,但我相信如果您只是在 Google 中搜索它,您会找到很多教程
标签: ios swift button sprite-kit labels