Monday, June 9, 2008

Tweening Experiments (Enter Frame)






CODE:
var myBrain:MovieClip = new MovieClip(); //create a new empty movie clip
var xPos:Number;
var yPos:Number;

box_mc.addEventListener(Event.ENTER_FRAME, rotate); //add new enter frame, call rotate function
stage.addEventListener(MouseEvent.CLICK, moveBox); //add new mouse event, call moveBox function

function rotate(event:Event):void{
box_mc.rotation += 8; //rotate box by 8 degrees 31 times/second
}

function moveBox(event:MouseEvent):void{
xPos = mouseX; //set xPos variable
yPos = mouseY; //set yPos veriable
myBrain.addEventListener(Event.ENTER_FRAME, moveIt); //add new enter frame, call moveIt
}

function moveIt(event:Event):void{
if(Math.abs(xPos-box_mc.x)<1 && Math.abs(yPos-box_mc.y)<1){ //when the box gets real close to destination...
myBrain.removeEventListener(Event.ENTER_FRAME, moveIt); //remove the enter frame that is on myBrain
box_mc.x = xPos; //snap box into position
box_mc.y = yPos; // " " "
}else{
box_mc.x += .1 * (xPos - box_mc.x); //simple easing formula
box_mc.y += .1 * (yPos - box_mc.y); //simple easing formula
}
}

1 comment:

Makubex said...

I know its old, but nice and simple.

Accidentally found ur blog - its cool.