编码非矩形健康栏

时间:2014-04-21 00:51:20

标签: actionscript-3 flash

我正在创建一个游戏,健康栏是使用动作脚本在flash中完成的。

如果条形只是矩形,我可以很容易地做到这一点,但它是一个不规则的形状,不知道如何。

例如http://i.stack.imgur.com/20J8a.png(绘制得很糟)

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用mask属性在不规则形状的对象上创建可见的移动边框。一个例子:

class HealthBar extends Sprite {
    // implement normal healthbar draw, any shape allowed
    var m:Sprite;
    function HealthBar() {
        m=new Sprite();
        m.graphics.beginFill(0x0,1);
        m.graphics.drawRect(0,0,100,50); // make sure it overlaps whole healthbar
        m.graphics.endFill();
        this.mask=m; // to set mask
        addChild(m); // necessary, otherwise it might not work properly
    }
    function adjustHealthBar(percentage:int):void {
        // will be called when you need to change the display
        // 0 = empty, 100 = full
        m.x=0-m.width*percentage/100; 
        // shift mask leftwards, so less of bar is visible
        // that's all! If you need fancy, redraw graphics of "m" for new healthbar state
    }
}