ActionScript 3: dynamically add container-limited movie clips

Last modified: allowed!

Well, I couldn’t find the ENTIRE answer here, but I finally got what I was after. Thanks so much for your help and patience.

as a side note: I think I might have had problems using the int and Number types, upon a closer look at my solution, I realized that Number was used, not int. it turns out the number contains floating points, but int does not. my numbers were probably rounded whenever I tried to fix it myself. for everyone I know, the TDI answer might be in place, and using int to fill in could have accumulated rounded numbers. Oh well, you learn something every day.

The correct code to limit movie clips in a clip in a container (or hide or something else) in the mod I was looking for is:

var picContainer:PicContainer = new PicContainer();
picContainer.x = stage.stageWidth / 2 - picContainer.width / 2;
picContainer.y = stage.stageHeight / 2 - picContainer.height / 2;
addChild(picContainer);

var totalPics:int = 17;

var pic:Pic = new Pic(); //make an instance just to get its width

var xRange:Number = picContainer.width - pic.width;
var spacing:Number = xRange / (totalPics - 1);

//A little optimization: only need to calculate this number ONCE:
var yLoc:Number = picContainer.height / 2 - pic.height / 2;

for(var i:int = 0; i < totalPics; i++) {
    pic = new Pic();
    pic.x = i * spacing;
    pic.y = yLoc;
    picContainer.addChild(pic);
}

the logic is pretty simple, and I don’t know why I couldn’t get it myself, because I was drawing diagrams that say exactly that logic. however, I did not have to put the numbers in the right places, or I would not have to ask if I would; P

BONUS CONTENT! as an added bonus (if someone finds this thread looking for answers ..) you can also have a “pic fan” from the center point (but they will still be in order from left to right) using this code:

var picContainer:PicContainer = new PicContainer();
picContainer.x = stage.stageWidth / 2 - picContainer.width / 2;
picContainer.y = stage.stageHeight / 2 - picContainer.height / 2;
addChild(picContainer);

var totalPics:int = 5;

var pic:Pic = new Pic(); //make an instance just to get its width

var padding:Number = (picContainer.width - (pic.width * totalPics))  / (totalPics + 1);

for(var i:int = 0; i < totalPics; i++) {
    pic = new Pic();
    pic.x = padding + i * (pic.width + padding);
    pic.y = picContainer.height / 2 - pic.height / 2;
    picContainer.addChild(pic);
}

Try it, they will make docking stations for large engines!

First edit: Well, there is some progress thanks to TDI, but not a complete solution.

, , , .

example:

alt text

:

var newPicContainer:picContainer = new picContainer();
var newPic:pic;

var picwidth:int = 100;
var amountofpics:int = 22;
var i:int;

//add a container
addChild(newPicContainer);

//center our container
newPicContainer.x = (stage.stageWidth/2)- (newPicContainer.width/2);
newPicContainer.y = (stage.stageHeight/2)- (newPicContainer.height/2);

var totalpicwidth:int = picwidth*amountofpics;

var totalpadding:int = newPicContainer.width - totalpicwidth;

var paddingbetween:int = (totalpadding / amountofpics);

for (i = 0; i < amountofpics; ++i)
{
    //make a new mc called newPic(and i value)  eg. newPic1
    this['newPic' + i] = new pic();
    this['newPic' + i].width = picwidth;

    //add our pic to the container
    newPicContainer.addChild(this['newPic' + i]);

    //set the new pics X
    if (i != 0)
    {
        // if i is not 0, set newpic(i)s x to the previous pic plus the previous pics width and add our dynamic padding 
        this['newPic' + i].x = this['newPic' + (i-1)].x + picwidth + paddingbetween;
    }
    else
    {
        this['newPic' + i].x = 0;
    }
}

!

:

, . , . :

, "" ( ) .

var newPicContainer:picContainer = new picContainer();
var newPic:pic;
var amount:int = 9;
var i:int;

//Add our container
addChild(newPicContainer);

//center our container
newPicContainer.x = (stage.stageWidth/2)- (newPicContainer.width/2);
newPicContainer.y = (stage.stageHeight/2)- (newPicContainer.height/2);


for (i = 0; i < amount; ++i)
{
 newPic = new pic();
 newPicContainer.addChild(newPic);

    //just so i know it adding them..
    trace(newPic.thisOne);
 newPic.thisOne = i;

    // set this one to its self (+5 for padding..) Times, the amount already placed. 
 newPic.x = (newPic.width+5) *i;
}

, - " ", , , , "" . , .

- :

newPic.x = (newPic.width *i) - stuff here to make it know not to go past the containing width;

, , .

.

+3
3

, width:

//Container Width    
newPicContainer.width;

newContainer :

//Container Width    
newPic.parent.width;

, , pics:

var arrayOfPics:array = [pic1, pic2, pic3, pic4, pic5];
var picsWidth:Number;

for each (var element:pic in arrayOfPics)
         picsWidth =+ element.width;

, , :

var totalPadding:Number = newPicContainer.width - picsWidth;

, , totalPadding .

var padding:Number = totalPadding / arrayOfPics.length + 1;

,

for (var i:int = 0; i < arrayOfPics.length; i++)
    {
    newPicContainer.addChild(arrayOfPics[i]);
    (i == 0) ? arrayOfPics[i].x = padding : arrayOfPics[i].x = arrayOfPics[i - 1].x + arrayOfPics[i - 1].width + padding;
    }
+2

...

 //maximum available length
 var maxLength:int; 

 // a single thumbnail width
 var picWidth:int = 100;

 // total number of pics in a container
 var maxNumPics:int;

 // as long as the maximum available length
 // is inferior to the container length 
 // add a new thumbnail
 while( maxLength < newPicContainer.length - 100 )
 { 
    maxLength += 100;
    maxNumPics += 1;
 } 

 // calculate the amount of available padding.
 var padding:Number =  ( newPicContainer.length - maxLength )/ maxNumPics; 

 //add your thumbs
 var picX:int;

 for( var i:int ; i < maxNumPics ; ++i )
 {
     var newPic:Pic = new Pic();
     newPic.x = picX;

     picX += padding + picWidth;
     newPicContainer.addChild( newPic ); 
 }
+1

Flex ( ), .

, , , .

Flex

Flex

0

Source: https://habr.com/ru/post/1769921/


All Articles