【AS3代码】小画板

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    
    public class Main extends Sprite
    {        
        private var ceng:Sprite;
        private var isdown:Boolean = false;        //默认鼠标未按下
        private var linesize:uint = 2;            //笔触粗细
        private var oldx:Number;                //记录上一个点的X坐标位置
        private var oldy:Number;                //记录上一个点的Y坐标位置
        
        private var changeSize:Sprite;            //改变笔尖大小的按钮
        private var fontsize:TextField;            //改变笔尖大小的按钮上的文字
        private var sizes:uint = 2;                //初始的笔尖大小
        private var colors:Number = 0x000000;    //初始的笔尖颜色
        
        public function Main():void
        {
            init();
        }
        
        private function init():void
        {
            ceng = new Sprite();
            this.addChild(ceng);
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN,ondown);    //鼠标按下            
            stage.addEventListener(MouseEvent.MOUSE_MOVE,onmove);    //鼠标移动            
            stage.addEventListener(MouseEvent.MOUSE_UP,onup);        //鼠标抬起
            
            //------------------------------------------------
            
            changeSize = new Sprite();
            changeSize.graphics.beginFill(0x000000,0.5);
            changeSize.graphics.drawRect(10,10,120,30);
            changeSize.graphics.endFill();
            this.addChild(changeSize);
            
            fontsize = new TextField();
            fontsize.text = "红色,变粗,清理舞台";
            fontsize.textColor = 0xff0000;
            fontsize.x = 20;
            fontsize.y = 15;
            changeSize.addChild(fontsize);
            
            fontsize.addEventListener(MouseEvent.CLICK,changes);
            
            //-----------------------------------------------
        }
        
        private function changes(evt:MouseEvent):void
        {
            sizes = 6;
            colors = 0xff0000;
            ceng.graphics.clear();
        }
        
        private function ondown(evt:MouseEvent):void
        {
            isdown = true;                //设为鼠标已按下
            oldx = stage.mouseX;        //当前鼠标按下那一刻,所在舞台的X坐标
            oldy = stage.mouseY;        //当前鼠标按下那一刻,所在舞台的y坐标
        }
        private function onmove(evt:MouseEvent):void
        {
            //如果鼠标已按下
            if(isdown)
            {
                ceng.graphics.lineStyle(sizes, colors);
                ceng.graphics.moveTo(oldx, oldy);                    //开始绘图的起始点
                ceng.graphics.lineTo(stage.mouseX, stage.mouseY);    //绘图的终点坐标
                
                //更新一下坐标数据(用本次绘制的终点坐标替换,成为下一次绘制的起点坐标)
                oldx = stage.mouseX;
                oldy = stage.mouseY;
            }
        }
        private function onup(evt:MouseEvent):void
        {
            isdown = false;            //设为鼠标抬起    
        }
    }
}
posted @ 2012-04-25 14:42  王翔(kingfly)  阅读(935)  评论(0编辑  收藏  举报