as 和 fla 之间的 EventDispatcher?
我正在 Flash 中制作一款格斗游戏,虽然一切都在运行,但我缺少一些东西:胜利/失败屏幕。从逻辑上讲,我知道该怎么做:
if character.hp < 0
{
character.dead = true;
dispatchevent("death", event)
}
我的问题是我不知道如何编码。我知道我将使用两个类和两个 .fla 文件(除非我错了)。
我在这里使用两个 .fla 文件:Menu.fla
文件和 Arena.fla
文件。 Menu.fla
包含游戏的整个导航、选项、角色选择屏幕等,当玩家进入战斗时,它会加载 Arena.fla
code> 文件,仅包含背景(取决于所选阶段),目前仅设置为一帧的长度。对于 Arena.fla
,真正的操作发生在我的类中,但从逻辑上讲,我只需要 HP.as
和 Character.as
。
在 Character.as
中,我声明了以下变量:
var isDead:Boolean = false; //is character dead?
在 HP.as
中,相信我应该具有以下内容:
if(currentHp<0)
{
currentHp = 0;
character.isDead = true; //declared as var `character:Object;`
EventDispatcher.dispatchEventListener("playerDead", playerDead);
}
最后,在 Arena.fla
中code>,我希望能够检测到上述事件侦听器,然后简单地移至第二帧,该帧将显示一条“第一玩家已获胜”或“第一玩家已失败”样式的消息,并带有一个按钮,该按钮将请允许我返回角色选择屏幕。这是我陷入困境的第一部分:如何在我的主 .fla 文件中检测分派的事件侦听器?
其次,如果玩家点击“CONTINUE”按钮(无论玩家赢还是输都会显示该按钮),我的 Menu.fla
(加载 Arena.swf
>) 检测到这个点击事件,卸载游戏,然后回到角色选择界面?
预先感谢您帮助我。我意识到这是很多文字,但这是我所能描述的最具有描述性的文字。如果您有任何疑问或需要对我的问题进行任何澄清,请随时发言。
——克里斯托弗
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 only 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您必须阅读 HP 的代码,但您知道
character.dead
实际上正在成为现实吗?您始终可以让
Arena.swf
调用 HP.as 中的一个函数,该函数将结束游戏并宣布获胜者。您可以向 Arena.swf 添加第二个 Frame,其中包含暗淡的背景和赢家或输家文本。一般来说,用户定义的类获得事件调度功能的最简单方法是扩展EventDispatcher。如果这是不可能的(即,如果该类已经扩展了另一个类),您可以改为实现 IEventDispatcher 接口,创建一个 EventDispatcher 成员,并编写简单的挂钩以将调用路由到聚合的 EventDispatcher 中。
激活
当 Flash Player 或 AIR 应用程序获得操作系统焦点并变为活动状态时调度。
停用
当 Flash Player 或 AIR 应用程序失去操作系统焦点并变得不活动时调度。
事件调度程序
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
谢谢大家的帮助,但我已经弄清楚了。事实证明,我的方法对于我想做的事情和我剩下的时间来说太复杂了。我将解释我是如何做到的。
我没有像我想象的那样使用
EventDispatcher
,而是使用SharedObject
,它让一切都像魔法一样工作。只要正确引用,就可以从应用程序/游戏中的任何位置访问
SharedObject
。因此,我只是创建了一个名为“winLossData”的 SharedObject,并在角色选择屏幕中将其设置为“NO WINNERS”。该 cookie 永远不会保存或写入磁盘,因此用户没有机会找到它(一般来说)。我决定使用包含所有控件的
Movement.as
类,并编写一个Event.ENTER_FRAME
类型的事件侦听器来不断检查角色的健康状态。如果其中一个低于 100,我的SharedObject
会立即将“玩家一号”或“玩家二号”作为值,具体取决于谁获胜(即其生命值不 100 以下)。之后,为了预防起见,我将失败角色的生命值重置为 100。代码如下:在我的
Menu.fla
中,我有另一个Event.ENTER_FRAME
类型的事件侦听器等待 cookie 改变值。一旦 cookie 更改值,Menu.fla
就会自动卸载外部 swf(在我们的示例中为Arena.swf
)并根据收到的显示结果>共享对象
。其余操作发生在Menu.fla
文件内,因此不需要任何额外的编码。再次感谢大家的帮助。
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 aSharedObject
, 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 aSharedObject
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 typeEvent.ENTER_FRAME
that checks constantly my characters' health status. If one of them is below 100, mySharedObject
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:In my
Menu.fla
, I have another event listener of typeEvent.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 receivedSharedObject
. The rest of the actions happen inside theMenu.fla
file, so no need for any extra coding.Once again, thank you all for your help.