0

When does ENTER_FRAME stops?
1. removeEventListener(Event.ENTER_FRAME,abc);
2. error occurs or the flash crashes
3. the instance of class is removed from stage
4. ?

The story:
I have several AS document for a game,one of it contains ENTER_FRAME which adding enemies.
It works fine usually,but sometimes it don't summon enemies anymore. I didn't change anything,I have just pressed Ctrl+enter to test again.
I have used trace to check, and found the ENTER_FRAME stops.
Otherwise, I put trace into another AS file of ENTER_FRAME, it keeps running.
Another ENTER_FRAME in levelmanage class for testing if it's working, both of it and addEventListener(Event.ENTER_FRAME, process); stops too I also don't get any errors, and I can still move my object via keys.
The levelmange class doesn't connect to any object,it shouldn't stop if anything on stage has removed.
what could be the problem?

The below as code is the one who stops operating.

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.utils.*;
    public class levelmanage extends MovieClip
    {
        var testing:int=0
        private var sttage = ninelifes.main;
        public var framerate = ninelifes.main.stage.frameRate;
        public var levelprocess:Number = 0;
        public var levelprocesss:Number = 0;
        private var level:int;
        public var randomn:Number;
        public function levelmanage(levell:int)
        {
            level = levell;
            addEventListener(Event.ENTER_FRAME, process);
        }
        function process(e:Event)
        {
            testing+=1
            if(testing>200){
                testing=0
                trace("working")//it don't trace "working"sometimes which means enterframe doesn't going
            }
            if (levelprocess>levelprocesss)trace(levelprocess);
            levelprocesss = levelprocess;
            if (levelprocess>=100 && enemy.enemylist.length==0)
            {
                finish();
                return();
            }
            if (levelprocess<=100 && enemy.enemylist.length<6)
            {
                switch (level)
                {
                    case 1 :
                        arrange("cir",0.5);
                        arrange("oblong",1);
                        break;
                }
            }
            }
        public function arrange(enemyname:String,frequency:Number)
        {
            randomn = Math.random();
            if (randomn<1/frequency/framerate)
            {
                var theclass:Class = Class(getDefinitionByName(enemyname));
                var abcd:*=new theclass();
                sttage.addChild(abcd);
                trace("enemyadded")
                switch (enemyname)
                {
                    case "cir" :
                        levelprocess +=  5;
                        break;
                    case "oblong" :
                        levelprocess +=  8;
                        break;
                }
            }
        }
        private function finish()
        {
            levelprocess=0
            trace("finish!");
            removeEventListener(Event.ENTER_FRAME,process);//not this one's fault,"finish" does not appear.
            sttage.restart();
        }
    }
}
sbk201
  • 168
  • 2
  • 8

2 Answers2

1

It's possible you eventually hit "levelprocess==100" and "enemy.enemylist.length==0" condition, which causes your level to both finish and have a chance to spawn more enemies, which is apparently an abnormal condition. It's possible that this is the cause of your error, although unlucky. A quick fix of that will be inserting a "return;" right after calling finish(). It's also possible that your "levelmanage" object gets removed from stage somehow, and stops receiving enter frame events, which might get triggered by a single object throwing two "sttage.restart()" calls. Check if this condition is true at any time in your "process" function, and check correlation with this behavior. And by all means eliminate possible occurrence of such a condition.

Vesper
  • 18,599
  • 6
  • 39
  • 61
  • thx for help,the main problem isn't solved yet I have put `return` after `finish()`, it prevent enemy appears after `finish()`; The levelmange class doesn't connect to any object,it shouldn't stop if anything on stage has removed. And`sttage.restart()` is checked, there is only this command to call `restart()`. Also,I have put another `ENTER_FRAME` in levelmanage class,this and the original one both stop for unknown reason. – sbk201 Sep 03 '12 at 06:28
  • Perhaps try to change the attachment point of your listener? You have `addEventListener(Event.ENTER_FRAME, process);` try `sttage.addEventListener(Event.ENTER_FRAME, process);` instead. The reason is, your `levelmanage` instance might not get added to the stage. Also check if you are controlling each and every of your listeners across the entire project, perhaps you've done something that can mess up Flash's event system. I expect the culprit is outside of this code. – Vesper Sep 03 '12 at 08:37
  • It seems that `sttage.addEventListener(Event.ENTER_FRAME, process);` works fine ALWAYS in 20 times of testing,I didn't encounter stop anymore, even if it has little other problem(ArgumentError: Error #2025). But finally `sttage.stage.addEventListener(Event.ENTER_FRAME, process);` works completely great. cheer – sbk201 Sep 03 '12 at 14:57
0
if(testing>200){
                testing=0
                trace("working")//it don't trace "working"sometimes which means enterframe doesn't going
            }

The deduction in your comment isn't correct. It means either EnterFrame isn't firing, or testing <= 200.

Try putting a trace right at the beginning of your process function.

If you don't have removeEventListener elsewhere, then it is unlikely EnterFrame is really stopping - it's hard to be sure from the example you've posted but I would bet the problem is somewhere in your logic, not an issue with the EnterFrame event itself.

Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
  • thank for the aid. I type `if(testing>200)` for reason,hence that the `trace()` shows message in a period of time. May be you are right,it's not the problem on `ENTER_FRAME`itself. – sbk201 Sep 03 '12 at 14:53