[AS3]用Timer跟addChild實現重複的背景

LINEで送る
[`evernote` not found]

其實就是要用AS3做一個永遠循環的場景
不過懶得把多餘的CODE拿掉了

改念就是用addChild把要重複的場景複製三個新增到舞台排好
用timer控制場景的移動
然後用goSpeed跟damp控制加速度

package{
 
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
 
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;
	ColorShortcuts.init()
 
	public class Main extends Sprite {
		var main1:MainMc = new MainMc();
		var main2:MainMc = new MainMc();
		var main3:MainMc = new MainMc();
		var _TopWaku:TopWaku = new TopWaku();
 
		var _btn_go:btn_go = new btn_go();
		var _btn_back:btn_back = new btn_back();
 
		var goSpeed:Number  = 0.1;
		var damp:Number = 1.05;
		var timeCount:Number;
 
		var moving:Boolean = false;
		var timer:Timer = new Timer(1);  
 
		public function Main() {			
			var mod:ModelLocator = ModelLocator.getInstance();  
 
			main1.x = -1000;
			main2.x = 0;
			main3.x = 1000;
			this.addChild(main1);
			this.addChild(main2);
			this.addChild(main3);
			_btn_go.y = _btn_back.y = 357;
			_btn_go.x = 955;
			_btn_back.x = 2;
			_btn_go.addEventListener(MouseEvent.MOUSE_OVER,goMove);
			_btn_go.addEventListener(MouseEvent.MOUSE_OUT,goMoveStop);
			_btn_back.addEventListener(MouseEvent.MOUSE_OVER,backMove);
			_btn_back.addEventListener(MouseEvent.MOUSE_OUT,backMoveStop);
			this.addChild(_TopWaku);
			this.addChild(_btn_go);
			this.addChild(_btn_back);
			mod.addEventListener("biru_over",function(e:Event){Tweener.addTween(_TopWaku,{_brightness:-0.8,time:1,transition:"easeOutBack"});});
			mod.addEventListener("biru_out",function(e:Event){Tweener.addTween(_TopWaku,{_brightness:0});});
			timer.addEventListener(TimerEvent.TIMER,moveAct);
		}
		private function mainInit(){
			main1.x = -1000;
			main2.x = 0;
			main3.x = 1000;
		}
		private function goMove(evt:MouseEvent){
			moving = true;
			timer.start(); 
		}
		private function backMove(evt:MouseEvent){
			moving = false;
			timer.start(); 
		}
		private function goMoveStop(evt:MouseEvent){
			goSpeed  = 0.1;
			timer.stop(); 
		}
		private function backMoveStop(evt:MouseEvent){
			goSpeed  = 0.1;
			timer.stop(); 
		}
		public function moveAct(e:TimerEvent){
			if(goSpeed<1.5){
				goSpeed  *= damp;
			}else{
				goSpeed = 1.5
			}
 
			if(moving){
				main1.x -= goSpeed;		
				main2.x -= goSpeed;		
				main3.x -= goSpeed;
			}else{
				main1.x += goSpeed;		
				main2.x += goSpeed;		
				main3.x += goSpeed;
			}
 
			if(main2.x >= 1000 || main2.x <= -1000) mainInit();
		}
	}
}