Skip to main content
markerline
Inspiring
February 5, 2011
Answered

How can I prevent children from shrinking to infinitesimal values on MouseEvents?

  • February 5, 2011
  • 2 replies
  • 849 views

Hi.

I have the following code:

////////////////////////////////////////////////////////////////////////////////////

//http://forums.adobe.com/message/3450898#3450898

//square_mc.y property edited by kglad AND NedMurphy both

// but kglad introduced the yGap variable, allowing for

// user defined space between movie clips.  This solved my

// problem of evenly distributed spacing between movie clips

// all of the other code I generated from either my own previous

// files or by thinking about the solution and plugging away

// at it.

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.MouseEvent;

var nextY:Number = 0;

var yGap:int = 2;

var container:MovieClip=new MovieClip();

var containerBig:MovieClip=new MovieClip();

containerBig.x=50;

containerBig.y=50;

containerBig.scaleX=1;

containerBig.scaleY=1;

addChild(container);

addChild(containerBig);

stage.addEventListener(Event.ENTER_FRAME, myFunc);

/*var imageHolder_mc:MovieClip=new MovieClip();

imageHolder_mc.graphics.beginFill(0x888888);

imageHolder_mc.graphics.lineTo(300,0);

imageHolder_mc.graphics.lineTo(300,200);

imageHolder_mc.graphics.lineTo(0,200);

imageHolder_mc.graphics.lineTo(0,0);

*/

var myFrames:Array=new Array();

for (var i:int=0; i<12; i++){

myFrames.push("my_"+i+"_mc");

trace(myFrames+" myFrames");

var square_mc:MovieClip=new MovieClip();

square_mc.graphics.beginFill(0xFF0000);

square_mc.graphics.lineTo(200,0);

square_mc.graphics.lineTo(200,200);

square_mc.graphics.lineTo(0,200);

square_mc.graphics.lineTo(0,0);

container.addChild(square_mc);

square_mc.name="my_"+i+"_mc";

trace(square_mc.name);

square_mc.addEventListener(MouseEvent.MOUSE_OVER, myFunc2);

square_mc.addEventListener(MouseEvent.MOUSE_OUT, myFunc3);

//square_mc.scaleX=.25+(.13*Math.sin(i/Math.PI))*mouseY*.005;

square_mc.scaleX=.25;

square_mc.scaleY=square_mc.scaleX;

square_mc.alpha=i*.1+.3;

square_mc.y=nextY+mouseY*(-1);

nextY+=square_mc.height+yGap;

}

function myFunc(e:Event):void{

container.y=-1*mouseY+stage.height;

trace(container.y+" containerY");

}

function myFunc2(e:MouseEvent):void{

e.target.scaleX*=1.2;

trace(stage.height+" stageHeight");

e.target.addEventListener(MouseEvent.MOUSE_DOWN, myFunc4);

e.target.mouseChildren=false;

}

function myFunc3(e:MouseEvent):void{

e.target.scaleX/=1.2;

e.target.removeEventListener(MouseEvent.MOUSE_OVER, myFunc2);

}

//add ImageHolder_mc MovieClip from Library and go to frame

// coinciding with Array value in for loop . . .

var imageHolder_mc:ImageHolder_mc=new ImageHolder_mc();

function myFunc4(e:MouseEvent):void{

containerBig.addChild(imageHolder_mc);

for(var j:int=0; j<12; j++){

if(e.target.name=="my_"+j+"_mc"){

imageHolder_mc.gotoAndStop(j);

}

}

trace(e.target.y+" e.target.y");

}

////////////////////////////////////////////////////////////////////////////////////

you can troubleshoot the issue by adding a 12 frame MovieClip to your Library when you test this code and give it the names I referenced above in Bold.

That is, whomever will have time to help me out.

The problem I'm facing is not with loading the content of the Movie Clip, (though I might experience issues with that later when I try to load dynamic swfs) because as for now I just have placeholders on frames.

What I want to know is why my red squares on the left navigation keep getting smaller as I repeat the MouseOver and MouseOut events.  It doesn't seem that these squares follow the logic of the code the way I have written it.  Some of the code was generated from a previous post with the help of contributors.

Now I have again hit a slight snag with coding the math properly.  Initially when I tested the code it worked fine without any "shrinking" going on.  However, now with more code added the red navigation squares don't bounce back to their original states, they just keep getting smaller.

Can anyone help me with this?

Kindly thanking in advance,

-markerline

This topic has been closed for replies.
Correct answer kglad

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.MouseEvent;

var alreadyExecuted:Boolean;

if(!alreadyExecuted){

alreadyExecuted=true;

var nextY:Number = 0;

var yGap:int = 2;

var container:MovieClip=new MovieClip();

var containerBig:MovieClip=new MovieClip();

containerBig.x=50;

containerBig.y=50;

containerBig.scaleX=1;

containerBig.scaleY=1;

addChild(container);

addChild(containerBig);

stage.addEventListener(Event.ENTER_FRAME, myFunc);

/*var imageHolder_mc:MovieClip=new MovieClip();

imageHolder_mc.graphics.beginFill(0x888888);

imageHolder_mc.graphics.lineTo(300,0);

imageHolder_mc.graphics.lineTo(300,200);

imageHolder_mc.graphics.lineTo(0,200);

imageHolder_mc.graphics.lineTo(0,0);

*/

var myFrames:Array=new Array();

for (var i:int=0; i<12; i++){

myFrames.push("my_"+i+"_mc");

trace(myFrames+" myFrames");

var square_mc:MovieClip=new MovieClip();

square_mc.graphics.beginFill(0xFF0000);

square_mc.graphics.lineTo(200,0);

square_mc.graphics.lineTo(200,200);

square_mc.graphics.lineTo(0,200);

square_mc.graphics.lineTo(0,0);

container.addChild(square_mc);

square_mc.name="my_"+i+"_mc";

trace(square_mc.name);

square_mc.addEventListener(MouseEvent.MOUSE_OVER, myFunc2);

square_mc.addEventListener(MouseEvent.MOUSE_OUT, myFunc3);

//square_mc.scaleX=.25+(.13*Math.sin(i/Math.PI))*mouseY*.005;

square_mc.scaleX=.25;

square_mc.scaleY=square_mc.scaleX;

square_mc.alpha=i*.1+.3;

square_mc.y=nextY+mouseY*(-1);

nextY+=square_mc.height+yGap;

}

function myFunc(e:Event):void{

container.y=-1*mouseY+stage.height;

trace(container.y+" containerY");

}

function myFunc2(e:MouseEvent):void{

e.target.scaleX*=1.2;

trace(stage.height+" stageHeight");

e.target.addEventListener(MouseEvent.MOUSE_DOWN, myFunc4);

e.target.mouseChildren=false;

}

function myFunc3(e:MouseEvent):void{

e.target.scaleX/=1.2;

e.target.removeEventListener(MouseEvent.MOUSE_OVER, myFunc2);

}

//add ImageHolder_mc MovieClip from Library and go to frame

// coinciding with Array value in for loop . . .

var imageHolder_mc:ImageHolder_mc=new ImageHolder_mc();

function myFunc4(e:MouseEvent):void{

containerBig.addChild(imageHolder_mc);

for(var j:int=0; j<12; j++){

if(e.target.name=="my_"+j+"_mc"){

imageHolder_mc.gotoAndStop(j);

}

}

trace(e.target.y+" e.target.y");

}

}

2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 6, 2011

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.MouseEvent;

var alreadyExecuted:Boolean;

