That thing where I said I was going to take my mind off of coding for a while? It didn't work.

Posted on July 6, 2012 | 4 minute read

In last night’s post I said I’d be taking at least a few days off of doing any coding to step back and think about the larger picture.

That hasn’t happened yet. Today I:

  1. Figured out why the game lagged severely at a certain point (my own stupidity, calling a function too many times accidentally) and fixed the problem.

  2. Added the beginning stage functionality of the game, or most of it

  3. Added a pointer entity to register GUI entity clicks

  4. Reduced the number of entities being spawned at a time from about 80 or so to 3-4

The last one is what I’m most excited about. Originally what was happening as you may have seen from a really early screenshot was that I had all these makeshift collider entities being piled up on top of each other as the player moved. This is because with everything being generated dynamically, I created these entities to handle walking on the ground instead of using a collision map in ImpactJS. Now, I’m sure this is possible to do with a collision map, but frankly I don’t know how and using these entities for collision (which were originally yellow squares but are now totally transparent) seemed much more flexible for all of the checks that I needed to do. So the very first iteration of this, from June 26, looked liked this:

June 26 collider entities

JS makeshift colliders

Every one of those yellow collider entities is 20 x 20px. That’s a lot of crap being spawned. So later I made it so that any unnecessary colliders are instantly killed upon spawning and then it looked like this on June 27:

June 27 collider entities

ImpactJS screenshot

The code to spawn these each frame was the following in the Controller class and then a few internal checks were being done in the entity itself, like seeing if it’s unnecessary and should be killed, not spawning it when it’s on top of a pit, etc:

                ig.game.spawnEntity( EntityCollision, ig.game.player.pos.x + 400, 420 );

But that still made up about 80 entities in total being spawned at one time. If I want this to run as smoothly as possible, with all of the performance issues that I’ve read about when porting to iOS, I had to modify things a bit. I had an epiphany in the shower. Instead of spawning a whole bunch of colliders one after the other, why not just modify the size of the spawned collider to take up the required amount of space on the screen? Here’s the final code, overcommented for this post (by ‘final’ I mean currently functional, but as always this will likely be changing down the line):

            // Get the first pit currently on the screen
            var nextPit = ig.game.getEntitiesByType( EntityPit1 )[0];
            if (nextPit) {
                // If a pit exists, get distance from player to pit 
                // and make the collider entity that wide
                var distanceToPit = ig.game.player.distanceTo(nextPit);
                var settings = {size: {x: distanceToPit, y: 20}};
                // Spawn the collider
                ig.game.spawnEntity( EntityCollision, ig.game.player.pos.x, 420, settings );
            }

            // If there's no pit on the screen, make collider width equal to the 
            // distance between the player entity and the right side of the screen
            else {
                var settings = {size: {x: ig.system.width - ig.system.width / 3, y: 20 }}
                ig.game.spawnEntity( EntityCollision, ig.game.player.pos.x, 420, settings );
            }

Excess colliders are still being killed straight away in the collider file, so we are left with only a few colliders on the screen at one time that are just the right width.

Here’s a current screenshot of the colliders. As soon as the player reaches the edge of the collider they’re on, another is created beside it. The one below it is technically not meant to be there, but I’ll keep it because it acts as a safeguard of sorts should something go wrong with the one that’s meant to spawn (entities are all outlined in red for the purpose of the screenshot as otherwise colliders are invisible):

July 7 collider entities

JavaScript collider modifications

This helped with having less entities being spawned but has more setbacks as well, like having to get that array of pits over and over again. I already have some ideas on how to optimize this further, but right now I’m again going to try to step back and not look at the code for a while.




Categories:game dev dev games
comments powered by Disqus