好的,想法很简单,在舞台上设置按钮,点击按钮改变颜色来绘制。我正在努力学习flash&动作脚本并不确定我的问题在哪里,但我无法弄清楚如何做到这一点。
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;
}
}
}
答案 0 :(得分: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,请确保整个类中都知道colorArray。
只需在colors
方法之外定义它就像Lukasz所说:
protected var colorArray:Array
使用数字而不是字符串0xFF0000
代替"0xFF0000"
答案 1 :(得分:0)
使用Button上的tag
属性将颜色(或颜色索引)设置为按钮本身,然后在点击处理程序中通过event.sender.tag
读取此标记。
您还可以使用颜色数组中的标记(以及其他数组中的名称)生成按钮。