|
-
[RESOLVED] How do I Add Lives to a Game?
Hi there, I'm creating a flash game, and would like to know if anyone knew how to add lives?
Basically a level is run in a single frame, and images of hearts will be in the corner. When all the lives are lost, the game must go to a different frame (e.g. the menu). I would like to have 10 lives, and whenever a life is lost, a heart to disappear, but the frame cannot change.
Can anyone help please?
-
Prid - Outing
You have to declare a variable to store the lives in, which are just numbers, and decrease that variable by one when you lose a life. In the code, where it makes you lose a life, check if the life variable is equals to 0, and if it is, go to another frame 
If you didn't understand anything, please post the FLA file, and I'll help you
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Thanks! And thanks for helping me in my other thread, too 
Is there a way I can set the variable to remove a movie clip - namely a heart - each time the number of lives is reduced?
-
Prid - Outing
Yup, in the code where you decrease the life variable by one, use this code:
Actionscript Code:
unloadMovie(_root["heart"+life_var]);
Suppose life_var is the variable for the lives, and you have 10 hearts, each with their own instance name, like heart1, heart2, etc. _root[] will find movieclip, variable, or anything else which has the name of the parts inside the square brackets put together. Let's say life_var is equals to 5, meaning that we've lost five lives. Then, when losing another life, life_var will be decreased by one, making it equals to 4, and then the movieclip which will be unloaded, will be, _root["heart"+life_var], which now would make up, heart4 - so the movieclip with the instance name, heart4, will be unloaded, aka, deleted. This is to prevent you from writing 10 alike codes, just to remove each individual heart. If you don't understand, just say so, and I'll make you an example 
and No problem
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Thank you for the reply, that was perfectly worded! I've got it to work partially - when the character dies for the first time, the first heart is removed (as it should be). However, every subsequent time the character dies, hearts aren't removed, and when he dies the amount of times equal to how many lives he starts with, nothing happens. I killed him practically a million times in a row, and nothing happened 
Am I missing something? It's as if the game doesn't remember how many lives have been lost, and resets the lives total each time the character dies.
This is the variable I created:
Actionscript Code:
var life_var: Number = 9;
And this is what happens when the character dies:
Actionscript Code:
life_var -= 1; unloadMovie(_root["heart"+life_var]); if(_root.life_var = 0){ gotoAndStop(2);
I made the life_var equal to nine because there are eight lives, so when one is lost, it totals 8 - removing 'heart8'.
-
Prid - Outing
RED ALERT - A CRITICAL ERROR HAS OCCURED!
You're not comparing life_var to 0, but rather setting the value of life_var to 0. The correct syntax should be this:
Code:
if(_root.life_var == 0){
anyways, that's not the problem, but I don't know it either. You can help by providing your current FLA file
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
I put that code into it, and it worked! No idea why. Or maybe it was just me reloading Flash...
Thanks, and merry christmas
-
Prid - Outing
Great, and no problem
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|