[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

AS3 scale and alpha tweening

Posted on 2010-08-17
5
Medium Priority
?
968 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 400 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 400 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses

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