Solved

AS3 scale and alpha tweening

Posted on 2010-08-17
5
950 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

696 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