The Spawn Engine

an educational open source Flash game engine

Updating newEnemy for version 0.2 using variables instead of numbers

If you have downloaded the release of the Spawn Enginen version 0.1 and played around with it you will have noticed a lot of things that need to be improved. Please feel free to comment what changes you would suggest! The idea is that each new release of the Engine should have a few improvements slowly making it better and better but always leaving a little room for students to improve the Engine. I don’t have any ambition to ever make the engine completely perfect. There must always be room for improvement inspiring students to do their best to do those improvements themselves.

This time I thought I’d blog about one area of improvement and I’d like to demonstrate it by showing how we will update newEnemy for version 0.2 using variables instead of numbers. This is what the function looks like in version 0.1 (removing comments).

public function newEnemy(te:TimerEvent) {
var e:Enemy = new Enemy;
var slumpX = 10 + Math.random()*530;
e.x = slumpX;
e.y = 10;
addChild(e);
enemies.push(e);
setNextEnemy();
}

The function works fine but there are a few things that the programmers eye should catch. Not errors just not good looking code.

Using variables instead of numbers

The first one I’d like to point out is using variables instead of numbers. Lets illustrate this with an example:

I want to use the engine for a game with a wider stage than the original engine has. I’m not sure how wide though. At the beginning of the design phase I’m changing the width of the stage back and forth to try and see what width is most suitable considering how wide enemies, pickups and the player is. Each time I change the width of the stage I need to go and change that number 530 to represent the correct value for the new width of the stage. If I have any other places in my code where I use a number for the stages width I’ll have to change them too. Annoying and time consuming! Lots of changes in lots of places. It’s also easy to miss one place and get errors. Not good. We should use variables or constants instead so we only need change in one place: at the top of our code where the variables and constants are declared.

I would add enemyMargin as an int variable with value 10 at the top of our code where we add the other enemy variables. I would also add the constant STAGE_WIDTH with the value corresponding to the stages width. If you have a GUI that removes some part of the stage you can adjust STAGE_WIDTH and STAGE_HEIGHT to have the correct values for your game.

Updating newEnemy for version 0.2 would look something like this:

public function newEnemy(te:TimerEvent) {
var e:Enemy=new Enemy;
var randomX = enemyMargin + Math.random() * (STAGE_WIDTH – enemyMargin);
e.x = randomX;
e.y = e.height;
addChild(e);
enemies.push(e);
setNextEnemy();
}

As you notive I also changed the swedish variable name slumpX to an english name randomX and also changed the numeric value of e.y to e.height. This value could be set to zero if you want the enemies to spawn off stage.

This was a little updating newEnemy for version 0.2 using variables instead of numbers. I hope you can find the many more places in 0.1 where we make this mistake. Can you find them and post a comment?

2011/03/13 Posted by | Engine Releases | , , | Leave a comment