问题 1:当我尝试获取每个添加的 mc 的名称时,我会得到最后添加的 mc 的名称。为什么会这样?我处于初级水平。我在互联网上搜索了一段时间,但找不到解决方案。[解决了]
问题 2:我有 4 个位置,下次他们随机获得其他位置时,如何将每个项目(4 个 vruchtItem)随机放置在其中一个位置,例如(在位置 posUp 的 appel,在 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;
}