Be a Supporter!

how to "game over" AS3

  • 2,983 Views
  • 15 Replies
New Topic Respond to this Topic
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
how to "game over" AS3 2010-12-12 19:27:55 Reply

so i got a hame working.
everything is fine but it starts as soon as i open it.

i want to make it so the player has to chose "start game" to begin.

also i got a "life" variable which starts at 100 and goes down to -1 everytime i get hit.
i want the game to be over when it reaches 0.

How do i stop the whole game? the listeners sounds..everything.


/Siggy sig

BBS Signature
Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to how to "game over" AS3 2010-12-12 19:57:59 Reply

addEventListener // starts the listener
removeEventListener // remove the listener

Also be sure to describes more your post....

FethrdWlf
FethrdWlf
  • Member since: Jul. 17, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to how to "game over" AS3 2010-12-12 20:53:08 Reply

Watcha' do is you make a button that has what you want it to look like, and you put it one frame before everything else. Then, on the same frame as the button you just made (But on a different layer), put this code:

stop();

That will make the Flash file, literally, stop before it goes past that frame. Then, assuming you gave you "Start Game" button the instance name of "start_btn", you would put this code right below "stop();

start_btn.addEventListener(MouseEvent.MOUSE_UP, startgame);
function startgame(e:MouseEvent):void{
play();
}

That will then make it so that when you click you button, and release it, the Flash file will go to the next frame where your game is. I'm assuming that you're an amateur at Flash (Sorry if you're not, and no offense anyways), so that is why it's so simple.


Don't you love the Blankness?....

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to how to "game over" AS3 2010-12-12 20:59:16 Reply

At 12/12/10 07:27 PM, nconde25 wrote: so i got a hame working.
everything is fine but it starts as soon as i open it.

i want to make it so the player has to chose "start game" to begin.

also i got a "life" variable which starts at 100 and goes down to -1 everytime i get hit.
i want the game to be over when it reaches 0.

How do i stop the whole game? the listeners sounds..everything.

please tell me you didnt google an fla file and changed the graphics in it to what you wanted?


BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-12 21:55:34 Reply

At 12/12/10 08:53 PM, FethrdWlf wrote: Watcha' do is you make a button that has what you want it to look like, and you put it one frame before everything else. Then, on the same frame as the button you just made (But on a different layer), put this code:

stop();

That will make the Flash file, literally, stop before it goes past that frame. Then, assuming you gave you "Start Game" button the instance name of "start_btn", you would put this code right below "stop();

start_btn.addEventListener(MouseEvent.MO USE_UP, startgame);
function startgame(e:MouseEvent):void{
play();
}

That will then make it so that when you click you button, and release it, the Flash file will go to the next frame where your game is. I'm assuming that you're an amateur at Flash (Sorry if you're not, and no offense anyways), so that is why it's so simple.

I've tried that. it doesn't stop the game from starting.
I've also tried placing a NG preloader on the first frame but the game still runs above it.
I think it's because the game it's not on the timeline but separated .as files.

At 12/12/10 08:59 PM, GodlyKira wrote:
At 12/12/10 07:27 PM, nconde25 wrote: so i got a hame working.
everything is fine but it starts as soon as i open it.

i want to make it so the player has to chose "start game" to begin.

also i got a "life" variable which starts at 100 and goes down to -1 everytime i get hit.
i want the game to be over when it reaches 0.

How do i stop the whole game? the listeners sounds..everything.
please tell me you didnt google an fla file and changed the graphics in it to what you wanted?

No. I'm following tutorials to learn.
For this game i've used this tutorial.
http://asgamer.com/2009/flash-game-desig n-basics-adding-library-objects-to-stage -with-as3

it took me like a week to finish it. i did it step by step.
i work like all day so i did it little by little.
There's no point if i just copy a code i don't understand.

here's what i did with that tutorial.
http://www.newgrounds.com/dump/item/c5ce a780a997f5ac54064d5cc60dfbd8

i did everything i could to make it look like i wanted it be.
if i could just code the whole game from scratch i would. but i can't.


/Siggy sig

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to how to "game over" AS3 2010-12-13 13:49:51 Reply

Oh thank god, and ive actually read that tutorial oddly enough :P but it was awhile back, i actually developed my own game idea from that, but it is much different. good job man, i played it for a good five minutes :P anyway

add to vars

var sKills:Number;
var sPoints:Number;

add to health deduction function

if (health <= 0 && lives <=0) {
    sKills = kills;
    sPoints = points;
    die();
}

add function anywhere

function die();
    kills = 0;
    points = 0;
    health = 100;
    lives = 10;
    //basically reset all game values besides sKills and sPoints
    gotoAndStop("gameover");
}

and finally on game over screen add a dynamic text box call it gamemsg or whatever

gamemsg.text = "Game Over" + "\" +sKills.toString()+"\"+sPoints.toString;

BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-14 19:03:28 Reply

thankyou GodlyKira that works, partially.

while the game goes to the frame named "gameover" once i die.
it doesn't stop the game.
it's like a background change. i mean the game runs above it, i can still move my ship and the enemies keep coming.

Spysociety said above i had to remove the listeners. would that make the game stop?
i've tried removeeventlistener but i'ts giving me errors.
i don't know which listener to remove(engine's loop i guess) and how to do it.

