View Full Version : How to make an object visually move?
izzie
January 16th, 2009, 02:22 PM
How do you write to make an object go to a specific position - and so that you see the movement?
f.ex;
myObject_mc has the position x=0 and y=0,
and then it should go to position x=0 and y=20?
(I need this for making a menu, with an object following where you go on the menu, so it can't just be done on the timeline)
Thanks!
scottc
January 16th, 2009, 02:34 PM
try tweenlite/max
http://blog.greensock.com/tweenmaxas3/
JTF2
January 16th, 2009, 02:36 PM
look into the Tween class, or if you dont like Adobe's you can download 3rd party ones eg TweenLite
an example of the Tween syntax is:
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
var tween:Tween = new Tween(myObject_mc, "y", Strong.easeOut, 0, 20, 5, true);
all this says is tween myObject_mc.y from 0 to 20 over 5 seconds using the asing equation Strong.easeOut.
izzie
January 16th, 2009, 02:55 PM
Thanks, that's great.
Can I ask also - what if I'd have the object follow the mouse(vertically)?
snickelfritz
January 16th, 2009, 03:30 PM
Download TweenGroup (http://blog.greensock.com/tweengroup/).
Place the "gs" folder in the same folder as your FLA.
For the script below:
"menu" is a parent Movieclip in which all of the menu buttons and "myObject_mc" are nested.
buttons are named "btn1" "btn2" etc...
ideally, there should be no space between the button instances. Use an invisible hit area in the base Movieclip symbol if you want some visible space between the buttons.
This prevents mouse out from causing disharmony in the movement of myObject_mc.
Note: myObject_mc and all buttons should have identical vertical registration points, so they can be aligned in a reusable function.
This script aligns myObject_mc with the currently selected button, and allows it to also move to the mouse over position.
on mouse out myObject_mc moves back to the currently selected button.
Should work with any number of nested buttons, by modifying the btnNum value to match the actual number of buttons.
Example:http://www.byrographics.com/AS3/example4/index.html
import gs.*;
// disable mouse events for the menu container and myObject_mc
menu.mouseEnabled = false;
menu.myObject_mc.mouseEnabled = false;
// declare variables and intitial values
var pointerTarget:Number = menu.btn1.y;// initial sticky position for myObject_mc
var btnNum:Number = 5;// number of buttons in menu; change this to match your project.
// set mouse event properties for all of the button instances
for (var i:Number = 1; i<=btnNum; i++)
{
menu["btn" + i].mouseEnabled = true;
menu["btn" + i].buttonMode = true;
menu["btn" + i].mouseChildren = false;
}
menu.addEventListener(MouseEvent.MOUSE_OVER, menuOver, false, 0, true);
function menuOver(event:MouseEvent):void
{
TweenLite.to(menu.myObject_mc, .5, {y:event.target.y});
// add out and click listeners
menu.addEventListener(MouseEvent.MOUSE_OUT, menuOut, false, 0, true);
menu.addEventListener(MouseEvent.CLICK, menuClick, false, 0, true);
}
function menuOut(event:MouseEvent):void
{
// reset y property of myObject_mc to pointerTarget
TweenLite.to(menu.myObject_mc, .5, {y:pointerTarget});
}
function menuClick(event:MouseEvent):void
{
// remove out and click listeners
menu.removeEventListener(MouseEvent.MOUSE_OUT, menuOut);
menu.removeEventListener(MouseEvent.CLICK, menuClick);
// y property of the button clicked is the sticky position for myObject_mc
pointerTarget = event.target.y;
// reset all buttons
for (i = 1; i<=btnNum; i++)
{
menu["btn" + i].mouseEnabled = true;
}
// disable button that was clicked
event.target.mouseEnabled = false;
menu.myObject_mc.y = pointerTarget;
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.