javascript - 在 EaselJS 中调整矩形大小

标签 javascript html html5-canvas easeljs

我似乎完全迷失在 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/

相关文章:

jquery - 停止文本环绕多个图像

javascript - context drawImage/canvas toDataURL 在 Opera Mobile 上明显滞后

javascript - 错误: invalid variable initialization

javascript - 单个页面上的多个选项卡

javascript - webkit 浏览器中 svg 文本的奇怪行为

javascript - HTML 按钮计数

javascript - 使用for循环将多条记录插入数据库

javascript - 动力学JS : Fading in a fill image when applying it to a polygon shape?

javascript - 使用 Javascript 将两个 SVG 元素合并为一个?

javascript - 输入变化时更新 Vue 减法方法