package
{import flash.display.Shape;import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.events.MouseEvent;import flash.events.TimerEvent;import flash.utils.Timer;import mx.controls.Label;//窗体大小,绘制帧率[SWF(width="400", height="300", frameRate="60")]public class MyDemo extends Sprite{private var circle:Shape;private var label:Label;private var timer:Timer;public function MyDemo(){label = new Label();label.x = 30;label.y = 30;addChild(label);circle = new Shape();circle.x = 50;circle.y = 100;addChild(circle);circle.graphics.beginFill(0xee2f2f);circle.graphics.drawCircle(circle.x,circle.y,40);circle.addEventListener(Event.ENTER_FRAME,changePos);//每帧调用changePos函数stage.addEventListener(MouseEvent.CLICK,onClick); //舞台上添加鼠标点击监听事件stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);//舞台添加键盘监听timer = new Timer(1000,5); //设置延迟时间是1秒,循环执行5次timerExcute()timer.addEventListener(TimerEvent.TIMER,timerExcute);timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete);//执行完成后调用timerCompletetimer.start();//启动timer}public function changePos(event:Event):void{if(circle.y >= 300){circle.y = 1;}else if(0<circle.y && circle.y < 300){ circle.y+=5;}}//在点击的地方产生绿色方形public function onClick(event:MouseEvent):void{var rect:Shape = new Shape();addChild(rect);rect.graphics.beginFill(0x11ff00);rect.graphics.drawRect(event.stageX,event.stageY,20,20);}public function keyDown(event:KeyboardEvent):void{var count:int = 0if(event.keyCode == 13) //监听Enter键{ var rect:Shape = new Shape();addChild(rect);rect.graphics.beginFill(0x00f1f0);rect.graphics.drawRect(Math.random()*400,Math.random()*300,10,10);}}public function timerExcute(event:TimerEvent){var rect:Shape = new Shape();addChild(rect);rect.graphics.beginFill(0x0f11f0);rect.graphics.drawRect(Math.random()*400,Math.random()*300,10,10);}public function timerComplete(event:TimerEvent){var rect:Shape = new Shape();addChild(rect);rect.graphics.beginFill(0x000000);rect.graphics.drawRect(Math.random()*400,Math.random()*300,30,60);}}
}
桌面程序Air效果图: