2015-11-07 42 views
0

從BitmapData創建sprite時遇到問題。當我有一個矩形形狀時,精靈創建時沒有問題,但是當bitmapData只是一條線時,精靈只是空的。Sprite from BitmapData line

bmd = this.game.add.bitmapData(this.line.width, this.line.height); 
 

 
//This works fine: 
 
bmd.ctx.rect(0, 0, 32, 32); 
 
bmd.ctx.fillStyle = "#0f0"; 
 
bmd.ctx.fill(); 
 
var platform = this.game.add.sprite(this.line.midPoint().x, this.line.midPoint().y, bmd); 
 

 
//This doesn't work 
 
bmd.ctx.this.lineWidth = "8"; 
 
bmd.ctx.strokeStyle = 'white'; 
 
bmd.ctx.moveTo(this.startPoint.x, this.startPoint.y); 
 
bmd.ctx.lineTo(this.endPoint.x, this.endPoint.y); 
 
bmd.ctx.stroke(); 
 
var platform = this.game.add.sprite(this.line.midPoint().x, this.line.midPoint().y, bmd);

是否有已知問題或我要在我的代碼可怕的錯誤?

感謝您的時間

回答

0

我找到了一種方法來做到這一點。

Game.prototype.create = function(){ 
 
    this.bmd = this.game.add.bitmapData(this.game.width-20, this.game.height); 
 
    this.bmd.ctx.beginPath(); 
 
    this.bmd.ctx.lineWidth = "4"; 
 
    this.bmd.ctx.strokeStyle = 'white'; 
 
    this.bmd.ctx.stroke(); 
 
    this.platform = this.game.add.sprite(0, 0, this.bmd); 
 
}; 
 

 
Game.prototype.draw = function(){ 
 
    this.bmd.clear(); 
 
    this.bmd.ctx.beginPath(); 
 
    this.bmd.ctx.moveTo(this.startPoint.x, this.startPoint.y); 
 
    this.bmd.ctx.lineTo(this.endPoint.x , this.endPoint.y); 
 
    this.bmd.ctx.lineWidth = 4; 
 
    this.bmd.ctx.stroke(); 
 
    this.bmd.ctx.closePath(); 
 
    this.bmd.render(); 
 
}

原來我應該添加一次精靈,然後就重新呈現它。