我似乎完全迷失在 EaslJS 中了。我不知道如何在调用我的函数时调整矩形的大小。
场景是这样的:我的玩家被击中,生命值下降,因此生命值条变小。
这是我创建健康栏的方法:
this.statusBar = new createjs.Shape()
this.statusBar.graphics
.beginFill("red")
.drawRect(0, 0, this.width, this.height);
this.statusBar.x = this.x+500;
this.statusBar.y = this.y;
this.statusBar.width = this.width;
this.statusBar.height = this.height;
这就是我试图调整它的大小的地方:
this.decreaseHealth = function(amount) {
percentageLeft = (player.health/player.fullPlayerHealth)*100;
console.log(this.fullBarWidth*(percentageLeft/100));
this.statusBar.width = this.fullBarWidth*(percentageLeft/100);
}
我的 stage.update();
在我的循环中,因此它正在按照您期望的方式更新。
最佳答案
在easelJS中,Shape对象实际上是一个用于进行自定义绘图的 Controller 。
如果您希望更改形状的内容,则必须重新绘制该形状的内容。
您可以在状态栏 Controller 中绘制健康矩形,如下所示:
// get a reference to the html canvas element
var canvas=document.getElementById("canvas");
// create a new Stage
var stage=new createjs.Stage(canvas);
// create a new Shape
// the shape object is actually a controller for doing custom drawings
var shape=new createjs.Shape();
// add the shape controller to the stage
stage.addChild(shape);
// draw a rectangle using the shape controller
shape.graphics.clear().beginFill("#F00").drawRect(30,20,150,25);
// display all updated drawings
stage.update();
然后,当您想要更改玩家的健康状况时,您可以像这样重新绘制健康状况矩形:
// redraw the health rectangle using the shape controller
shape.graphics.clear().beginFill("#F00").drawRect(30,20,75,25);
// display all updated drawings
stage.update();
关于javascript - 在 EaselJS 中调整矩形大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15637982/