Question 1: When i try to get the name of every added mc i get the name of the last added mC. why is this happening? i’m on beginner level. I have search on the internet for a while but can’t find a solution. [SOLVED]
Question 2: i have 4 positions , how to put every Item (the 4 vruchtItem) randomly in one of those positions like (appel at position posUp, banaan at posRight etc etc) next time they randomly get other positions.
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;
}
You could do something like this for your random positioning function. This assumes your four assets are the only children of a parent container that you’d pass to the function, Super easy to have it work the same way with an array of your assets: