Reputation: 1664
I have a game idea in mind that doesn't require any of the SpriteKit features like (physics, collisions or actions) it's a simple strategy game something like ( Plague Inc , or Democracy ) of you guys are familiar with those games. So i figured i could make it easy for myself and just create it the normal way with the luxury of using Storyboard viewControllers and object instead of doing all this in code. But there is one feature SpriteKit has that would be very essential to my game( or any game for that matter ) that is not build in the normal Xcode projects which is the "Update" method, you know for keeping track of score and other values, so i implemented something close to it and works just fine:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var finish = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "check", userInfo: nil, repeats: true)
}
func check() {
if score == 10 {
println("Win!")
}
}
This works just fine but i'm not sure if this is the best practice or how much will a method like this will affect the performance of the app, especially if i have more than 1 method that runs all the time .
Upvotes: 7
Views: 1289
Reputation: 620
Just going off of your description, you should be fine with making it within UIKit. SpriteKit is made for High Performance games that require more computing power than your simple card game. The kind of games you are talking about are Not high performance at all. Most of the processing power would come from the number crunching in your simulation, but that's really about it.
If you need something like the update for the scores, you can just use a simple didSet or something like that. You could also try using a timer, but I think using a simple didSet would be the simple solution as it automatically changes when you get and set a variable. If I add one to a variable, Xcode automatically updates the score for me with didSet. I don't know all the details of your game, so just try it out in UIKit first (takes the less time). If it's slow then go to SpriteKit.
I'll link the docs to didSet for you. If you are the document reader type then try hackingWithSwift. Both are good options:
"willSet is called just before the value is stored. didSet is called immediately after the new value is stored."
if you want to read more then click the link...
Upvotes: 0
Reputation: 2789
I've created games with and without SpriteKit, and what you're doing is perfectly fine.
You may want to consider checking the score wherever you're updating the score. If the score gets updated in more than one place, then call a function that updates the score and then immediately checks it. Then, you can avoid the loop all together. Also, if the score gets updated multiple times before the loop runs, it may skip the case where score == 10.
Upvotes: 5
Reputation: 31304
With your current code, you're calling the check()
function every second. All this function does is compare an integer. This is one of the most basic things a computer can do: the CPU in an iOS device is capable of running many thousands of such comparisons in much less time than a second. So you have no reason to worry at all.
Now, as your build your game up you might find yourself extending and adding things to this method, in which case performance might become a problem. The best thing to do is, as one of the comments above suggests, just build your game and see if it becomes a problem. It's always good to be thinking about performance, but at the same time it can be bad to optimize when you don't need to.
Upvotes: 2