Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 阶段不使用TypeScript(0.9.1.1)中的window.onresize更新_Javascript_Html_Typescript_Window Resize - Fatal编程技术网

Javascript 阶段不使用TypeScript(0.9.1.1)中的window.onresize更新

Javascript 阶段不使用TypeScript(0.9.1.1)中的window.onresize更新,javascript,html,typescript,window-resize,Javascript,Html,Typescript,Window Resize,我有一个绘制html5画布的渲染器类,我希望它在调整窗口大小时重新绘制内容。问题是update()代码只执行一次-在initialize()内部-并且调整窗口大小没有任何作用。另一方面,每次调整窗口大小时,onWindowResized()都会执行。那么,错误在哪里 /// <reference path="../lib/easeljs.d.ts" /> /// <reference path="../lib/tweenjs.d.ts" /> /// <refere

我有一个绘制html5画布的渲染器类,我希望它在调整窗口大小时重新绘制内容。问题是update()代码只执行一次-在initialize()内部-并且调整窗口大小没有任何作用。另一方面,每次调整窗口大小时,onWindowResized()都会执行。那么,错误在哪里

/// <reference path="../lib/easeljs.d.ts" />
/// <reference path="../lib/tweenjs.d.ts" />
/// <reference path="../lib/soundjs.d.ts" />
/// <reference path="../lib/preloadjs.d.ts" />

class Renderer
{
    private mStage:createjs.Stage;
    private mShape:createjs.Shape;

    public initialize (stage:createjs.Stage):void
    {
        this.mStage = stage;
        this.mStage.autoClear = true;
        this.update ();

        window.onresize = this.onWindowResized;
    }

    public update ():void
    {
        if (this.mStage)
        {
            this.mStage.canvas.width = window.innerWidth;
            this.mStage.canvas.height = window.innerHeight;

            this.mShape = new createjs.Shape();
            this.mShape.graphics.beginFill("#dddddd");
            this.mShape.graphics.drawRect(0, 0, this.mStage.canvas.width, this.mStage.canvas.height);
            this.mShape.graphics.beginFill("#ff4400");
            this.mShape.graphics.drawCircle(this.mStage.canvas.width / 2, this.mStage.canvas.height / 2, 25);

            this.mStage.addChild(this.mShape);
            this.mStage.update();
        }
    }

    private onWindowResized (event:UIEvent):void
    {
        this.update();
    }
}
//
/// 
/// 
/// 
类渲染器
{
私有mStage:createjs.Stage;
私有mShape:createjs.Shape;
公共初始化(stage:createjs.stage):无效
{
this.mStage=阶段;
this.mStage.autoClear=true;
this.update();
window.onresize=this.onWindowResized;
}
公共更新():void
{
if(this.mStage)
{
this.mStage.canvas.width=window.innerWidth;
this.mStage.canvas.height=window.innerHeight;
this.mShape=new createjs.Shape();
这个.mShape.graphics.beginll(“#dddddddd”);
this.mShape.graphics.drawRect(0,0,this.mStage.canvas.width,this.mStage.canvas.height);
这个.mShape.graphics.beginll(“#ff4400”);
this.mShape.graphics.drawCircle(this.mStage.canvas.width/2,this.mStage.canvas.height/2,25);
this.mStage.addChild(this.mShape);
this.mStage.update();
}
}
private onWindowResized(事件:UIEvent):无效
{
这个.update();
}
}

这里的问题是,当事件执行时,它将位于事件上下文中(用于调整大小的窗口、用于单击的DOM元素、鼠标悬停等),而不是您的类-因此
将不是您的类的句柄

你可以用

window.onresize = this.onWindowResized.bind(this);

这是一个实例,
bind
比胖箭头语法更优雅。

Ok,我通过键入window.onresize=This.onWindowResized.bind(This)解决了这个问题。重要的是要记住,在TypeScript中,“this”范围与ActionScript不同。