0

オブジェクトが画像を描画しないという問題があります。画像の onload プロパティを draw 関数に設定しました。

//ctor
function Sprite(someargs,pContext)
    this.setContext(pContext); //This could be the problem?
    this.setX(px); 
    this.setY(py);
    this.setTexture(pImagePath);    


//I run this in the constructor 
Sprite.prototype.setTexture = function(pImagePath){
    this.texture = new Image();
    this.texture.onload = this.draw();  
    this.texture.src = pImagePath;
};

Sprite.prototype.draw = function(){

    this.getContext().drawImage(this.texture,this.getX(),this.getY(),100,100);
};

Sprite.prototype.setContext = function(pContext){
       this.mContext = pContext;
};

実行時にエラーはありませんが、イメージはキャンバスに描画されません。上記のすべてのメソッドにアラートを設定しました。これらはすべて実行されています。

なぜそれが描かれていないのか、誰にもアイデアがありますか?

乾杯

4

1 に答える 1

1
this.texture.onload = this.draw();  

関数ではなく、関数の結果設定しonloadていますdrawdraw

this.texture.onload = this.draw;

ここでのコンテキストが失われるため、これも良くありませんthisthis関数内は代わりにdrawを指しますtextureSprite

あなたは(現時点では)bind関数drawを必要とし、それをに渡しますthisSpriteonload

this.texture.onload = this.draw.bind(this);

また:

var that = this;
this.texture.onload = function() { that.draw(); }
于 2013-10-26T17:28:50.727 に答える