[Flash/ActionScript] 5일차 - DisplayObject 움직임
1. 엔터프레임을 이용한 움직임
import flash.display.MovieClip;
import flash.events.Event;
var mc:MovieClip = this.getChildByName("mc") as MovieClip;
var speed:Number = int(Math.random()*4)+1;
this.addEventListener(Event.ENTER_FRAME, enterFrameListener);
trace(speed);
function enterFrameListener(ev:Event):void{
mc.x += speed;
}
2. 감속공식을 이용한 움직임
import flash.events.Event;
this.y = 100;
this.x = 0;
this.addEventListener(Event.ENTER_FRAME, enterFrameListener);
function enterFrameListener(ev:Event):void{
this.x += ((stage.stageWidth-this.width)-this.x)*0.05;
}
3. 마우스를 부드럽게 따라다니는 무비클립
import flash.display.MovieClip;
import flash.events.Event;
var mc:MovieClip = this.getChildByName("mc") as MovieClip;
mc.addEventListener(Event.ENTER_FRAME, enterFrameListener);
function enterFrameListener(ev:Event):void{
mc.x += (this.mouseX - mc.x) * 0.3;
mc.y += (this.mouseY - mc.y) * 0.3;
}
4.일정시간으로 반복호출
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
var timer:Timer = new Timer(200);
timer.addEventListener(TimerEvent.TIMER, timerListener);
timer.start();
function timerListener(ev:TimerEvent):void{
var snow:MovieClip = new Snow() as MovieClip;
snow.x = Math.random() * stage.stageWidth;
snow.y = -(snow.height /2);
this.addChild(snow);
}
import com.greensock.easing.Linear;
import com.greensock.TweenLite;
this.scaleX = this.scaleY = Math.random();
var time:Number = Math.random()*3+3;
var tgrotation:Number = Math.random()*180-90; 회전값을 임의로 정의(-90~90)
var tgX:Number = this.x + Math.random()*100-50;
TweenLite.to(this, time, {
x:tgX,
y:stage.stageHeight+this.height/2,
rotaion:tgrotation,
ease:Linear.easeNone,
onComplete:remove
});
function remove():void{
parent.removeChild(this);
}
댓글
댓글 쓰기