The Spawn Engine

an educational open source Flash game engine

2×2 pickups in each spawn using for loops

Lets say we want to have 2×2 pickups in each spawn using for loops. Maybe your pickup is ammo and you need more ammo pickups in order to balance the game. Having four pickups come each time in a 2×2 formation makes it harder to pick them all up but not impossible. This addon is a simple change to the newPickup function.

Remember that this addon is written for version 0.1 so if you are reading this from the future using some other version then there might be some small changes you’ll need to take into account.

First we need to change the line

var p:Pickup = new Pickup;

to separate the declaration and initiation by writing

var p:Pickup;
p = new Pickup;

This simply because we want to add a for loop and inside the loop we want to initialize a new p (second row) while keeping the declaration outside the loop (first row).

Now how do we write a for loop? If you dont remember you should read Step 2 of activetuts Loop article. Read it? Good! Lets continue. You know we want the loop to go around twice and in each loop create two pickups next to each other. Lets write the for loop like this

var p:Pickup;
for (var j:int=0; j<2; j++) {
p = new Pickup;

Where do we put the closing brace? Well we want the for loop to create the pickup and add it to the stage and our pickup array. So add the closing brace right before setNextPickup().

You can test your code now but we are only spawning two pickups now and they will both have random x values and not be next to each other. To fix this we first want to move the creation of slumpX out of the loop, like this

var p:Pickup;
var slumpX = 10 + Math.random()*530;
for (var j:int=0; j<2; j++) {

Now we only make the random number slumpX once outside the loop and can use that inside the loop to place our pickups in formation. We want to change p.x = slumpX so it uses the variable j from the loop for example like this

p.x = slumpX + j*10;

In our first round in the loop we know that j is zero so the first pickup will be placed on an x value of slumpX + 0*10 which will put the pickup at slumpX. Our next and last pickup will be placed at slumpX + 1*10 which means that 10 is how far to the right the second pickup will be placed. Try it out! Maybe you want your pickups closer or farther apart. A little beta testing will help determine that.

How would you write if you want to make sure that regardless of how wide the pickup is there will always be a distance of 10 between the two pickups? You would of course use the property width of the movieclip p by writing p.width like

p.x = slumpX + j*(p.width + 10);

Try it out!

So now we have two pickups spawn next to each other. Lets add another two by using another for loop. This second loop should be placed right around the first by starting before and finishing after like this

for (var i:int=0; i<2; i++) {
for (var j:int=0; j<2; j++) {

The first loop has a loop variable i that we can use in order to space out the pickups along the y axis. We need to change the line p.y = 10 by using the i variable just like we used the j variable to space out the x values.

p.y = 10 + i*(p.height + 10);

Now test the code! It should run and give you 2×2 pickups in each spawn. My code looks like this now

public function newPickup(te:TimerEvent) {
var p:Pickup;
var slumpX = 10 + Math.random()*530;
for (var j:int=0; j<2; j++) {
for (var i:int=0; i<2; i++) {
p = new Pickup;
p.x = slumpX + i*(p.width+10);
p.y = 10 + j*(p.height + 10);
addChild(p);
pickups.push(p);
}
}
setNextPickup();
}

If you understand this tutorial you should be able to add two or three enemies spawning next to each other in each spawn. Or every time you shoot instead of just one shot you could have two. Give it a try!

How can you make this addon better? What did I do wrong or less effective? Just comment below!

2011/03/13 Posted by | Tutorials | , | Leave a comment