ForumsProgramming ForumHealth code

11 3807
Midmenj
offline
Midmenj
216 posts
Nomad

Anybody know a decent health code that works for Flash CS4 professional using AS 2.0?

also does anybody know hot to lock levels and make it so once they pass it they can play it?

I would be thankful if anyone knows how to do it. It's for a game i'm making.

  • 11 Replies
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Health like in energy? You don't know how to use variables and _xscale?

//Starting value
Health = 100;

//Put this inside a loop
EnergyBar._xscale = Health;


_xscale works with percentages, so 100 equals the full bar, and it will get smaller whenever you subtract from the variable. It's imperative that it's in a Loop like onEnterFrame, or else it would run only once, and wouldn't update the value

You can do the level thing with global variables. To make a variable global just put _global. in front of it

_global.Level2 = True;
Midmenj
offline
Midmenj
216 posts
Nomad

so where do you put what? haha i need more instruction then that thanks but more detail

jpx111
offline
jpx111
264 posts
Nomad

Midmenj, I'd recommend that you learn the concept of variables and constants before proceeding to a task such as making a game.
I'll explain the code and the concept of variables a bit here...

The word 'Health' is a variable. Pretty much anything can be a variable (or var). A variable in programming is exactly like a variable in mathematics...it is a letter/word that can have values that change.
You are making 'Health' a variable since this is going to change as you play the game. I presume what you are trying to make is a game in which the character's health goes down as he hits something or something hits him. So you need to make a hit test. The code will be structured sorta like this:

If hittest(whatever condition) = true {
Health -= Health - 10;
EnergyBar._xscale = Health;
}

Also, you will need a health bar with the instance name of EnergyBar for this to work.
And, this code will go in the main timeline...

If you don't understand hit test, plz google it...I would explain it to you but i gtg...

Hope you understand it

Midmenj
offline
Midmenj
216 posts
Nomad

kind of... but say im making a game and when the character touches the wall or something it losses health would i still use this?

and do i put the hit test in the wall?

and does the health bar need anything else other then the instance name?

thanks dude im appreciating this a lot! =]

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Take a look in Maaron's thread, he is making a maze game too

http://armorgames.com/community/thread/2366645/maze-game

Midmenj
offline
Midmenj
216 posts
Nomad

k thanks dude

PixelSmash
offline
PixelSmash
566 posts
Nomad

I do have to comment on the code from jpx111... I'm pretty sure it's just a typo, but I'd hate for you to use it and find some strange readings

If hittest(whatever condition) = true {
Health -= Health - 10;
EnergyBar._xscale = Health;
}

The second line should be Health -= 10;

If you're using different damages, it's a good idea to have that damage in a seperate value, making it a bit like this:

var damage1:Number = 10;

if(hitTest(mc1,mc2)){
Health -= damage1;
}

Also, instead of changing the HP bar when the damage is dealt, perhaps an update function at the end of the code is a better idea... that way it only updates once, instead of possibly more than once per frame. (For instance, when a player is hit by three bullets at once.)
This makes your code run a bit faster - it only has to execute the _xscale once, instead of three times!

function Update(){
HPbar._xscale = 100*Health;
}
jpx111
offline
jpx111
264 posts
Nomad

Lol yes I was probably drunk when I wrote that :P

And just so you know, Midmenj, there are probably better and more efficient ways of doing it. I am not a flash programmer...I know programming from a more generic view...thus, you most probably will be able to optimize the code I've shown you in more ways. Try to find things like PixelSmash did and work them out

PixelSmash
offline
PixelSmash
566 posts
Nomad

Lol for posting drunk

And indeed... what you could do is get all of your code working, quick and dirty, and then optimize it so that it doesn't take as much computing. The less lines of script flash has to read, the faster it'll run!

jpx111
offline
jpx111
264 posts
Nomad

Lol metaphorically of course :P

Dirty coding...nice

Midmenj
offline
Midmenj
216 posts
Nomad

hey thanks! =D

Showing 1-11 of 11