0

I'm really confused here. I get the code to work flawlessly and then went back and tried something and now it won't work. I can't seem to figure out when now and I'm starting to be really frustrated as a result. I also don't seem to get any errors handed to me.

I got two files Gameindex and one that is an external swf which is my level imported on top of the gameindex. I want to move to somewhere else on the timeline when the event is called but as of now calling the event isn't working.

My code in the level is as such:

private function enterFrameHandler(event:Event):void
    {
        updatePosition();
        updateRotation();

        scorebox.text=String(score);

        var t:Number = Math.random();
        if(t>0.98) {
            addasteroid();
        }

        for (var a:int = 0;a<allast.length;a++) {

          if (contains(allast[a])) {

            for (var b:int = 0;b<allbullets.length;b++) {

                if (allbullets[b].hitTestObject(allast[a])) {
                    //removeChild(allbullet[b]);
                    if (contains(allast[a])) {
                    removeChild(allast[a]);
                    }
                    //score     
                    score+=5;

                    if (score==20) {
                        dispatchEvent(new Event("next_level"));
                    }

                }
                if (allast[a].hitTestObject(_player)) {
                    //removeChild(allbullet[b]);
                    trace("hitplayer");
                    endGame();  
                }


            }

            allast[a].y += 2;

          }
        }

    }//function

Now in my game index I have the fallowing code:

import flash.events.Event;

addEventListener("next_level", movetointro);

   function movetointro(e:Event){
     trace("IT IS WORKING");}

Why don't me movetointro function work?

1 Answers1

0

You can try this ( working 100% ) :

package  {

    import flash.display.MovieClip;

    public class Game extends MovieClip {   

        import flash.events.*

        private var score:Number
        private static const event_name:String = 'my_event_name'
        private static const min_score_to_go:Number = 20

        public function Game() {
            score = 0
            addEventListener(event_name, goto_next_level)
        }

        public function score_up(){
            score ++
            verify_score()
        }

        public function score_down(){
            score --
            verify_score()
        }

        private function goto_next_level(e:Event){
            trace('OK, I can go to next level')
            removeEventListener(event_name, goto_next_level)
        }

        private function verify_score(){
            trace('score : '+score)
            if(score == min_score_to_go){
                dispatchEvent(new Event(event_name))
            }
        }

    }   
}
akmozo
  • 9,829
  • 3
  • 28
  • 44