Swift - Starting a game with out pressing the screen

时间:2015-06-25 18:22:42

标签: ios iphone swift

I am making a game that, when the game loads in, doesn't require the user to tap the screen to start, but starts straight away.

My code with the "tap to start":

import Foundation
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var isStarted = false

    override func didMoveToView(view: SKView) {
        //backgroundColor = UIColor.greenColor()

        addTapToStartLabel()

    func addTapToStartLabel() {
        let tapToStartLabel = SKLabelNode(text: "Tap to start!")
        tapToStartLabel.name = "tapToStartLabel"
        tapToStartLabel.position.x = view!.center.x
        tapToStartLabel.position.y = view!.center.y + 40
        tapToStartLabel.fontColor = UIColor.whiteColor()
        tapToStartLabel.fontName = "Helvetica"
        tapToStartLabel.fontSize = 22.0
        addChild(tapToStartLabel)
    }

    func start() {
        isStarted = true

        let tapToStartLabel = childNodeWithName("tapToStartLabel")
        tapToStartLabel?.removeFromParent()

        square1.stop()
        movingGround.start()
        wallGen.startGenWallsEvery(1)
        diamondGen.startGenDiamondsEvery(1)
    }

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

        if isGameOver {
            restart()
        } else if !isStarted {
            start()
        } else {
            square1.flip()
        }
    }

I have tried a couple of things and I can't seem to figure it out.

1 个答案:

答案 0 :(得分:0)

First, your didMoveToView is missing } Your game have started, but you should override update() function that is called at the beginning of every frame and do some actions there.