Skip to main content
Inspiring
November 19, 2015
Answered

How to Control Multiple Children Individually Within an Array?

  • November 19, 2015
  • 1 reply
  • 625 views

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.

This topic has been closed for replies.
Correct answer kglad

there are numerous problems with that code.

use:

var array : Array = new Array();

var p1_card:Minion;

function createCard():void

{

    p1_card = new Minion();

    p1_card.x = Math.random() * 400;

    p1_card.y = 900;

p1_card.addEventListener(MouseEvent.CLICK,click_card);

p1_card.clickNum=0;  // assuming Minion extends movieclip or, at least, is dynamic

p1_card.endXY = whatever end x,y you want when clicked a 2nd time.

    addChild(p1_card);

    array.push(p1_card);

}

function click_card(e:MouseEvent):void{

Minion(e.currentTarget).clickNum++;

if(Minion(e.currentTarget).clickNum%2==1){

e.currentTarget.startDrag();

} else {

e.currentTarget.x=e.currentTarget.y=e.currentTarget.endXY;

// remove click listener if you want to disable repeated dragging

}

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 19, 2015

there are numerous problems with that code.

use:

var array : Array = new Array();

var p1_card:Minion;

function createCard():void

{

    p1_card = new Minion();

    p1_card.x = Math.random() * 400;

    p1_card.y = 900;

p1_card.addEventListener(MouseEvent.CLICK,click_card);

p1_card.clickNum=0;  // assuming Minion extends movieclip or, at least, is dynamic

p1_card.endXY = whatever end x,y you want when clicked a 2nd time.

    addChild(p1_card);

    array.push(p1_card);

}

function click_card(e:MouseEvent):void{

Minion(e.currentTarget).clickNum++;

if(Minion(e.currentTarget).clickNum%2==1){

e.currentTarget.startDrag();

} else {

e.currentTarget.x=e.currentTarget.y=e.currentTarget.endXY;

// remove click listener if you want to disable repeated dragging

}

}

Inspiring
November 19, 2015

Thank you very much, after I got the code working, there is one issue: for some reason, when I try to click on the card, I get this error:

TypeError: Error #1006: startDrag is not a function.

    at Hearthclone_Scene1_fla::MainTimeline/click_card()


I don't understand why it's doing this.

function click_card(e:MouseEvent):void

{

    Minion_MurlocRaider(e.currentTarget).clickNum++;

    if (Minion_MurlocRaider(e.currentTarget).clickNum % 2 == 1)

    {

        e.currentTarget.startDrag();

    }

    else

    {

        e.currentTarget.x = 900;

        e.currentTarget.y = 900;

    }

}

That's the part it's screwing up at. Any ideas at all? Thank you.

kglad
Community Expert
Community Expert
November 19, 2015

Minion_MurlocRaider should extend the sprite class and be dynamic