【AS3代码】小画板升级版(带重绘回放和清空功能)
生成按钮类
package com.ui
{
import flash.display.SimpleButton;
import org.osmf.layout.AbsoluteLayoutFacet;
public class Button extends SimpleButton
{
public function Button(thistext:String):void
{
var arr:Array = init(thistext);
super(arr[0],arr[1],arr[2],arr[2]); //调用父类的构造器函数
}
private function init(thistext:String):Array
{
var atext:Text = new Text(thistext);
var btext:Text = new Text(thistext);
var ctext:Text = new Text(thistext);
var a:Mysprite = new Mysprite(0xffff00);
var b:Mysprite = new Mysprite(0x00ff00);
var c:Mysprite = new Mysprite(0x0000ff);
a.addChild(atext);
b.addChild(btext);
c.addChild(ctext);
var abc:Array = [a, b, c];
return abc;
}
}
}
import flash.display.SimpleButton;
import org.osmf.layout.AbsoluteLayoutFacet;
public class Button extends SimpleButton
{
public function Button(thistext:String):void
{
var arr:Array = init(thistext);
super(arr[0],arr[1],arr[2],arr[2]); //调用父类的构造器函数
}
private function init(thistext:String):Array
{
var atext:Text = new Text(thistext);
var btext:Text = new Text(thistext);
var ctext:Text = new Text(thistext);
var a:Mysprite = new Mysprite(0xffff00);
var b:Mysprite = new Mysprite(0x00ff00);
var c:Mysprite = new Mysprite(0x0000ff);
a.addChild(atext);
b.addChild(btext);
c.addChild(ctext);
var abc:Array = [a, b, c];
return abc;
}
}
}
绘图类
package com.ui
{
import flash.display.Sprite;
public class Mysprite extends Sprite
{
public function Mysprite(col:uint):void
{
init(col);
}
private function init(col:uint):void
{
this.graphics.beginFill(col);
this.graphics.drawRect(0,0,100,30);
this.graphics.endFill();
}
}
}
import flash.display.Sprite;
public class Mysprite extends Sprite
{
public function Mysprite(col:uint):void
{
init(col);
}
private function init(col:uint):void
{
this.graphics.beginFill(col);
this.graphics.drawRect(0,0,100,30);
this.graphics.endFill();
}
}
}
文档类
package
{
import com.ui.Button;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var isdown:Boolean = false;
private var oldx:Number;
private var oldy:Number;
private var ceng:Sprite; //绘图层
//---------------------------------------------
private var dataarr:Array = new Array(); //记录鼠标位置的数组
private var ci:uint = 0; //记录绘图时向数组内添加的次数
private var huici:uint; //记录回放时播放到数组内的哪一步
private var a:Button; //重绘按钮
private var b:Button; //清除按钮
public function Main():void
{
init();
}
private function init():void
{
//创建2个按钮,并监听按钮点击事件
a = new Button("重绘");
a.x = 400;
a.y = 300;
b = new Button("清除");
b.x = 400;
b.y = 340;
this.addChild(a);
this.addChild(b);
a.addEventListener(MouseEvent.CLICK,draw);
b.addEventListener(MouseEvent.CLICK,clear);
//创建绘制图层
ceng = new Sprite();
this.addChild(ceng);
//监听鼠标的三种动作状态(按下,移动,抬起)
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, ondown);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onmove);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onup);
}
private function ondown(evt:MouseEvent):void
{
oldx = this.stage.mouseX;
oldy = this.stage.mouseY;
if(a.hitTestPoint(this.stage.mouseX, this.stage.mouseY, true) || b.hitTestPoint(this.stage.mouseX, this.stage.mouseY, true))
{
isdown = false;
}
else
{
isdown = true;
dataarr[ci] = new Array(oldx, oldy); //将按下去时的第一步坐标记录在回放数组里
}
}
private function onmove(evt:MouseEvent):void
{
if(isdown == true)
{
ci++; //次数递增
ceng.graphics.lineStyle(2,0xff0000);
ceng.graphics.moveTo(oldx, oldy);
ceng.graphics.lineTo(stage.mouseX, stage.mouseY);
oldx = stage.mouseX;
oldy = stage.mouseY;
dataarr[ci] = new Array(oldx, oldy); //将绘制的每一步坐标都记录在回放数组中
}
}
private function onup(evt:MouseEvent):void
{
isdown = false;
}
//重绘
private function draw(evt:MouseEvent):void
{
this.stage.addEventListener(Event.ENTER_FRAME,huizhi);
}
//执行回放
private function huizhi(evt:Event):void
{
//如果绘制的次数小于数组的长度,则可继续回放
//绘制时,起始值是0,假如绘制长度为100,所以绘制次数一定要小于总长度
if(huici < (dataarr.length - 1))
{
ceng.graphics.lineStyle(2,0x000000);
ceng.graphics.moveTo(dataarr[huici][0], dataarr[huici][1]);
huici++;
ceng.graphics.lineTo(dataarr[huici][0], dataarr[huici][1]);
}
else
{
//将绘制次数清零
huici = 0;
//移除监听器
this.stage.removeEventListener(Event.ENTER_FRAME, huizhi);
}
}
//清除
private function clear(evt:MouseEvent):void
{
ceng.graphics.clear();
}
}
{
import com.ui.Button;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var isdown:Boolean = false;
private var oldx:Number;
private var oldy:Number;
private var ceng:Sprite; //绘图层
//---------------------------------------------
private var dataarr:Array = new Array(); //记录鼠标位置的数组
private var ci:uint = 0; //记录绘图时向数组内添加的次数
private var huici:uint; //记录回放时播放到数组内的哪一步
private var a:Button; //重绘按钮
private var b:Button; //清除按钮
public function Main():void
{
init();
}
private function init():void
{
//创建2个按钮,并监听按钮点击事件
a = new Button("重绘");
a.x = 400;
a.y = 300;
b = new Button("清除");
b.x = 400;
b.y = 340;
this.addChild(a);
this.addChild(b);
a.addEventListener(MouseEvent.CLICK,draw);
b.addEventListener(MouseEvent.CLICK,clear);
//创建绘制图层
ceng = new Sprite();
this.addChild(ceng);
//监听鼠标的三种动作状态(按下,移动,抬起)
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, ondown);
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onmove);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onup);
}
private function ondown(evt:MouseEvent):void
{
oldx = this.stage.mouseX;
oldy = this.stage.mouseY;
if(a.hitTestPoint(this.stage.mouseX, this.stage.mouseY, true) || b.hitTestPoint(this.stage.mouseX, this.stage.mouseY, true))
{
isdown = false;
}
else
{
isdown = true;
dataarr[ci] = new Array(oldx, oldy); //将按下去时的第一步坐标记录在回放数组里
}
}
private function onmove(evt:MouseEvent):void
{
if(isdown == true)
{
ci++; //次数递增
ceng.graphics.lineStyle(2,0xff0000);
ceng.graphics.moveTo(oldx, oldy);
ceng.graphics.lineTo(stage.mouseX, stage.mouseY);
oldx = stage.mouseX;
oldy = stage.mouseY;
dataarr[ci] = new Array(oldx, oldy); //将绘制的每一步坐标都记录在回放数组中
}
}
private function onup(evt:MouseEvent):void
{
isdown = false;
}
//重绘
private function draw(evt:MouseEvent):void
{
this.stage.addEventListener(Event.ENTER_FRAME,huizhi);
}
//执行回放
private function huizhi(evt:Event):void
{
//如果绘制的次数小于数组的长度,则可继续回放
//绘制时,起始值是0,假如绘制长度为100,所以绘制次数一定要小于总长度
if(huici < (dataarr.length - 1))
{
ceng.graphics.lineStyle(2,0x000000);
ceng.graphics.moveTo(dataarr[huici][0], dataarr[huici][1]);
huici++;
ceng.graphics.lineTo(dataarr[huici][0], dataarr[huici][1]);
}
else
{
//将绘制次数清零
huici = 0;
//移除监听器
this.stage.removeEventListener(Event.ENTER_FRAME, huizhi);
}
}
//清除
private function clear(evt:MouseEvent):void
{
ceng.graphics.clear();
}
}
}