23 July 2011

Day 2 - Creating enemies

I know I said I wasn't going to update the game over the weekend, but I was bored this arvo so I did a little bit of work.  I didn't get much done, but I now have a function which will create an enemy which will move across the screen.  I only had about half an hour so I haven't coded the hit detection for when the enemy gets shot by a bullet or anything like that yet, but that will come soon enough.

So the first thing  I need to do is create a new MC in the library to represent the enemy.  So I created a circle which will represent the enemy for now, I can always make something better looking later.  I also want some way for the user to know how much health each enemy has left.  So I created another movieclip called progressbar which is just a red rectangle.  I will attach an instance of progressbar to each new enemy and then as the enemies health decreases I will decrease the length of progressbar.  I don't know if that makes sense but you'll see what I mean.

Function makeenemy()
The first function I need to code is the function to create an enemy, which I will call makeenemy().  I'll post the code and then run through it.
enemy_xspeeds = new Array();
enemy_yspeeds = new Array();
enemy_rspeeds = new Array();
enemy_hps = new Array();
enemy_starthps = new Array();
enemy_maxspeeds = new Array();
enemy_IDs  = new Array();
enemycount = 0;
nextenemyID = 0;
function makeenemy(type:Number){
    switch(type){
        case 1:
            enemy_maxspeeds[enemycount] = 4;
            enemy_starthps[enemycount] = 20;
            size = 40;
            break;
    }

    enemy_xspeeds[enemycount] = 0;
    enemy_yspeeds[enemycount] = enemy_maxspeeds[enemycount];
    enemy_hps[enemycount] = enemy_starthps[enemycount]; 
    enemy_IDs[enemycount] = nextenemyID;
    enemy_rspeeds[enemycount] = 0;
    en = _root.attachMovie("enemy_"+type,"enemy_"+nextenemyID,_root.getNextHighestDepth());
    en._x = Math.round(Math.random()*(stagexmax-stagexmin-10))+5;
    en._y = stageymin + (enemy_sizes[enemycount]/2);
    en._width = enemy_sizes[enemycount];
    en._height = enemy_sizes[enemycount];

    progressbar = en.attachMovie("progressbar","progressbar",_root.getNextHighestDepth());
    progressbar._width = 40;
    progressbar._height = 6;
    nextenemyID++;
    enemycount++;
}
Now like the bullets, we need a whole lot of arrays to keep track of the enemy data, and they are declared in lines 1 to 7. Like with the bullets, I need arrays for x, y and rotation speeds. I also need an arrays for health, the enemies starting health (for changing the length of the progressbar), and the enemies max speed.

line 10 declares the function to create a new enemy.

Line 11 is a switch statement which is like an if statement but is better suited to multiple different scenarios. Because I don't know how many different types of enemies i might end up with in the game it is better to use this statement instead of a simple if statement.

What this switch statement is saying is if type = 1 then perform all the code between case 1: and break; and then jump to the closing } bracket of the switch statement.

Lines 19 - 23 are pretty self explanatory, just setting up variables for the new enemy. The enemy appears at a random point at the top of the screen.

Lines 24 - 28 attach the enemy to the screen, resize it and move it into position.

Lines 30 - 32 attach the progressbar to the enemy movieclip and resize it.

Function enemystep()

The second function is very similar to the bulletstep() function I discussed the other day.  It is called every frame and controls the movement of each enemy on screen at that moment.

 
function enemystep(){
    for (a=0;a<enemycount;a++){
        if (enemy_yspeeds[a] < enemy_maxspeeds[a]){enemy_yspeeds[a] += gravity;}
        if (enemy_yspeeds[a] > enemy_maxspeeds[a]){enemy_yspeeds[a] = enemy_maxspeeds[a];}
        if (enemy_xspeeds[a] != 0){enemy_xspeeds[a] *= xfriction;}
        _root['enemy_'+enemy_IDs[a]]._x += enemy_xspeeds[a];
        _root['enemy_'+enemy_IDs[a]]._y += enemy_yspeeds[a];
        _root['enemy_'+enemy_IDs[a]]._rotation += enemy_rspeeds[a];
    }
}  
A pretty simple function, line 2 is a loop to step through all the enemies on screen. Line 3 applies gravity to the enemies vertical (y) speed if it isn't already at it's maximum speed and Line 4 reduces the enemies speed if it goes over the maximum.
Line 5 applies friction to the enemies horizontal (x) speed so that it doesn't just keep going endlessly.
Lines 6 - 8 move the actual object on stage.

Lastly I added a new line to gunbarrell.onEnterFrame() which calls enemystep() every frame.

Ok that's all for now, I'm going out tonight, more probably on Monday. Sorry no working prototype today, you'll just have to wait until Monday.

14 comments:

  1. Excellent post, look forward to reading you're next update!

    ReplyDelete
  2. dude you code like a beast! i cant wait to start using what i learn here in my java class.

    ReplyDelete
  3. So do you ever mess up and have to look through code for ages just to fix it?

    ReplyDelete
  4. wow man you really are doing a lot of work. im impressed good sir!
    p.s. i liked the title of the blog post, i thought it was about making enemies not literally 'making' them

    ReplyDelete
  5. And you do this when you're BORED?

    Can't wait to see what the prototype looks like!

    ReplyDelete
  6. How do you proofread your code? Does it take a long time? I hope maybe someday I'll get to play around on a game you design. :) I'd buy it, definitely. Have a good weekend, mate.

    ReplyDelete
  7. Action script seems pretty straight forward to other languages. Maybe i will try it out.

    ReplyDelete
  8. Amazing, this sounds really pro. Keep doing your good work and posts.

    ReplyDelete
  9. Your blog is really great. There's not that much which post stuff so interesting.
    Congratz and keep doing it.
    Followed!

    ReplyDelete
  10. Agh code! It's an alien language that I don't understand.

    ReplyDelete
  11. Really interesting to watch your progress... Flash is alien to me though, i'm all about javascript.

    ReplyDelete
  12. Really nice, that's not too hard at all!

    ReplyDelete