问题1:当我尝试获取每个添加的mc的名称时,我得到最后添加的mC的名称。为什么会这样?我在初级水平。我在互联网上搜索了一段时间但找不到解决方案。 [解决]
问题2:我有4个职位,如何将每个项目(4个vruchtItem)随机地放在其中一个位置,如下一次随机获得其他位置时(如posl位置,banaan at posRight等等)。
public function vruchtenItemKruis(e:Event):void
{
var vruchtArray:Array = new Array("Aardbei", "Appel", "Banaan", "Sinaasappel");
var i:Number= 0;
while (i < vruchtArray.length)
{
var vrucht = vruchtArray[i];
switch(vrucht)
{
case ("Aardbei"):
vruchtItem = new Aardbei(kruisWeg.width / 2, 50, 50, 65);
break;
case ("Appel"):
vruchtItem = new Appel(kruisWeg.width / 2, kruisWeg.height - 50, 50, 60);
break;
case ("Banaan"):
vruchtItem = new Banaan(50, kruisWeg.height / 2, 60, 40);
break;
case ("Sinaasappel"):
vruchtItem = new Sinaasappel(kruisWeg.width - 50, kruisWeg.height / 2, 70, 45);
break;
}
removeEventListener(Event.ADDED_TO_STAGE, vruchtenItemKruis);
i++;
addChild(vruchtItem);
vruchtItem.name = vrucht;
trace(vruchtItem.name);
vruchtItem.buttonMode = true;
vruchtItem.addEventListener(MouseEvent.MOUSE_OVER, vruchtNaam);
}
}
public function randomPosXY(xPos:int, yPos:int):int
{
xPosition = xPos;
yPosition = yPos;
// Select an random positie van 4
var vruchtPos:Array = new Array("posUp", "posRight", "posDown", "posLeft");
var randomPos:int = Math.floor(Math.random() * vruchtPos.length);
var posXY:String = vruchtPos[randomPos];
//trace ("random xy started");
var tot:Number;
if(posXY)
{
while (tot < vruchtPos.length) {
var itemPos = vruchtPos[tot];
switch(posXY)
{
case ("posUp"):
xPosition = kruisWeg.width / 2;
yPosition = 0 + 15;
break;
case ("posDown"):
xPosition = kruisWeg.width / 2;
yPosition = kruisWeg.height - 15;
break;
case ("posLeft"):
xPosition = 0 + 15;
yPosition = kruisWeg.height / 2;
break;
case ("posRight"):
xPosition = kruisWeg.width - 15;
yPosition = kruisWeg.height / 2;
break;
}
}
}
return xPosition;
//return yPosition;
}
答案 0 :(得分:0)
预先设置您的位置列表,然后在创建每个MC时,从列表中取出一个位置并将其删除。当您添加下一个MC时,将只有3个位置,其中一个随机删除。等等。
答案 1 :(得分:0)
你可以为你的随机定位功能做这样的事情。这假设您的四个资产是您传递给该函数的父容器的唯一子项,超级容易让它与您的资产数组一样工作:
public function randomPosXY(itemParent:DisplayObjectContainer):void
{
var vruchtPos:Array = new Array("posUp", "posRight", "posDown", "posLeft");
var xPosition:Number;
var yPosition:Number;
var i:int = itemParent.numChildren;
while (i--) {
var randomPos:int = Math.round(Math.random() * (vruchtPos.length - 1));
switch(vruchtPos[randomPos]) {
case ("posUp"):
xPosition = kruisWeg.width / 2;
yPosition = 0 + 15;
break;
case ("posDown"):
xPosition = kruisWeg.width / 2;
yPosition = kruisWeg.height - 15;
break;
case ("posLeft"):
xPosition = 0 + 15;
yPosition = kruisWeg.height / 2;
break;
case ("posRight"):
xPosition = kruisWeg.width - 15;
yPosition = kruisWeg.height / 2;
break;
}
//assign position to current item
itemParent.getChildAt(i).x = xPosition;
itemParent.getChildAt(i).y = yPosition;
//remove the used position from the array
vruchtPos.splice(randomPos, 1);
}
}