Diego Benítez Diego Benítez - 5 months ago 34
Swift Question

How can I flash the whole screen?

I wanted to know how can the the screen flash when I touch the screen. I had tried to

colorizeWhitColor
but it only colorized the background, and I don't know how to return to the same color.

scene?.runAction(SKAction.colorizeWithColor(UIColor.blackColor(), colorBlendFactor: 1.0, duration: 0.5))


class GameViewController: UIViewController {

let viewFlash = UIView.init(frame: UIScreen.mainScreen().bounds)

override func viewDidLoad() {
super.viewDidLoad()

if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill

skView.presentScene(scene)

viewFlash.hidden = true
self.view.addSubview(viewFlash)
}
func flashScreen(color: UIColor, flashTime: NSTimeInterval){
viewFlash.backgroundColor = color
viewFlash.hidden = false



NSTimer.scheduledTimerWithTimeInterval(flashTime, target: self, selector:#selector(ViewController.stopFlash), userInfo: nil, repeats: false) //here it tells me that

}
func stopFlash(){
viewFlash.hidden = true
}

}



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

/* Called when a touch begins */




if gameStarted == false {

flashScreen(UIColor.whiteColor(), 0.1)


circuloVerde.removeFromParent()
circuloMorado.removeFromParent()
circuloRojo.removeFromParent()
circuloBlanco.removeFromParent()


enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.685, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true)


gameStarted = true

circuloPrincipal.runAction(SKAction.scaleTo(0.44, duration: 0.5))

score = 0

scoreLabel.text = "\(score)"

hits = 0

highscoreLabel.runAction(SKAction.fadeOutWithDuration(0.5))



}



}

Answer

In GameViewController.swift:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    let viewFlash = UIView.init(frame: UIScreen.mainScreen().bounds)

    override func viewDidLoad() {
        super.viewDidLoad()

        viewFlash.hidden = true
        self.view.addSubview(viewFlash)

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = false
            skView.showsNodeCount = false

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill

            skView.presentScene(scene)
        }
    }
    func flashScreen(color: UIColor, flashTime: NSTimeInterval){
        viewFlash.backgroundColor = color
        viewFlash.hidden = false
        NSTimer.scheduledTimerWithTimeInterval(flashTime, target: self, selector:#selector(self.stopFlash), userInfo: nil, repeats: false)
    }
    func stopFlash(){
        viewFlash.hidden = true
    }
}

In GameScene.swift:

import SpriteKit

class GameScene: SKScene {

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

        /* Called when a touch begins */

        if gameStarted == false {

            //This is the new part
            var parentVC = view?.window?.rootViewController as! GameViewController
            parentVC.flashScreen(UIColor.whiteColor(), flashTime: 0.1)
            //This is the new part

            circuloVerde.removeFromParent()
            circuloMorado.removeFromParent()
            circuloRojo.removeFromParent()
            circuloBlanco.removeFromParent()


            enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.685, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true)


            gameStarted = true

            circuloPrincipal.runAction(SKAction.scaleTo(0.44, duration: 0.5))

            score = 0

            scoreLabel.text = "\(score)"

            hits = 0

            highscoreLabel.runAction(SKAction.fadeOutWithDuration(0.5))

        }
    }
}