How much impact do ImpactJS timers have on performance? Plus implementing player speed boosts.

Posted on July 2, 2012 | 3 minute read

Got quite a bit done yesterday, including a new kind of obstacle and quite a few difficulty refinements. I also changed how I handle player movement. Not sure yet if this is the best way I could’ve done this, but it works better than what I had with the amount of velocity changes the player entity has to go through. I started running into problems when trying to implement a speed boost, but ended up getting it working in the end after making the movement changes.

How much, if any, impact do running timers have on performance?

One thing I’m not sure about is how much impact timers have on performance. That is if I initiate a timer when I spawn an entity, but don’t use it until later is it worth pausing the timer and then unpausing it, or should I just let it run? Currently I define my timer, pause it, unpause it when I need it, and then pause it again. But is this actually making any difference (or is it potentially even doing more harm than good)?

I want the player entity’s vel.x to increase temporarily when the player presses D or the right arrow key. To do this I created three new attributes in the player entity:

    boost: 500,
    boosted: false,
    boostsLeft: 3,
    maxBoosts: 3,

Then in the player’s init() I create the boostTime timer and pause it immediately:

        this.boostTime = new ig.Timer(0.2);
        this.boostTime.pause();

In the update() function, when conditions to trigger a boost are met, I do this (over-commented for the purpose of this post):

         // Speed Boost
        if( ig.input.pressed('boost') && !this.boosted && this.boostsLeft > 0) {
            // Reset and unpause timer
            this.boostTime.reset();
            this.boostTime.unpause();
            // Increase vel.x 
            this.maxVel.x += this.boost;
            this.setSpeed();
            // Identify as Boosted and decrease remaining boosts
            this.boosted = true;
            this.boostsLeft--;
        }

        // If the player is boosted and time runs out, decrease speed	
        if (this.boostTime.delta() > 0 && this.boosted) {
                // Decrease vel.x back to pre-boost levels
                this.maxVel.x -= this.boost;
                this.setSpeed();	
                // Identify as unboosted and pause timer again
                this.boosted = false;
                this.boostTime.pause();
            }

I’m not sure if I should just let the timer run without the pausing and unpausing, or use a different solution altogether. Right now this does what I need it to do, but I’m trying to get everything to run as efficiently as possible. I know it’s said that we shouldn’t really worry about performance until we have functionality in and working. I’m following this advice for the most part, but without going into detail I know based on others’ experiences that I will very likely be running into very annoying performance issues down the line with this particular project, so it remains in the back of my mind.




Categories:game dev dev games
comments powered by Disqus