[AS3]DispatchEvent的古怪問題

來源:互聯網
上載者:User

如下面代碼:

package {

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.EventDispatcher;

 

    public
class TestAction extends Sprite

    {

        private
var _dispatcher:EventDispatcher;

        

        public
function TestAction()

        {

            _dispatcher = new EventDispatcher();

            

            addEventListener(Event.ENTER_FRAME,__enterFrame);

            

            _dispatcher.addEventListener(TestEvent.TEST,__test);

        }

        

        private
var e:TestEvent = new TestEvent(TestEvent.TEST);

        

        private
function __enterFrame(event:Event):void

        {

            _dispatcher.dispatchEvent(e);

        }

        

        private
function __test(event:TestEvent):void

        {

            trace(event);

        }

    }

}

 

此代碼會報如下錯誤:

TypeError: Error #1034: 強制轉換類型失敗:無法將 flash.events::Event@1967b21 轉換為 TestEvent。

    at flash.events::EventDispatcher/dispatchEventFunction()

    at flash.events::EventDispatcher/dispatchEvent()

    at TestAction/__enterFrame()[E:\Flash\TestAction\TestAction.as:23]

 

原因 變數 e在dispatch後再調用第二此dispatch就會報如下錯誤,由此可見,如果Event如果已經dispatch,調用_dispatcher.dispatchEvent(e),_dispatcher會new 一個Event對象進行發布。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.