EventDispatcher between an as and an fla?
I am making a fighting game in Flash and while I have everything running, I am missing something: a victory/loss screen. Logically, I know how to do it:
if character.hp < 0
{
character.dead = true;
dispatchevent("death", event)
}
My problem is that I have no idea as to how to code it. I know I will use two classes and my two .fla files (unless I am wrong).
I have two .fla files that are in play here: the Menu.fla
file and the Arena.fla
file. Menu.fla
contains the entire navigation of the game, options, character selection screens, etc. and when it is time for the player to engage in battle, it loads the Arena.fla
file, which contains on开发者_如何学运维ly the backgrounds (depending on the selected stage) and for now is set to a length of one frame only. For Arena.fla
, the real action happens in my classes, but logically, I would only need HP.as
and Character.as
.
In Character.as
, I have declared the following variable:
var isDead:Boolean = false; //is character dead?
In HP.as
, believe I should have the following:
if(currentHp<0)
{
currentHp = 0;
character.isDead = true; //declared as var `character:Object;`
EventDispatcher.dispatchEventListener("playerDead", playerDead);
}
And finally, in Arena.fla
, I want to be able to detect the above-mentioned eventlistener and simply move on to a second frame which will display a message in the style of "PLAYER ONE HAS WON" or "PLAYER ONE HAS LOST" with a button that will allow me to go back to the character selection screen. This is the first part in which I am stuck: how do I detect the dispatched event listener in my main .fla file?
Secondly, if the player clicks on the "CONTINUE" button, which displays regardless if the player has won or lost, how can my Menu.fla
(which loads the Arena.swf
) detect this click event, unload the game, and go back to the character selection screen?
Thank you in advance for helping me out. I realize this is a lot of text but it's the most descriptive I can be. If you have any questions or need any clarification concerning my question, feel free to speak up.
-Christopher
I'm not sure about the code you have to read the HP but do you know that character.dead
is actually becoming true?
You could always have the Arena.swf
call a function in the HP.as that will end the game and declare a winner.You could add a second Frame to Arena.swf that contains a dimmed background and a WINNER or LOSER text.'
In general, the easiest way for a user-defined class to gain event dispatching capabilities is to extend EventDispatcher. If this is impossible (that is, if the class is already extending another class), you can instead implement the IEventDispatcher interface, create an EventDispatcher member, and write simple hooks to route calls into the aggregated EventDispatcher.
activate Dispatched when Flash Player or an AIR application gains operating system focus and becomes active.
deactivate Dispatched when Flash Player or an AIR application loses operating system focus and is becoming inactive.
Event dispatcher
Thank you all for your help, but I have figured it out. Turns out my method was far too complicated for what I wanted to do, and for the time I had left. I will explain how I did it.
Instead of using an EventDispatcher
like I thought I would, I used a SharedObject
, which simply made everything work like magic.
A SharedObject
can be accessed from anywhere in the application/game, as long as it is referred to it correctly. So I simply created a SharedObject
called "winLossData" set to "NO WINNERS" in my character selection screen. This cookie is never saved nor written to the disk, so there's no chance for the user to find it (generally speaking).
I have decided to use the Movement.as
class which contains all of my controls and wrote an event listener of type Event.ENTER_FRAME
that checks constantly my characters' health status. If one of them is below 100, my SharedObject
immediately takes for value either "PLAYER ONE" or "PLAYER TWO", depending on who won (i.e. whose health points are not under 100). Afterward, just for precaution, I reset the losing character's health points to 100. Here's the code:
function whoWon(event:Event):void
{
if(playerSpriteBar.getPower() <= 0)
{
winner.data.winner = "Player Two";
playerSpriteBar.update(100);
}
if(playerAIBar.getPower() <= 0)
{
winner.data.winner = "Player One";
playerAIBar.update(100);
}
}
In my Menu.fla
, I have another event listener of type Event.ENTER_FRAME
that waits for the cookie to change value. As soon as the cookie changes values, Menu.fla
automatically unloads the external swf (in our case, Arena.swf
) and displays the results, accordingly to the received SharedObject
. The rest of the actions happen inside the Menu.fla
file, so no need for any extra coding.
Once again, thank you all for your help.
精彩评论