• 第二章 ActionScript 3.0学习之画星星(鼠标及键盘事件)


        今天觉得学到的比较有趣,所以记录之......~~~

        下面这段就是画出星星的代码:StarShape.as

    package {
    	import flash.display.Shape;
    	import flash.display.GradientType;
    	
    	public class StarShape extends Shape {
    		public function StarShape (x:Number = 50, y:Number = 50, points:int = 5, innerRadius:Number = 20, outerRadius:Number = 50, angle:Number = 0, color:uint = 0xff0000) {//x和y是起始点~~
    			var count = Math.abs(points);
    			this.graphics.lineStyle(2, 0x85DB18);
    			//开始填色
    			this.graphics.beginFill(color);
    			if (count > 2) {
    				//init vars
    				var step, halfStep, start, n, dx, dy;
    				//计算两点之间的距离
    				step = (Math.PI * 2) / points;
    				halfStep = step / 2;
    				//起始角度
    				start = (angle / 180) * Math.PI;
    				this.graphics.moveTo(x + (Math.cos(start) * outerRadius), y - (Math.sin(start) * outerRadius));
    				//画星状图的边
    				for (n = 1; n <= count; n++) {
    					dx = x + Math.cos(start + (step * n) - halfStep) * innerRadius;
    					dy = y - Math.sin(start + (step * n) - halfStep) * innerRadius;
    					this.graphics.lineTo(dx, dy);
    					dx = x + Math.cos(start + (step * n)) * outerRadius;
    					dy = y - Math.sin(start + (step * n)) * outerRadius;
    					this.graphics.lineTo(dx, dy);
    				}
    			}
    			this.graphics.endFill();
    		}
    	}
    }
    

      具体显示效果如下:

    鼠标点击和键盘按下事件练习(按下的按键是Alt,Shift,Ctrl,单个或两两组合,三个一起,同时点击鼠标):

    源码:

    SampleMouseAndkey.as
    package {
    	import flash.display.Sprite;
    	import flash.events.MouseEvent;
    	
    	public class SampleMouseAndKey extends Sprite {
    		public function SampleMouseAndKey() {
    			this.stage.addEventListener(MouseEvent.CLICK, clickHandler);
    		}
    		private function clickHandler(evt:MouseEvent):void {
    			var color:uint = 0xffffff;
    			if (evt.ctrlKey) color = 0x66cc00;
    			if (evt.altKey) color = 0x669933;
    			if (evt.shiftKey) color = 0x66ff00;
    			//按两个键
    			if (evt.altKey && evt.ctrlKey) color = 0xffcc00;
    			if (evt.altKey && evt.shiftKey) color = 0xffff00;
    			//三个键//转载注明原文地址:http://www.cnblogs.com/xianfangloveyangmei/p/4425589.html
    			if (evt.altKey && evt.ctrlKey && evt.shiftKey) color = 0xff9900;
    			trace("click:" + color.toString(16));
    			var star:StarShape = new StarShape(evt.stageX, evt.stageY, 5, 10, 20, 0, color);
    			addChild(star);
    			var clear_star:
    		}
    	}
    }
    

      

    效果:(背景白色,鼠标单击可以看到画出的效果)

    下面的带清舞台(其实是创建子对象覆盖):

    修改后的SampleMouseAndkey.as

    package {
    	import flash.display.Sprite;
    	import flash.events.MouseEvent;
    	
    	public class SampleMouseAndKey extends Sprite {
    		public function SampleMouseAndKey() {
    			this.stage.addEventListener(MouseEvent.CLICK, clickHandler);
    		}
    		private function clickHandler(evt:MouseEvent):void {
    			var clear:Sprite = new clear_stage(0, 0, 400, 400);
    			var color:uint = 0xffffff;
    			if(!(evt.altKey && evt.ctrlKey && evt.shiftKey)){
    				if (evt.ctrlKey) color = 0x66cc00;
    				if (evt.altKey) color = 0x669933;
    				if (evt.shiftKey) color = 0x66ff00;
    				//按两个键
    				if (evt.altKey && evt.ctrlKey) color = 0xffcc00;
    				if (evt.altKey && evt.shiftKey) color = 0xffff00;
    				//三个键
    				//if (evt.altKey && evt.ctrlKey && evt.shiftKey) color = 0xff9900;
    				trace("click:" + color.toString(16));
    				var star:StarShape = new StarShape(evt.stageX, evt.stageY, 5, 10, 20, 0, color);
    				addChild(star);//先画了星星
    			}
    			if (evt.altKey && evt.ctrlKey && evt.shiftKey) {
    				var clear:Sprite = new clear_stage(0, 0, 400, 400);
    				addChild(clear);
    			}
    			
    		}
    	}
    }
    import flash.display.Sprite;
    class clear_stage extends Sprite {//清理舞台
    	public function clear_stage(x:Number, y:Number, w:Number, h:Number) {
    		this.graphics.beginFill(0xFFFFFF);
    		this.graphics.drawRect(x, y, w, h);
    		this.graphics.endFill();
    		//trace("........");
    	}
    }
    

    具体效果如下(同时按Alt,Shift,Ctrl,同时单击击鼠标左键清舞台):舞台白色~~~

    可以测试一下上面的点击结果哦,嘻嘻~~新手笔记!!大婶别吐。

  • 相关阅读:
    __proto__、prototype、constructor 之间的关系
    call()与apply()区别
    条件注释判断浏览器
    永远在页面底部的层
    jQuery1.9和jQuery2.0加载(IE10下条件判断失效)
    mousewheel
    2013多校第四场 G题 ZZ的搬砖难题
    hdu 4389 x mod f(x) 数位DP
    hdu 4468 spy 构造kmp
    hdu 4466 triangle 三角形统计 数学计数
  • 原文地址:https://www.cnblogs.com/xianfangloveyangmei/p/4425589.html
Copyright © 2020-2023  润新知