Solved

AS3 scale and alpha tweening

Posted on 2010-08-17
5
917 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
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,…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now