actionscript-3 - ActionScript 3 : Dynamically adding movieclips constrained to a container

标签 actionscript-3 for-loop actionscript image-gallery

上次编辑:已解决!

好吧,我无法在这里找到完整的答案,但我终于得到了我想要的。非常感谢您的所有帮助和耐心等待。

作为旁注:我想我可能在使用 int 和 Number 类型时遇到问题,在仔细检查我的解决方案后,我意识到正在使用 Number 而不是 int。结果 number 包含 float 而 int 不包含。每当我试图自己解决这个问题时,我的数字可能会四舍五入。据我所知,TDI 的答案可能是正确的,使用 int 进行填充可能会累积四舍五入的数字。哦,好吧,你每天都会学到一些东西。

以我一直在寻找的方式将影片剪辑限制为容器影片剪辑(或 Sprite 或其他)的正确代码是:

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

逻辑很简单,我不知道为什么我自己搞不懂,因为我画的图就是这个逻辑。但是,我一定没有把数字放在正确的地方,否则我就不必问了,我会吗 ;P

奖励内容! 作为额外的奖励(如果有人发现此线程正在寻找答案..) 您还可以使用以下代码使“图片”从中心点散开(但它们仍然按从左到右的顺序排列):

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

试一试,这些都是很棒的缩略图停靠引擎!

第一次编辑:好吧,TDI 带来了一些进步,但还不是一个完整的解决方案。

你看,问题仍然是影片剪辑没有完全挤进容器,最后一两个留在外面。

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's 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;
    }
}

再次感谢任何人!

原帖:

您好,第一次在这里发帖。我希望我没有弄错任何东西。我的问题如下:

我有一个非常基本的 for 循环,它创建一个“缩略图”并将其放在包含影片剪辑中的前一个(带有一点填充)旁边。

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's 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;

我必须承认我的数学不太好,所以这部分编码让我无法理解..

提前感谢任何接受者..

最佳答案

您可以通过显式调用其宽度属性来获取容器的长度:

//Container Width    
newPicContainer.width;

或者 newContainer 也是添加图片的父级:

//Container Width    
newPic.parent.width;

然后你需要得到你的图片占用的总长度:

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

关于actionscript-3 - ActionScript 3 : Dynamically adding movieclips constrained to a container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951202/

相关文章:

actionscript-3 - 神秘的 "watson"函数

JavaScript for 循环导致浏览器崩溃

javascript - 从 Flash 到 Javascript OOP 对象的 ExternalInterface 调用

actionscript-3 - 在 Actionscript 3 中查找前 3 个最接近的目标

actionscript-3 - Actionscript 3外部接口(interface),将变量传递给javascript?

arrays - 如何从长度为 'n' 的数组中删除等距元素以匹配给定长度 'y' ?

ruby-on-rails - Rails 循环遍历表单字段中的数据

c++ - 为什么 C++ 不允许在一个 auto 语句中使用多种类型?

java - FLEX java.home 路径反斜杠

android - 一个 Flex 移动应用程序中的两个 ane 文件发生冲突