newpic.x=(newpic.width*i)-这里的东西使它知道不要超过包含宽度;
< /代码>
我必须承认我数学不太好,所以这部分编码让我很难理解。
感谢任何提前接受者..
作为旁注:我想我可能在使用int和number类型时遇到了问题,在仔细检查我的解决方案后,我意识到使用的是数字而不是int。结果发现数字包含浮点,而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
奖金内容!
作为额外的奖励(如果有人发现这个线程正在寻找答案…)
您也可以使用以下代码将“pic”的风扇从中心点移出(但它们仍然按从左到右的顺序排列):
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](https://i.stack.imgur.com/1fqFB.jpg)
我修改的代码如下:
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;
我必须承认我数学不太好,所以这部分编码让我很难理解。
感谢任何提前接受者。