How to increase speed of timer object using Swift - ios

I am using an NSTimer.scheduledTimerWithTimeInterval object but cannot increase the speed of the clock to faster than 1/1000. I have tried invalidating the clock and re-creating. timer2 & timer3 work fine on the iOS simulator but does not work when testing on iPhone 5s device. Speed stays always on 1/1000 even when firing timer2 or timer3. Below is my definition:
timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
timer2 = NSTimer.scheduledTimerWithTimeInterval(0.002, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
timer3 = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

You cannot change a timer. If you want a timer running at a different interval, you have to invalidate and destroy the existing timer and replace it with a new one. That's quite a common thing to do; timers are lightweight objects.

Related

Scheduled timers in SpriteKit

I have scheduled timers that add sprite nodes to the screen as obstacles
func timers(){
personTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(spawnPerson), userInfo: nil, repeats: true)
bikeTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(spawnBike), userInfo: nil, repeats: true)
motorcycleTimer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(spawnMotorcycle), userInfo: nil, repeats: true)
}
I added a function to invalidate those timers. so that a bonus "level" can be ran.
func invalidateTimers(){
// Obstacles
personTimer.invalidate()
bikeTimer.invalidate()
motorcycleTimer.invalidate()
}
When the bonus is called
func bonus() {
invalidateTimers()
bonusTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(spawnDiamonds), userInfo: nil, repeats: true)
}
The problem that I'm having is that when the bonus is done running I invalidate the bonusTimer and recall timers(). But when I do all the timers in the function seem to be firing twice. Whats an easy workaround for that since they can't just be paused.
Instead of using timers, consider using SKActions, as they work well with SpriteKit. To start the timer, run:
let wait1 = SKAction.wait(forDuration: 1)
let personTimer = SKAction.repeatForever(SKAction.sequence([wait1, SKAction.run {
spawnPerson() // spawnBike() etc. for each different timer
}]))
self.run(personTimer, withKey: "spawnPerson")
with modified wait values and function calls for each different timer. Then to stop the timer, run:
self.removeAction(forKey: "spawnPerson")
for each action using a different key.
Instead of using timers, you can use update: method of your SKScene subclass. It calls once per frame and has currentTime parameter. So you can easily calculate time intervals you need and trigger corresponding methods.

How to add a timer to a function - IOS Swift

Say I had a function, any function, that I wanted to run for only three seconds and then never run again. How would I do this? Would I use NSTimer? Thank you for your help.
Here you go,
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("updateProgress:"), userInfo: nil, repeats: false)
func updateProgress(timer: NSTimer){
}

I can't declare a NSTimer in the GameScene class as it believes there is an "Extra Argument 'selector' in call [duplicate]

I have the following line of code:
changeColour = NSTimer.scheduledTimerWithTimeInterval(TIMES, target: self, selector: "changeColourOfPage", repeats: true)
but it gives the error "Extra argument 'selector' in call"
when I change the TIMES variable to a number like 1.0, it works fine. The variable TIMES is set to 1.0.
Is this just a glitch, or am I being stupid about something?
I need to use it to run a method at random intervals.
Please help!
Just had this same issue. For me the problem was that I was passing a time interval as a Float not a Double. Simple fix was (using the code from the original post):
NSTimer.scheduledTimerWithTimeInterval(Double(TIMES), target: self, selector: Selector("changeColourOfPage"), userInfo:nil, repeats: true)
I had the same error message and once I cast the time as a Double it worked fine. Hope that helps someone!
It looks like you're missing the userInfo argument. Try this:
Swift 2
let TIMES = 1.0
var changeColour = NSTimer.scheduledTimerWithTimeInterval(TIMES, target: self, selector: "restart", userInfo: nil, repeats: true)
Swift 3, 4, 5
let TIMES = 1.0
var changeColour = Timer.scheduledTimer(timeInterval: TIMES, target: self, selector: #selector(restart), userInfo: nil, repeats: true)

Extra argument 'selector' in call error

class ViewController: UIViewController {
func ChangePage()
{
NSLog("Hej")
}
var timers = NSTimer(NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
}
I get the following error from Xcode 6:
Extra Argument 'selector' in call
I've tried several configurations, does it have something to do with where in the code it's placed?
You might want to use:
var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changePage", userInfo: nil, repeats: true)
This returns a timer that is already added to the run loop and fires automatically.
To stop the timer to fire, you must invalidate it like this
timer.invalidate()
You should add timeInterval in the constructor like:
NSTimer(timeInterval: NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
And yes, it does matter where you put. The problem is, that timers is a property, and it is created before the initialization. So when it is created, self is not existing, but you refer to it, and that causes the problem.

Can an NSTimer take multiple selectors?

I'm trying to use an NSTimer in my app, and was wondering if it's possible to call two methods when the timer fires.
Here's the code:
gameTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector:
Selector("gameMovement" && "fireBullet"), userInfo: nil, repeats: true)
I'm getting an error saying there are two arguments in the Selector.
Nope. You would call just one method that delegates to all the things you want.
func someFunc() {
gameTimer = NSTimer.scheduledTimerWithTimeInterval(
0.01,
target: self,
selector: Selector("timerFired"),
userInfo: nil,
repeats: true
)
}
func timerFired() {
gameMovement()
fireBullet()
}
This is a more maintainable pattern anyway, as it's easier to see how your code flows.

Resources