Question

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();
        }
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top