if(!alreadyExecuted){

alreadyExecuted=true;

var nextY:Number = 0;

var yGap:int = 2;

var container:MovieClip=new MovieClip();

var containerBig:MovieClip=new MovieClip();

containerBig.x=50;

containerBig.y=50;

containerBig.scaleX=1;

containerBig.scaleY=1;

addChild(container);

addChild(containerBig);

stage.addEventListener(Event.ENTER_FRAME, myFunc);

/*var imageHolder_mc:MovieClip=new MovieClip();

imageHolder_mc.graphics.beginFill(0x888888);

imageHolder_mc.graphics.lineTo(300,0);

imageHolder_mc.graphics.lineTo(300,200);

imageHolder_mc.graphics.lineTo(0,200);

imageHolder_mc.graphics.lineTo(0,0);

*/

var myFrames:Array=new Array();

for (var i:int=0; i<12; i++){

myFrames.push("my_"+i+"_mc");

trace(myFrames+" myFrames");

var square_mc:MovieClip=new MovieClip();

square_mc.graphics.beginFill(0xFF0000);

square_mc.graphics.lineTo(200,0);

square_mc.graphics.lineTo(200,200);

square_mc.graphics.lineTo(0,200);

square_mc.graphics.lineTo(0,0);

container.addChild(square_mc);

square_mc.name="my_"+i+"_mc";

trace(square_mc.name);

square_mc.addEventListener(MouseEvent.MOUSE_OVER, myFunc2);

square_mc.addEventListener(MouseEvent.MOUSE_OUT, myFunc3);

//square_mc.scaleX=.25+(.13*Math.sin(i/Math.PI))*mouseY*.005;

square_mc.scaleX=.25;

square_mc.scaleY=square_mc.scaleX;

square_mc.alpha=i*.1+.3;

square_mc.y=nextY+mouseY*(-1);

nextY+=square_mc.height+yGap;

}

function myFunc(e:Event):void{

container.y=-1*mouseY+stage.height;

trace(container.y+" containerY");

}

function myFunc2(e:MouseEvent):void{

e.target.scaleX*=1.2;

trace(stage.height+" stageHeight");

e.target.addEventListener(MouseEvent.MOUSE_DOWN, myFunc4);

e.target.mouseChildren=false;

}

function myFunc3(e:MouseEvent):void{

e.target.scaleX/=1.2;

e.target.removeEventListener(MouseEvent.MOUSE_OVER, myFunc2);

}

//add ImageHolder_mc MovieClip from Library and go to frame

// coinciding with Array value in for loop . . .

var imageHolder_mc:ImageHolder_mc=new ImageHolder_mc();

function myFunc4(e:MouseEvent):void{

containerBig.addChild(imageHolder_mc);

for(var j:int=0; j<12; j++){

if(e.target.name=="my_"+j+"_mc"){

imageHolder_mc.gotoAndStop(j);

}

}

trace(e.target.y+" e.target.y");

}

}
markerline
Inspiring
February 6, 2011

I can certainly see why this would work but I never ever thought of wrapping the entire set of functions in a conditional statement.  I thought it was improper form to do so but I guess there are certain circumstances where one might need to do just that.  Thank you one hundred percent!

As an aside . . .

In the other post with the Tween class (http://forums.adobe.com/message/3451253#3451253) I had added the Tweens but I see that for some reason when I add this functionality back into your updated code it still doesn't behave correctly when I'm trying to use e.target.scaleX*1.5 and e.target.scaleX/1.5 .  With hard-coded values it seems to work.  Perhaps a bug in the interpreter or just bad math on my part (likely the latter)

function myFunc2(e:MouseEvent):void

{

var myTween:Tween = new Tween(e.target, "scaleX", Elastic.easeIn, e.target.scaleX*1, e.target.scaleX*1.5, .1, true); // BAD CODE

//e.target.scaleX *=  1.2;

//trace(stage.height+" stageHeight");

e.target.addEventListener(MouseEvent.MOUSE_DOWN, myFunc4);

e.target.mouseChildren = false;

}

function myFunc3(e:MouseEvent):void

{

var myTween:Tween = new Tween(e.target, "scaleX", Elastic.easeIn, e.target.scaleX*1, e.target.scaleX/1.5, .1, true); // BAD CODE

// e.target.scaleX /=  1.2;

//e.target.removeEventListener(MouseEvent.MOUSE_OVER, myFunc2);

}

kglad
Community Expert
Community Expert
February 6, 2011

you're welcome.

it's never a good idea to nest named functions inside other functions, but it's ok to next named function inside if-statements.

it's not really necessary though.  the functions could be outside that if-statement.

kglad
Community Expert
Community Expert
February 6, 2011

that code should only execute once.  use a boolean to prevent it from re-executing and adding more and more listeners when your frame replays.

markerline
Inspiring
February 6, 2011

Okay, I'll give it a shot.  Thanks for your response.  When I solve it I will come back and award points (or I might award them beforehand, since I definitely can take your word for it). 

kglad
Community Expert
Community Expert
February 6, 2011

sounds good.

that will look like:

///////////////////////////////////////

var alreadyExecuted:Boolean;

if(!alreadExecuted){

alreadyExecuted=true;

.

.

// your code

.

.

}

///////////////////////////////////