0

好的,想法很简单,舞台上的一组按钮,点击按钮改变颜色来绘制。我正在尝试学习 flash 和 actionscript,但不确定我的问题出在哪里,但我不知道该怎么做。

package {
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Artist extends Sprite {
    public var drawing:Boolean;
    public var colorArray:Array;
    public var dc;

    public function colors() {
        colorArray = ["0xFF0000","0xFFA500","0xFFFF00","0x00FF00","0x0000FF","0x4B0082","0x8F00FF","0xFF69B4","0x00CCFF","0x008000","0x8B4513"];

        for (var i:int = 0; i < colorArray.length; i++) {
        this["btn_" + i].addEventListener(MouseEvent.CLICK, set_color);
        }
    }

    public function set_color(e:MouseEvent):void {
        dc = colorArray;
    }

    public function Artist() {
        graphics.lineStyle(10,dc);
        drawing = false;
        stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
    }

    public function startDrawing(event:MouseEvent):void {
        graphics.moveTo( mouseX, mouseY);
        drawing = true;
    }

    public function draw(event:MouseEvent) {
        if(drawing) {
            graphics.lineTo(mouseX,mouseY);
        }
    }

    public function stopDrawing(event:MouseEvent) {
        drawing = false;
    }
}

}

4

2 回答 2

1

您应该通过按钮名称获取索引,然后您可以使用单击的索引分配颜色。

for (var i:int = 0; i < colorArray.length; i++) {
    this["btn_" + i].addEventListener(MouseEvent.CLICK, set_color);
}

public function set_color(e:MouseEvent):void {
    // Get the button name and fetch it's index
    var index:int = int(e.currentTarget.name.substring(4));
    dc = colorArray[index];
}

此外,如果您想在方法中访问它,请确保 colorArray 在整个类中都是已知的。colors正如Lukasz 所说 ,只需在方法之外定义它:protected var colorArray:Array并使用数字而不是字符串作为颜色0xFF0000而不是"0xFF0000"

于 2012-11-27T09:42:20.747 回答
0

使用tagButton 上的属性将颜色(或颜色索引)放入按钮本身,然后在单击处理程序中通过event.sender.tag.
您还可以使用颜色数组中的标签(以及另一个数组中的名称)生成按钮。

于 2012-11-27T07:54:17.647 回答