page 31 TimerEvent.TIMER


//生成两个sprite对象,使之在x和y轴上同时运动
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author
	 */
	public class Main extends Sprite
	{
		private var _square:Sprite;
		private var _circle:Sprite;
		
		public function Main()
		{
			_square = new Sprite();
			_square.graphics.beginFill(0xff0000);
			_square.graphics.drawRect(0, 0, 100, 100);
			_square.graphics.endFill();
			addChild(_square);
			_square.x = 100;
			_square.y = 50;
			
			_circle = new Sprite();
			_circle.graphics.beginFill(0x0000ff);
			_circle.graphics.drawCircle(50, 50, 50);
			_circle.graphics.endFill();
			addChild(_circle);
			_circle.x = 100;
			_circle.y = 200;
			
			var squareTimer:Timer = new Timer(50, 0);
			squareTimer.addEventListener(TimerEvent.TIMER, onSquareTimer);
			squareTimer.start();
			
			var circleTimer:Timer = new Timer(100, 0);
			circleTimer.addEventListener(TimerEvent.TIMER, onCircleTimer);
			circleTimer.start();
		}
		
		private function onSquareTimer(event:TimerEvent):void
		{
			_square.x++;
			_square.y++;
		}
		
		private function onCircleTimer(event:TimerEvent):void
		{
			_circle.x++;
			_circle.y++;
		}
	
	}

}

你可能感兴趣的:(【读书笔记】AS3,CookBook,timer,function,import,class)