Kircho
Kircho

Reputation: 71

AS 3 simple ease

How can I move an object and be able to physically see it when it is moving? Not just disappear and appear on a different location like it would be using the following code.

buttonL2_btn.addEventListener(MouseEvent.CLICK, left);

function left(event:Event):void{

      box_mc.x =241.5;
}

This is going to move myObject to any location specified, but again I want to be able to see it when moving.

Upvotes: 1

Views: 3141

Answers (3)

Placeable
Placeable

Reputation: 592

Okay I am getting a bit sick of people constantly suggesting some tweening engine. Sure they rock, but it won't help the OP to understand what he is doing.

Kircho to move an object with a really easy tween I suggest the following code in an onEnterFrame event for your object to move:

addEventListener(Event.ENTER_FRAME, onEnterFrame);
var xGoal:Number = 100;     //::  The target X destination for your object
var yGoal:Number = 100;     //::  The target Y destination for your object
var smothness:Number = 10;  //::  Smoothness factor for movement.  The lower the  value the faster the movement.

function onEnterFrame(e:Event):void
{
      box_mc.x += (xGoal - box_mc.x) / smothness;
      box_mc.y += (yGoal - box_mc.y) / smothness;
}

Will move/ease your box object to the desired location with a set smoothness.

Upvotes: 3

Daniel
Daniel

Reputation: 35684

You can install any of the 437 available tweening engines
or you can add a few lines of code

set up a variable that holds the destination value

var dest:Number = 241.5; // this is what gets updated on mouse click

on enterframe event for box:

function onBoxEnterFrame(e:MouseEvent):void{
  if (dest != box_mc.x){
    var easeNum:Number = 0.4 // between 0 and 1, the higher the number, the slower the transition
    box_mc.x = box_mc.x * easeNum + dest * (1-easeNum);
  }
}

you can add a few more lines to snap the position when it is close (less than 0.1 difference) or use a more linear change where you adjust incrementally like box_mc.x += 5; until it matches the dest number

Upvotes: 1

Marcelo Assis
Marcelo Assis

Reputation: 5194

In your example you are just setting it's X position when some button is pressed, when you need to change X into an EnterFrame event, like this:

this.addEventListener(Event.ENTER_FRAME, move);

function move(event:Event):void{
      box_mc.x -= 5
}

Your box_mc should move left 5 pixels accordingly with your framerate.


You can use a easing library to that easily. I strongly recommend TweenMax.

Upvotes: 4

Related Questions