Solved

AS3 scale and alpha tweening

Posted on 2010-08-17
5
935 Views
Last Modified: 2013-11-11
Hi all, I have this file working just as I want it, except there's one annoying little problem. When you move the mouse over the lettering in one of the boxes then move your mouse away it appears again! http://www.andrewsaldridgehost.com/error/flashMapAS3Test.swf - The FLA is here: http://www.andrewsaldridgehost.com/error/flashMapAS3Test.fla and here's the code:
import fl.transitions.Tween; 
import fl.transitions.TweenEvent; 
import fl.transitions.easing.*; 
import flash.events.* 

button_mc.addEventListener(MouseEvent.MOUSE_OVER, grow) 
button_mc.addEventListener(MouseEvent.MOUSE_OUT, shrink)
button_2_mc.addEventListener(MouseEvent.MOUSE_OVER, grow)
button_2_mc.addEventListener(MouseEvent.MOUSE_OUT, shrink)

function grow(e:MouseEvent):void { 
    
	var sX:Tween = new Tween(e.currentTarget, "scaleX", Regular.easeOut, e.currentTarget.scaleX, 1.5, 0.5, true) 
    var sY:Tween = new Tween(e.currentTarget, "scaleY", Regular.easeOut, e.currentTarget.scaleY, 1.5, 0.5, true)
	var area:Tween = new Tween(e.currentTarget.getChildAt(1),"alpha",Regular.easeInOut,e.currentTarget.getChildAt(1).alpha,1,.4,true);
	
} 

function shrink(e:MouseEvent):void { 
     
    var sX:Tween = new Tween(e.currentTarget, "scaleX", Regular.easeOut, e.currentTarget.scaleX, 1, 0.5, true) 
    var sY:Tween = new Tween(e.currentTarget, "scaleY", Regular.easeOut, e.currentTarget.scaleY, 1, 0.5, true)
	var area:Tween = new Tween(e.currentTarget.getChildAt(1),"alpha",Regular.easeInOut,e.currentTarget.getChildAt(1).alpha,0,.2,true);
	
} 

Open in new window

Many thanks :]
0
Comment
5 Comments
 
LVL 5

Assisted Solution

by:edchipman
edchipman earned 100 total points
ID: 33456641
Looks like the mouse over event is being called when its shrinking as well, one way to stop this would be to remove the event listeners on that button when its in transition. See the code snippit for how to do this.

import fl.transitions.Tween; 
import fl.transitions.TweenEvent; 
import fl.transitions.easing.*; 
import flash.events.* 

button_mc.addEventListener(MouseEvent.MOUSE_OVER, grow) 
button_mc.addEventListener(MouseEvent.MOUSE_OUT, shrink)
button_2_mc.addEventListener(MouseEvent.MOUSE_OVER, grow)
button_2_mc.addEventListener(MouseEvent.MOUSE_OUT, shrink)

function grow(e:MouseEvent):void { 
    //Remove listeners
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, grow);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, shrink);
	
	
	var sX:Tween = new Tween(e.currentTarget, "scaleX", Regular.easeOut, e.currentTarget.scaleX, 1.5, 0.5, true) 
    var sY:Tween = new Tween(e.currentTarget, "scaleY", Regular.easeOut, e.currentTarget.scaleY, 1.5, 0.5, true)
	var area:Tween = new Tween(e.currentTarget.getChildAt(1),"alpha",Regular.easeInOut,e.currentTarget.getChildAt(1).alpha,1,.4,true);
	
	//using sX as a target as it an sY are the longest tweens
	sX.addEventListener(TweenEvent.MOTION_FINISH, enableListeners);
} 

function shrink(e:MouseEvent):void { 
    //Remove listeners
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, grow);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, shrink);
     
    var sX:Tween = new Tween(e.currentTarget, "scaleX", Regular.easeOut, e.currentTarget.scaleX, 1, 0.5, true) 
    var sY:Tween = new Tween(e.currentTarget, "scaleY", Regular.easeOut, e.currentTarget.scaleY, 1, 0.5, true)
	var area:Tween = new Tween(e.currentTarget.getChildAt(1),"alpha",Regular.easeInOut,e.currentTarget.getChildAt(1).alpha,0,.2,true);
	
	//using sx as a target as it an sy are the longest tweens
	sX.addEventListener(TweenEvent.MOTION_FINISH, enableListeners);
}

function enableListeners(e:TweenEvent):void {
	//Clean up
	e.currentTarget.removeEventListener(TweenEvent.MOTION_FINISH, enableListeners);
	
	
	//Redefine Listeners based on the tween's target
	e.currentTarget.obj.addEventListener(MouseEvent.MOUSE_OVER, grow);
	e.currentTarget.obj.addEventListener(MouseEvent.MOUSE_OUT, shrink);
}

Open in new window

0
 
LVL 5

Expert Comment

by:edchipman
ID: 33456701
Oh and tweens have this nasty habbit of being cleaned up by the garbage collector before they complete, the trick to fixing this is to apply a listener to them then remove that listener when it completes. Have a look at http://www.flashmorgan.com/index.php/2007/11/18/as3-garbage-collection-the-reason-your-tweens-are-ending-early/ for more information on that issue :).
0
 
LVL 5

Assisted Solution

by:truromeo4juliet
truromeo4juliet earned 100 total points
ID: 33456773
you need to incorporate an if statement to check a variable to see if the object is currently on the screen, and only run the tweek if it's visible... otherwise, flash will restart from the original variables of the clips you're modifying. I could give the exact code you may need, but I'm not at home right now, sorry. :(
0
 
LVL 39

Accepted Solution

by:
blue-genie earned 300 total points
ID: 33456964
Hi, you're the second one this week.
you need to set the mouseChildren properties to false

button_mc.mouseChildren = false;
button_2.mouseChildren = false;

the thing is the mc inside is triggering the mouse over etc as well
0
 

Author Comment

by:wheelsfalloffbandwagons
ID: 33462466
Thanks Blue Genie, I thought as much! When I didn't move the mouse over the text it was okay!!! Thanks for the other assistance though, it gave me a good insight into what I was doing :] Points for all!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How width columns automaticaly 4 435
Failed download 57 570
Adobe flash player fails to install 12 313
black magic design 1 130
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question