首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript + Class + createjs.EventDispatcher

TypeScript + Class + createjs.EventDispatcher
EN

Stack Overflow用户
提问于 2015-11-19 01:25:01
回答 1查看 1.1K关注 0票数 1

我正在尝试使用createjs EventDispatcher作为来自类的dispatchEvents的一种方式。我正在使用createjs.EventDispatcher扩展类,并使用dispatchEvent来触发事件。

在执行这一行this.dispatchEvent(createJSEvent);时,我会得到以下错误:

Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null.

简化了TypeScript代码以演示我想做的事情:

代码语言:javascript
复制
    export class deviceOrientation extends createjs.EventDispatcher {
        constructor() {
            super();
            // wait 2 seconds and then fire testDispatch
            setTimeout(this.testDispatch(), 2000);
        }
        testDispatch():void {
            var createJSEvent:createjs.Event = new createjs.Event("change", true, true);
            this.dispatchEvent(createJSEvent);          
          }
    }

    // This is the starting function
    export function appExternalModuleTest(): void {
        let _deviceOrientation: deviceOrientation;
        _deviceOrientation = new deviceOrientation();
        _deviceOrientation.addEventListener("change", () => this.changeOrientation());
        //_deviceOrientation.on("progress", () => this.changeOrientation());
    }

    export function changeOrientationi(event: Event): void {
        console.log('orienationHasChanged ');
    }

我用的是easeljs-0.8.1.min.js

我不确定CreateJS是否能做到这一点。有没有更好的方法?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-19 06:20:49

这个问题看起来很奇怪,因为我在我的项目中做的几乎一样,没有任何问题。

简而言之,我有一个用于createjs类声明的d.ts文件,并且在我的“普通”类型记录类中使用这些声明。

例如:

d.ts:

代码语言:javascript
复制
declare module createjs
{
    export class EventDispatcher
    {
        addEventListener(type: string, listener: any, useCapture?: boolean): void;

        removeEventListener(type: string, listener: any, useCapture?: boolean): void;
        removeAllEventListener(type?: string): void;

        dispatchEvent(event: Event): boolean;
    }

    export class Event
    {
        public type: string;

        public target: any;
        public currentTarget: any;

        constructor(type: string, bubbling?: boolean, cancelable?: boolean);

        clone(): Event;
    }
}

普通班:

代码语言:javascript
复制
module flashist
{
    export class TestEventDispatcher extends createjs.EventDispatcher
    {
        public constructor()
        {
            super();
        }

        public testDispatch(): void
        {
            var tempEvent: createjs.Event = new createjs.Event("test");
            this.dispatchEvent(tempEvent);
        }
    }
}

在代码的其他地方,您应该创建一个TestEventDispatcher类的实例。类似于:

代码语言:javascript
复制
this.testDispatcher = new TestEventDispatcher();
this.testDispatcher.addEventListener("test", (event: createjs.Event) => alert("Test Event Listener"));
this.testDispatcher.testDispatch();

我刚刚测试了代码,它对我有用。

我唯一的想法是确保easel.js文件在您的主应用程序文件之前加载。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33793654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档