For healthbar, it's best to use the draw actor of the player to assign it's value; because getting hurt isn't the only thing that changes it, getting a health powerup also changes it.
Player>Draw Actor>
- Code: Select all
int i=round(health*0.1);
healthbar.animpos=i;
If the player only has 10 health points anyway, you can skip the i/round part and just assign the animpos directly.
I used (health*0.1), it does the same as (health/10) but it's good to get in the habit of using multiplication instead of division, as its faster (though it doesn't matter with single objects like player; it's enemies and bullets where this is really important).
With other things, i like to make a 'control' actor to handle events that aren't actor specific. here's what i mean:
WIth the healthbar script, i just put it in player, because health is a player variable, and we may do other things with it too, like link can throw swords when his health is full. Therefore, it's good to keep all script referring to player health inside player's draw actor.
Now the monster spawn rate is not linked to any specific creature, so i'd put that, along with level script and music code inside an actor called 'control'.
So to make a game harder as time goes on, use a specific variable to count down with, like this:
timera++;
if(timera==tlimit){
timera=0;
tlimit--; //every time tlimit is reached, it gets smaller, therefore spawning enemies faster!
//do spawn stuff here
}
When tlimit reaches a tiny number, like 0, you can then say that player has won the level. Then, to disable the script, just set tlimit to a negative number. timera counting up will never get to that negative number, then you can set tlimit positive again to start spawning all over again.
Now when you want to make your random spawning positions, precalculate them (don't use complex math inside a function call)
i=round(rand(20))-10; //rand 20 -10 results in answer from -10 to 10.
j=round(rand(20))-10;
CreateActor("Monster", "m1", "", "", i, j, true); //use i and j here, not the rand() math.
So throw that inside the timera script, and boom! Randomly spawning actors that spawn faster as the level progresses.
One thing you can do is if you have waves, or levels, you can work that into the same script - putting this before you calculate i an j and spawn the monsters:
for(i=0; i<=wave; i++){
i=round...etc; etc. //rest of spawning script here
}
Now, not only does the level get harder as time goes on, but each level spawns one more enemy each time than the last one.
Two notes: I always use i and j as my example ints; usually i is reserved for loops, it's good to name them something that's easy to work with but also describes what they are, if i were making this script in my own game i'd probably call the monster
random spawn values mx and my, (for monsterx and monstery).
Also, you see i am fond of using variables to count instead of the timer() function; timers aren't really meant to be used as frame counts. This is a big misconception for many GE beginners;
A timer is to get a specific amount of time, for performing time-sensitive but not GAME TIME sensitive operations, such as....starting a new song immediately after the current song finishes.
Since framerate can change depending upon the action and the computer speed, timers can screw stuff up. I had a game where stuff spawned in timers; the game slowed down, the timer didn't, more stuff spawned, slowing the game down further, and so on in a snowball effect until the game crashed.
Also, timer is an actor specific variable; it's not global. This means that if link dies, he cannot disable any timers in any other actors but himself. If control were spawning enemies on a timer, link would have to find a way to stop control when he died, (DestroyTimer() only works inside the actor the timer is called for!) while using a variable, link can just say
"control.tlimit=-1;" and control will stop spawning.