代码之家  ›  专栏  ›  技术社区  ›  Ólafur Waage

如何在不改变边框颜色的情况下改变精灵的颜色?

  •  0
  • Ólafur Waage  · 技术社区  · 14 年前

    我有这个密码

    public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
    {
        this.graphics.beginFill(arg_color);
        this.graphics.lineStyle(1.0, 0x000000, 0.7);
        this.graphics.drawRect(0, 0, 7, 13);
        this.alpha = 1.0;
        this.x = x;
        this.y = y;
        this.graphics.endFill();
    }
    

    在这里我构造类(从sprite扩展而来)。然后我需要一个函数来改变精灵的颜色。目前我有这个

    public function setColor(arg_color:int):void
    {
        color = arg_color;
    
        this.graphics.beginFill(color);
        this.graphics.drawRect(0, 0, 7, 13);
        this.graphics.endFill();
    }
    

    这似乎是工作,但这是创造一个新的矩形。我不想要。

    我尝试过色彩变换,它改变了一切,甚至改变了边框,这不是我想要的。我无法进行颜色变换,然后设置边框颜色。

    那么,如何在不改变边框颜色的情况下改变精灵的颜色呢?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Ólafur Waage    14 年前

    我找到了答案。

    在类中创建两个精灵。身体和边界。分别设置它们,然后在body sprite上使用transform only更改颜色。

    这是修改后的构造函数

    public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void
    {
        body.graphics.beginFill(arg_color);
        body.graphics.drawRect(x + 1, y + 1, 6, 12);
        body.graphics.endFill();
    
        border.graphics.beginFill(0xFFFFFF);
        border.graphics.lineStyle(1.0, 0x000000, 0.7);
        border.graphics.drawRect(x, y, 7, 13);
        border.graphics.endFill();
    
        this.addChild(border);
        this.addChild(body);
    }