we are getting closer but something is missing :D


/Siggy sig

BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-14 19:49:40 Reply

UPDATE:

removeEventListener("enterFrame", loop);

that makes the enemies stop coming, they don't instantly dissapear, you have to wait for them to go offstage.
that's ok i guess.
i can still move the ship and shoot thou.

lol this never ends.


/Siggy sig

BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-14 22:06:49 Reply

i got rid of the ship as well!
i've made a function inside my Ship class.

function die() : void
{
			gotoAndPlay("dead"); 
			explo.play();
			removeEventListener("enterFrame", loop); 
			if (stage.contains(this))
				stage.removeChild(this);
}

<bold>thanks for all guys i think i'm finally learning some as3!</bold>


/Siggy sig

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to how to "game over" AS3 2010-12-15 01:05:33 Reply

your also going to want to remove all the player listeners Keys Down Keys up, and right and init function at the start that gets called on game start to reinitialize and properly set all variables, basically similar to die, but glad to hear you got it figured out for the most part


BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-15 20:46:11 Reply

At 12/15/10 01:05 AM, GodlyKira wrote: your also going to want to remove all the player listeners Keys Down Keys up, and right and init function at the start that gets called on game start to reinitialize and properly set all variables, basically similar to die, but glad to hear you got it figured out for the most part

The function that manages keys pressed it's "loop" inside "ship" class.
I stopped that listener on my die function. (if that's what you meant).

My game starts as soon as it loads, I haven't yet done a function for play/replay.. that's something i have to figure out i think it'll be the oposite to my die funtion. hopefully with some trial/error I will manage to make it.


/Siggy sig

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to how to "game over" AS3 2010-12-15 21:21:43 Reply

Notice how in die I told you to reset all game start values? that was for easier replaying, however, you can make a new function

init();

function init() {
//move all listeners, and call your player constructor class in this function
//set variables in this function basically everything the game needs to run (not other functions ofcourse)
}

BBS Signature
nconde25
nconde25
  • Member since: May. 29, 2010
  • Offline.
Forum Stats
Member
Level 23
Animator
Response to how to "game over" AS3 2010-12-16 20:37:25 Reply

At 12/15/10 09:21 PM, GodlyKira wrote: Notice how in die I told you to reset all game start values? that was for easier replaying, however, you can make a new function

init();

function init() {
//move all listeners, and call your player constructor class in this function
//set variables in this function basically everything the game needs to run (not other functions ofcourse)
}

Excellent! it works just perfect.
Before, the engine started the game but now i've placed a listener to a "play" button which calls an "init" function, that function it's just the engine.. but now you gotta click the button to run it. =D

God i couldn't ask for more! thank you very much!
I could never figure this out by myself, you should be a teacher!

P.S: when you told me i copied the fla. i felt sad lol, because my knowledge it's limitated, but you took the time to explain these things to me and the fact i managed to understand them and make them work makes me happy now. thanks!!!


/Siggy sig

BBS Signature
GodlyKira
GodlyKira
  • Member since: Dec. 21, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to how to "game over" AS3 2010-12-16 20:41:13 Reply

no reasons to feel bad, we were all beginners at one point, and im glad i could be of help, and the fact you understood what I was saying shows potential... a lot of kids come on here expecting to be able to do great things but dont want to put in effort to know and understand, I wish you best of luck man


BBS Signature
lecramstar
lecramstar
  • Member since: May. 20, 2011
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to how to "game over" AS3 2011-05-20 07:48:01 Reply

At 12/13/10 01:49 PM, GodlyKira wrote: Oh thank god, and ive actually read that tutorial oddly enough :P but it was awhile back, i actually developed my own game idea from that, but it is much different. good job man, i played it for a good five minutes :P anyway

add to vars

var sKills:Number;
var sPoints:Number;

add to health deduction function

if (health <= 0 && lives <=0) {
sKills = kills;
sPoints = points;
die();
}

add function anywhere

function die();
kills = 0;
points = 0;
health = 100;
lives = 10;
//basically reset all game values besides sKills and sPoints
gotoAndStop("gameover");
}

and finally on game over screen add a dynamic text box call it gamemsg or whatever

gamemsg.text = "Game Over" + "\" +sKills.toString()+"\"+sPoints.toString;

Hi guys.

I've followed the same turtorial as nconde25.
I'm stuck at the part where you say:

if (health <= 0)

I'm getting errors from that part.
I use the same vars etc as the turtorials, my health is called s_hp.
I want it to trace "gameover" when my s_hp is equal to 0.
Does anyone know how to code this?

Thanks in advance :D

lecramstar
lecramstar
  • Member since: May. 20, 2011
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to how to "game over" AS3 2011-05-20 08:53:57 Reply

Nevermind :)

Figured it out myself.