·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3代码写的一个画板功能的实例源代码

时间:2014-04-08 10:57王翔
[AS3]as3代码写的一个画板功能的实例源代码
  1. package 
  2.     import flash.display.Sprite; 
  3.     import flash.events.MouseEvent; 
  4.     import flash.text.TextField; 
  5.      
  6.     public class Main extends Sprite 
  7.     {         
  8.         private var ceng:Sprite; 
  9.         private var isdown:Boolean = false;        //默认鼠标未按下 
  10.         private var linesize:uint = 2;            //CuPlayer.com笔触粗细 
  11.         private var oldx:Number;                //记录上一个点的X坐标位置 
  12.         private var oldy:Number;                //记录上一个点的Y坐标位置 
  13.          
  14.         private var changeSize:Sprite;            //改变笔尖大小的按钮 
  15.         private var fontsize:TextField;            //改变笔尖大小的按钮上的文字 
  16.         private var sizes:uint = 2;                //初始的笔尖大小 
  17.         private var colors:Number = 0x000000;    //初始的笔尖颜色 
  18.          
  19.         public function Main():void 
  20.         { 
  21.             init(); 
  22.         } 
  23.          
  24.         private function init():void 
  25.         { 
  26.             ceng = new Sprite(); 
  27.             this.addChild(ceng); 
  28.              
  29.             stage.addEventListener(MouseEvent.MOUSE_DOWN,ondown);    //鼠标按下             
  30.             stage.addEventListener(MouseEvent.MOUSE_MOVE,onmove);    //鼠标移动             
  31.             stage.addEventListener(MouseEvent.MOUSE_UP,onup);        //鼠标抬起 
  32.              
  33.             //------------------------------------------------ 
  34.              
  35.             changeSize = new Sprite(); 
  36.             changeSize.graphics.beginFill(0x000000,0.5); 
  37.             changeSize.graphics.drawRect(10,10,120,30); 
  38.             changeSize.graphics.endFill(); 
  39.             this.addChild(changeSize); 
  40.              
  41.             fontsize = new TextField(); 
  42.             fontsize.text = "CuPlayer.com红色,变粗,清理舞台"
  43.             fontsize.textColor = 0xff0000
  44.             fontsize.x = 20
  45.             fontsize.y = 15
  46.             changeSize.addChild(fontsize); 
  47.              
  48.             fontsize.addEventListener(MouseEvent.CLICK,changes); 
  49.              
  50.             //----------------------------------------------- 
  51.         } 
  52.          
  53.         private function changes(evt:MouseEvent):void 
  54.         { 
  55.             sizes = 6
  56.             colors = 0xff0000
  57.             ceng.graphics.clear(); 
  58.         } 
  59.          
  60.         private function ondown(evt:MouseEvent):void 
  61.         { 
  62.             isdown = true;                //CuPlayer.com设为鼠标已按下 
  63.             oldx = stage.mouseX;        //当前鼠标按下那一刻,所在舞台的X坐标 
  64.             oldy = stage.mouseY;        //当前鼠标按下那一刻,所在舞台的y坐标 
  65.         } 
  66.         private function onmove(evt:MouseEvent):void 
  67.         { 
  68.             //CuPlayer.com如果鼠标已按下 
  69.             if(isdown) 
  70.             { 
  71.                 ceng.graphics.lineStyle(sizes, colors); 
  72.                 ceng.graphics.moveTo(oldx, oldy);                    //CuPlayer.com开始绘图的起始点 
  73.                 ceng.graphics.lineTo(stage.mouseX, stage.mouseY);    //CuPlayer.com绘图的终点坐标 
  74.                  
  75.                 //更新一下坐标数据(用本次绘制的终点坐标替换,成为下一次绘制的起点坐标) 
  76.                 oldx = stage.mouseX; 
  77.                 oldy = stage.mouseY; 
  78.             } 
  79.         } 
  80.         private function onup(evt:MouseEvent):void 
  81.         { 
  82.             isdown = false;            //设为鼠标抬起     
  83.         } 
  84.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器