Skip to main content
Participant
December 14, 2012
Answered

How to move a Movie clip object smoothly to a certain point by just clicking a button?

  • December 14, 2012
  • 2 replies
  • 1834 views

So I'm making a simple point and click game. And in it, you hit one or two buttons. Then a boat moves next to your character (instance name pondPageBoat) What code would I use to set/move it to this certain point on the screen?

This topic has been closed for replies.
Correct answer kglad

import fl.transitions.Tween;

import fl.transitions.easing.None;

your_btn.addEventListener(MouseEvent.CLICK,clickF);

var tween:Tween;

function clickF(e:MouseEvent):void{

tween = new Tween(pondPageBoat, "x", None.easeNone, pondPageBoat.x, destinationX, 1, true);  // define destinationX.  if you want to tween the y property use another Tween()

}

2 replies

sinious
Legend
December 14, 2012

Oh, the many ways. I'd extoll you go to greensock.com, download TweenLite and use that but Flash has something built in so let's use that to be easy.

The Tween class:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/transitions/Tween.html

Check the examples.

So for example if you had "a_mc" and you wanted it to move to "b_mc" then the code would look like this:

import fl.transitions.Tween;

import fl.transitions.easing.Regular;

new Tween(a_mc, "x", Regular.easeOut, a_mc.x, b_mc.x, 1, true);

new Tween(a_mc, "y", Regular.easeOut, a_mc.y, b_mc.y, 1, true);

This tells the Tween class that you want to move object "a_mc". You want to start at the position a_mc (x/y) is and move it to the position b_mc is (x/y). It does it over 1 second. It also eases the motion just for some polish.

So import Flash's regular Tween class along with one type of easing. Start a separate Tween per the property you'd like to change (x is 1, y is 2, etc).

Season to taste.

If you download TweenLite from greensock.com it's faster and lets you supply multiple properties to change over time in the same command. e.g.:

TweenLite.to(a_mc,1,{x:b_mc.x, y:b_mc.y});

Syntax is flexible and easier. I didn't clutter it with easing though.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 14, 2012

import fl.transitions.Tween;

import fl.transitions.easing.None;

your_btn.addEventListener(MouseEvent.CLICK,clickF);

var tween:Tween;

function clickF(e:MouseEvent):void{

tween = new Tween(pondPageBoat, "x", None.easeNone, pondPageBoat.x, destinationX, 1, true);  // define destinationX.  if you want to tween the y property use another Tween()

}