PDA

View Full Version : Platform Game - Health



GregH501
July 11th, 2007, 05:13 PM
Hi. I am making a Flash Platform game. I have managed everything so far. But am working on health. I have a health bar with 5 pieces to it...
I would like to make it so when you get hit by an enemy, for one (or two, depending on the enemy) health pieces to be lost...
I have tried using gotoAndPlay, but this cannot be right, as there will be many enemies, and it will not work.
I have used the unloadMovie and the gotoAndPlay command...But I thought the command would need to be to look up how much life is left, and take off depending on the enemy...
I've used this code on the man movie clip:

if (hitTest(_root.enemy)==true){
tellTarget(_root.healthbar){
unloadMovie("health5");
}
}

this is good, but doesn't help when coming to multiple enemies...

or this:


if (hitTest(_root.enemy2)==true){
tellTarget(_root.health){
_root.health.gotoAndPlay("two")
}
}

And tried this:


if (hitTest(_root.enemy && _root.enemy2)==true){
tellTarget(_root.health){
_root.health.gotoAndPlay("three")
}
}


Any help! Please???

jose3760
July 12th, 2007, 09:36 PM
try making each of the 5 health squares their own move clip, and give each one an instance of "health1" through 5. On the keyframe, have a code something like:
_root.health = 5;


on square 1, you should have this code:

onClipEvent(enterFrame){
if(_root.health == 1){
_root.health1._visible = true;
_root.health2._visible = false;
_root.health3._visible = false;
_root.health4._visible = false;
_root.health5._visible = false;
}
}

on square 2:

onClipEvent(enterFrame){
if(_root.health == 2){
_root.health1._visible = true;
_root.health2._visible = true;
_root.health3._visible = false;
_root.health4._visible = false;
_root.health5._visible = false;
}
}

on square 3:

onClipEvent(enterFrame){
if(_root.health == 3){
_root.health1._visible = true;
_root.health2._visible = true;
_root.health3._visible = true;
_root.health4._visible = false;
_root.health5._visible = false;
}
}

and finally, square 4:

onClipEvent(enterFrame){
if(_root.health == 4){
_root.health1._visible = true;
_root.health2._visible = true;
_root.health3._visible = true;
_root.health4._visible = true;
_root.health5._visible = false;
}
}

on square 5 you should have this code:

onClipEvent(enterFrame){
if(_root.health == 5){
_root.health1._visible = true;
_root.health2._visible = true;
_root.health3._visible = true;
_root.health4._visible = true;
_root.health5._visible = true;
}
}

Also, on the enemy, for what ever his attack function is, add:

_root.health -= 1;




Hope it works!

Byte
July 12th, 2007, 10:35 PM
Actually, an easier way would be to make the health MC with 6 frames. 1st frame has no health bars. And then add one every next frame. Then add this code on the onEnterFrame of your character



if(_root.healthMC._currentFrame !=1){
_root.healthMC.gotoAndStop(_currentFrame - 1)
_root.health = _root.healthMC._currentFrame - 1
}else{
//You are dead actions
}


So much easier in my opinion.

GregH501
July 13th, 2007, 07:42 AM
Actually, an easier way would be to make the health MC with 6 frames. 1st frame has no health bars. And then add one every next frame. Then add this code on the onEnterFrame of your character



if(_root.healthMC._currentFrame !=1){
_root.healthMC.gotoAndStop(_currentFrame - 1)
_root.health = _root.healthMC._currentFrame - 1
}else{
//You are dead actions
}
So much easier in my opinion.

Sounds good, but what code do you use on the enemy?

jose3760
July 13th, 2007, 01:22 PM
True, I never though of it that way ^^]

Byte
July 13th, 2007, 10:28 PM
Sounds good, but what code do you use on the enemy?

Put this code in the main timeline (if you have an actions layer, put it somewhere there)


function loseHealth(lossAmount){
var health:Number
if(health > 0){
health -= lossAmount
_root.healthMC.gotoAndStop(health+1)
}else{
//dead actions
}
}
This creates a litte function called loseHealth which you can call every time you want your character to get damaged. Also, it has a property, called lossAmount. This allows you to lose health and specify the amount in one quick function call. Thus, calling "loseHealth(2)" would drop your player's health by 2. So thats the kind of code you would put on your enemy.

Ignore that code I wrote before, its wrong. It would just decrease the player's health until it was zero. Also a little confusing.

Also, the method used here (declaring a variable for health) allows you to change the amount of health. Its also alot simpler and more efficient for flash to use.

Any more questions just ask :)

GregH501
July 14th, 2007, 07:27 AM
Hmm...interesting...But it doesn't work...Firstly, the health MC keeps on cycling through the frames...
Secondly, it just overall doesn't work, I put the code in, and it does nothing...

Byte
July 14th, 2007, 11:56 AM
Well, make sure your healthMC has a stop action on every frame. Or at the very least, the first frame.

Also, the code only defines a function. You have to call it to make it work. I also generalized when I used _root.healthMC. Make sure the instance name for your health bar is "healthMC" with no quotation marks.

To call the function just write loseHealth("numberOfLostHealthGoesHere"). So if you wanted your character to lose health when he gets clicked on (as an example):



_root.character.onRelease = loseHealth(1)

GregH501
July 14th, 2007, 03:39 PM
Sorry! But I have done exactly what you've said, and it says undefined, whenever I try and trace the function, so something must be wrong. I've called the function in the enemy movie clip, but still says undefined...

Byte
July 15th, 2007, 01:27 PM
Well tracing only returns the value of something. Trying to trace a function wouldnt work because its a function and doesnt have a value. A variable holds value. If you traced "loseAmount" from within the function, then it would return the loseAmount given when you called the function.

If this doesnt help, try posting your .fla. I use Flash 8.

GregH501
July 15th, 2007, 02:28 PM
Ok, have a look please
http://www.zshare.net/download/27030788d890a1/

Sorry for inconvenience!!

jose3760
July 15th, 2007, 11:39 PM
my method may be longer, but it still gets the job done without any hastle =]

Byte
July 16th, 2007, 12:49 PM
my method may be longer, but it still gets the job done without any hastle =]

Yeah but I'm a stickler for efficient coding practices :p

Also, I figured out what the problem was with the game. Your hittests were taking place on the movieclip, so it was searching for a function on the movieclip, not the timeline. Thus, I added some _root.'s to function calls and variables and now it works!

Basically, I rewrote the function a little to make it gotoAndStop() properly, and also defined _root.health in the 6th frame on the health bar (so when the game starts it automatically defines once properly).

Anyway, I would like to note that your hitTest runs 24 times every second, thus making any touch to an enemy instantly fatal. I suggest you add some functionality to make the player temporarily invincible after being damaged, so they only take the hit once.

Anyway, hope I helped, good luck with your game :)

GregH501
July 22nd, 2007, 06:15 PM
Wow! Cheers! I forgot the _root's lol! Right, this invicible thing, its really annoying, I've been trying to find other games that do it, but cos my game has different states for running and jumping, it doesn't work, so any tips?
I tried this:

if(warmedup==undefined){
gotoAndPlay("warmup");
}
and then, link to the frames with warmup, and have warmedup=1; at the end...
Could anyone help? Please?