3


I am trying to make a simple card matching game via flash + actionscript, and am having major trouble with assigning event listeners and name. I have got all the card generation statements in place, and they all draw onto my stage, but even though I assigning their instance name with newCard.name, the name I get when tracing the click is always "root1" on every single button, and I have no idea why.

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;

public dynamic class cardGameMain extends MovieClip {

    public function cardGameMain() {
        addCards();
    }
    public function addCards() {
        var lastCard:int;
        for (var i = 1; i < 17; i++) {
            var newCard:MovieClip;
            newCard = new cardBackSymbol();
            newCard.name = "card" + i;
            addChild(newCard);
            newCard.addEventListener(MouseEvent.MOUSE_UP, decideCard);
            if (i == 1 || i == 5 || i == 9 || i == 13) {
                newCard.x = 20;
                if (i == 1) {
                    newCard.y = 20;
                }
                else if (i == 5) {
                    newCard.y = 240;
                }
                else if (i == 9) {
                    newCard.y = 460;
                }
                else if (i == 13) {
                    newCard.y = 680;
                }
                lastCard = 20;
            } else if (i > 1 && i < 5) {
                newCard.x = lastCard + 145;
                newCard.y = 20;
                lastCard = lastCard + 145;
            } else if (i > 5 && i < 9) {
                newCard.x = lastCard + 145;
                newCard.y = 240;
                lastCard = lastCard + 145;
            } else if (i > 9 && i < 13) {
                newCard.x = lastCard + 145;
                newCard.y = 460;
                lastCard = lastCard + 145;
            } else {
                newCard.x = lastCard + 145;
                newCard.y = 680;
                lastCard = lastCard + 145;
            }
            trace(newCard.name + " position is " + newCard.x + ", " + newCard.y);
        }
    }

    public function decideCard(e:MouseEvent):void {
        trace(this.name)
    }
}

}

Any help on the matter is MUCH appretiated!

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40

1 Answers1

1

You're using the this keyword which is referring to the containing class, not the object clicked.

Try this instead:

public function decideCard(e:MouseEvent):void {
    trace(DisplayObject(e.currentTarget).name)
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • @londondrugs-mediaservices Thank you so much! I had to change it slightly and replace DisplayObject with MovieClip and it works perfectly!

    Just a quick question again whilst I am here, eventually inside the decideCard function I am going to be using that name to call cerrtain things out of an array. If I do this, will the same way work? (e.g. if (MovieClip(e.currentTarget).name == card1))
    – user1801301 Nov 05 '12 at 21:42
  • @user1801301 - Odd, the name property is inherited from DisplayObject so it should work, but no matter... To answer your question yes within the decideCard function that line would work (assuming you use "card1" in quotes as it's a string your evaluating) – BadFeelingAboutThis Nov 05 '12 at 21:48