How to Control Multiple Children Individually Within an Array?
I'm trying to make an online card game, and though I got an array to add multiple instances of a single card onto the stage, I made some code so you should be able to click and drag the cards around on the screen. But it seems that only the LAST one created can be dragged around.
So here is the array, it's sort of sloppy right now but I was just programming something to make sure it was going to work:
var array : Array = new Array();
function createCard():void
{
var p1_card : Minion = new Minion();
p1_card.x = Math.random() * 400;
p1_card.y = 900;
addChild(p1_card);
array.push(p1_card);
}
createCard();
createCard();
createCard();
This creates 3 cards, all the same instance.
Now here's the script telling it to, when clicked on, the card can be dragged around:
for each (var p1_card in array)
{
addEventListener(Event.ENTER_FRAME, p1_engine);
function p1_engine(e:Event):void
{
if (p1_card_drag == "true")
{
p1_card.x = mouseX;
p1_card.y = mouseY;
addEventListener(MouseEvent.CLICK, _click);
}
p1_card.addEventListener(MouseEvent.CLICK, click_card);
function click_card(event:MouseEvent):void
{
p1_card_drag = "true";
}
function _click(e:MouseEvent):void
{
if (p1_card_drag=="true")
{
p1_card_drag = "false";
p1_card.x = 960;
p1_card.y = p1_card_y;
}
}
removeEventListener(MouseEvent.CLICK, _click);
}
}
}
Basically, the script makes it so when you click on the card, it begins to drag it, and after you click anywhere else with it, it makes it appear in a specific location. But again, for some reason, this only seems to apply to the LAST instance created, rather than ALL of them. Anyone know what I'm doing wrong? Thank you.
