Friday, April 1, 2016

Swiftly Learning: Mini Combat Part 1: NSTimer

Mini Combat. Pretty exciting stuff. Two dudes, two buttons, a few lines of text, a few more lines of code making them all do things. You can view it here, if you like! 

This was my solution to an exercise in Mark Price's iOS 9 and Swift 2: From Beginner to Paid Professional class. In order to keep it on Github, I will need to change the graphics out from the provided ones to some of my own... or the internet's. :)

This project has me working with Classes in Swift for the first time. It's interesting, the other course I was doing suggested using Structs before Classes and then this one hasn't even mentioned Structs at all yet. I don't know which way is right, not yet. Maybe he's saving Structs for later cause they are so badass? We'll see!

The basic requirements were something like:
  • Landscape only
  • 2 characters each with HP, attack power, a name, and an attack button
  • Attack button is disabled for 3 seconds after an attack is made
  • Winner should be displayed at the end
  • Lets you restart the game
What tripped me up so far on this one was disabling the button for 3 seconds after attacking. I knew how to disable a button, and I knew I had to use an NSTimer, but actually making them work together was a huge pain.

I started out with this, which did not work:
NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("onPlayerButtonPressed"), userInfo: nil, repeats: false)

I knew that the Selector part had to be a function, but I couldn't figure out what function it was supposed to be. My best guess was whatever was calling for the timer. So, I tried all of them - and they all failed! Shit! I googled and googled and found lots of info about Selectors but nothing answered my question. I finally asked a friend if he could explain it and he did, in one sentence - "it's just the method name for the method that you want called when the time fires". I probably should have figured that out - but now I knew! This led me to create my little buddy, enableButton! 

The problem with enableButton is that I'm actually pretty tired and can't wrap my head around how I'm supposed to use it for both of the buttons that I have. I'm sure I'll figure that out tomorrow - but for now I'm breaking the "don't repeat yourself" rule just to get it working. (I'm also doing almost all of my shit in ViewController.swift so it's not like I'm doing anything right yet anyways!). 

Another little tidbit that I encountered while trying to figure out the NSTimer shenanigans, is that the whole Selector thing changed in Swift 2.2 which came out what, last week? I sure am glad I watched the video on devslopes.com about the Swift 2.2 changes already. Armed with that, Xcode's auto-suggest, and my new friend enableButton, I crafted this, which works as intended:

NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(ViewController.enableBlackButton), userInfo: nil, repeats: false)

Note the selector part - definitely different than it was:
selector: Selector("enableButton")
vs
#selector(ViewController.enableButton)

Yep. Now it works, and when my little dudes attack, they're forced to take a 3 second break before they attack again. Speaking of breaks, this one has been long enough. I'd better get back to it and try to figure out that button thing. 


No comments:

Post a Comment