• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

make children with addChild and for loop butt up against each other?

Mentor ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

I have the following code:

import flash.display.MovieClip;

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

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);

addChild(square_mc);

square_mc.scaleX=.25+(.13*Math.sin(i/2)+.03*Math.abs((Math.sin(i*2)/2+Math.cos(i*2)/2)));

square_mc.scaleY=square_mc.scaleX;

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

square_mc.y=i*(45+2*i);

}

I would like the square_mc's to sit against each other and I can't figure out the proper math to put into the square_mc.y parameter to make them do so.  I thought about using the square_mc.height parameter or the square_mc.scaleX parameter in conjunction with i to sit them right one above each other but when I tried that it didn't work.

Anyone able to help me with some simple math?

-markerline

TOPICS
ActionScript

Views

602

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

:


import flash.display.MovieClip;

var nextY:Number = 0;

var yGap:int = 2;

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

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);

addChild(square_mc);

square_mc.scaleX=.25+(.13*Math.sin(i/2)+.03*Math.abs((Math.sin(i*2)/2+ Math.cos(i*2)/2)));

square_mc.scaleY=square_mc.scaleX;

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

square_mc.y=nextY;

nextY+=square_mc.height+yGap;

}

I would like the square_mc's to sit against each other and I can't figure out the proper math to put into the square_mc.y parameter to make them do so.  I thought about using the square_mc.height parameter or the square_mc.scaleX parameter in conjunction with i to sit them right one above each other but when I tried that it didn't work.

Anyone able to help me with some simple math?

-markerline

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

Here's one approach...

var currentY:Number = 0;

for (var i:int=0; i<10; i++){
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);
addChild(square_mc);
square_mc.scaleX=.25+(.13*Math.sin(i/2)+.03*Math.abs((Math.sin(i*2)/2+ Math.cos(i*2)/2)));
square_mc.scaleY=square_mc.scaleX;
square_mc.alpha=i*.1+.3;
square_mc.y=currentY;
currentY += square_mc.height;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

Two of the top experts in this forum helped me with the same issue and I like both of your answers.  I will give each of you "helpful" since I can't assign 2 correct answers.  I hope that resonates ok with you guys.

And you DID use the height property but why it didn't work for me I will not know.

This is what I had at one point during the process:

import flash.display.MovieClip;

for (var i:int=0; i<10; i++){
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);
addChild(square_mc);
square_mc.scaleX=.25+(.13*Math.sin(i/2)+.03*Math.abs((Math.sin(i*2)/2+Math.cos(i*2)/2)));
square_mc.scaleY=square_mc.scaleX;
square_mc.alpha=i*.1+.3;
//square_mc.y=i*(45+2*i);
square_mc.y=i*(square_mc.height);


}

I wonder why this math does not work properly.  Eventually I will use this to create a scrolling thumbnail menu with the alpha being 1 for all children but the children being different thumbnails.  I may post again if I have trouble along the way of achieving the scrolling part, where the mouseY position will alter the inverse Y position of the children and also cause them to change in size according to the sine wave function that I implemented above (square_mc.scaleX=....).  Looking at my math for that line I see I have added too much so I will probably simplify the code further.

Thank you Both!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

You're welcome... kGlad's the expert, I just help if I can.

That approach didn't work because you are treating the height like it is a constant, but it is not since it is dependent on the  scaleX value which is varying.

square_mc.scaleY=square_mc.scaleX; // this changes the height of the square from one to the next

square_mc.y=i*(square_mc.height); // this would only work if the height was constant

just a fabricated example....

y3 = 3x30=90  // ends at 120 (90 + 30)

y4 = 4x32 = 128 // starts 8 pixels after the previous ends

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 05, 2011 Feb 05, 2011

Copy link to clipboard

Copied

LATEST

Ah, I see I see.  Thanks for that explanation.  I expanded this post into another thread with added development to my interface.  I'm hoping someone comes up with a solution for my issue.  You might find it by clicking on my avatar and searching for my threads if you're interested in helping again.

Best,

-markerline

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines