制作平台的游戏,需要方法停止运行 [英] Making platform game, need method to stop running
问题描述
我正在闪光灯一个平台的游戏。
I am making a platform game in flash.
我有一个目标类(包含code为目标精灵,就是当你打它的类,它继续游戏的下一部分)。
I have a goal class(the class which contains code for the goal sprite, where when you hit it, it continues to next part of game).
在目标构造,2事件侦听器的添加,有如下几点:
Inside the goal constructor, 2 event listeners are added, they are as follows:
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, eFrame);
该beginClass功能是好的,并且只运行一次,但eFrame是检查,如果玩家已经命中目标,所以它不停地运转。问题是,一旦玩家击中目标,eFrame继续运行,而在一个菜单描述下一个场景到播放器。我eFrame功能如下。
The beginClass function is fine, and only runs once, but eFrame is what checks if the player has hit the goal, so it is constantly running. The problem is, once the player hits the goal, eFrame continues to run, while in a menu describing the next scene to the player. My eFrame function is below.
private function eFrame(event:Event):void{
if(hitTestObject(_root.mcMain)){
var lastScore:int = _root.mainScore;
_root.mainScore = lastScore;
while (_root.lvlHolder.numChildren > 0) {
_root.lvlHolder.removeChildAt(0);
}
_root.mcMain.removeChildAt(0);
_root.isInCut = true;
if (_root.lvlCurrent == 1) {
_root.gotoAndStop(2);
} else if (_root.lvlCurrent == 2) {
_root.gotoAndStop(3);
} else if (_root.lvlCurrent == 3) {
_root.gotoAndStop(4);
}
}
}
框架2,3,4,是显示一个消息到播放器只是文本和一个按钮图像,然后播放器命中继续。我的问题是eFrame仍试图运行,但类没有被实例化,方法是造成滞后的极端金额一旦玩家继续。
Frames 2, 3, 4, are frames with just text and a button that display a message to the player, and then the player hits continue. My problem is that eFrame is still trying to be run, but the class has not been instantiated, and the method is causing extreme amounts of lag once the player continues.
推荐答案
内的目标,什么是_root点?
Inside Goal, what's the point of _root?
反正这里是我做了什么:
更改事件 ADDED
到 ADDED_TO_STAGE
,这样一来,当事件被触发,我们知道这个雪碧有一个期
属性。
Anyway here's what I've done:
Change the event ADDED
to ADDED_TO_STAGE
, that way, when the event is fired we know this Sprite has a stage
property.
addEventListener(Event.ADDED_TO_STAGE, beginClass);
删除从构造函数中的 eFrame
事件。将它添加到 beginClass
,与期
,像这样:
Remove the eFrame
event from the constructor. Add it to beginClass
, with stage
, like so:
stage.addEventListener(Event.ENTER_FRAME, eFrame);
现在在 eFrame
,可以赫然删除与舞台参照该事件。它没有早点工作,因为引用错了(不管它是与 _root
变量)。
Now in eFrame
, you can awesomely remove the event with the stage reference. It didn't work earlier because the reference was wrong (whatever it was with the _root
variable).
stage.removeEventListener(Event.ENTER_FRAME, eFrame);
但 - 记得这部分的code之前做到这一点:
BUT - remember to do it before this part of your code:
while (_root.lvlHolder.numChildren > 0) {
_root.lvlHolder.removeChildAt(0);
}
因为当精灵被删除,它不会有期
属性了。只记得清理所有可能的方案活动。我不能完全肯定期
是正确的地方,将您进入帧事件,我只是认为,这样因为你前面所谓的_root。
because when the sprite is removed, it won't have the stage
property anymore. Just remember to clean up events in all possible scenarios. I'm not entirely sure stage
is the right place to place your enter frame event, I just assumed so because of what you earlier called _root.
这篇关于制作平台的游戏,需要方法停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!