第二章 ActionScript 3.0學習之畫星星(滑鼠及鍵盤事件)

XIANFANG發表於2015-04-14

    今天覺得學到的比較有趣,所以記錄之......~~~

    下面這段就是畫出星星的程式碼: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,同時單擊擊滑鼠左鍵清舞臺):舞臺白色~~~

可以測試一下上面的點選結果哦,嘻嘻~~新手筆記!!大嬸別吐。

相關文章