Thursday, May 14, 2015
Saturday, May 2, 2015
CJAW : Swift TicTacToe Part2
This week, added some minor changes to the Tic Tac Toe Game. Basically replaced all x,y parameters to CGPoint, and used grid size’s half width and half height in order to position the Xs and Os. Then added a reset button to clear the nodes on screen.
To add reset button :
In Main.Storyboard, drag and drop a button(from Object library. If you are not able to see Object library on the right bottom corner, then do View->Utilities->Show Identity Inspector to see the library) into a suitable location inside the view controller. I moved the button to bottom left corner, below a space where the grid would appear.
If you are not able to view assistant editor, then click on View->Assistant Editor->Show Assistant Editor. This window shows the GameViewController.swift file’s code. Do a control+drag from the button to somewhere inside the class GameViewController. When prompted, chose Connection:Action ( to call a function when the button is touched), Name: OnTouch(), Type:UIButton, Event:Touch Up Inside, Arguments: Sender. This will create a function inside the GameViewController class. This function will be called when the button is touched.
@IBAction func OnTouch(sender: UIButton) {
PresentGameScene()
}
Create a function called PresentGameScene() which would delete all existing nodes inside the GameScene, and make the view present the scene once again, fresh.
func PresentGameScene(){
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* 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
scene.Reset();
skView.presentScene(scene)
}
//Make sure to reuse the code in
override func viewDidLoad() {
super.viewDidLoad()
PresentGameScene();
PresentGameScene();
}
Now we need to implement scene.Reset(). In GameScene.swift file, implement the function
func Reset(){
self.removeAllChildren();
}
That is all. It is quite simple. removeAllChildren takes care of removing all the child nodes from the game scene, and cleans the slate for fresh start.
Github link: https://github.com/swtsvn/CJAW/tree/TicTacMain/TicTac
Subscribe to:
Comments (Atom)