【问题标题】:AS3 Dispatch event not workingAS3 调度事件不起作用
【发布时间】:2014-11-02 22:55:35
【问题描述】:

我真的很困惑。我让代码完美地工作,然后回去尝试了一些东西,现在它不起作用了。我似乎无法弄清楚现在是什么时候,结果我开始感到非常沮丧。我似乎也没有收到任何错误。

我有两个文件 Gameindex 和一个是外部 swf 文件,这是我在 gameindex 顶部导入的关卡。当事件被调用时,我想移动到时间轴上的其他地方,但到目前为止调用事件不起作用。

我在关卡中的代码是这样的:

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

现在在我的游戏索引中,我有以下代码:

import flash.events.Event;

addEventListener("next_level", movetointro);

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

为什么我的 movetointro 功能不起作用?

【问题讨论】:

    标签: actionscript-3 flash dispatchevent


    【解决方案1】:

    你可以试试这个(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))
                }
            }
    
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 2011-11-29
      • 2016-10-04
      • 1970-01-01
      • 2014-10-27
      • 2011-03-27
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多