Okay, it's AS3 newbie question time again. (Sorry.) Same card game as
before--this time, I'm trying to make the cards drag-and-drop. All code shown here is in the document class.
This code, which isn't for dragging-and-dropping, works perfectly:
function showDeck () {
var cardMCsArray:Array = new Array ();
for (i = 0 ; i < playerDeck.length ; i++ ) {
var myCard:Card = new Card(playerDeck[i][1] , playerDeck[i][2]);
myCard.name = "card" + i;
addChild(myCard);
myCard.x = 10;
myCard.y = 40 + (i * 25);
myCard.rotation = 0;
myCard.gotoAndStop(playerDeck[i][0]);
cardMCsArray.push(myCard);
cardMCsArray[i].addEventListener(MouseEvent.CLICK, selectCard);
function selectCard (event:MouseEvent):void{
trace(event.currentTarget.name);
}
}
}
This, on the other hand, fails miserably:
function showDeck () {
var cardMCsArray:Array = new Array ();
for (i = 0 ; i < playerDeck.length ; i++ ) {
var myCard:Card = new Card(playerDeck[i][1] , playerDeck[i][2]);
myCard.name = "card" + i;
addChild(myCard);
myCard.x = 10;
myCard.y = 40 + (i * 25);
myCard.rotation = 0;
myCard.gotoAndStop(playerDeck[i][0]);
cardMCsArray.push(myCard);
//MAKE DRAGGABLE
cardMCsArray[i].addEventListener(MouseEvent.MOUSE_DOWN, startCardDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, stopCardDrag);
cardMCsArray[i].addEventListener(Event.ENTER_FRAME, dragCard);
var clickOffset:Point = null;
function startCardDrag(event:MouseEvent) {
clickOffset = new Point(event.localX, event.localY);
}
function stopCardDrag(event:MouseEvent) {
clickOffset = null;
}
function dragCard(event:Event) {
if (clickOffset != null) { //if Card is being dragged
cardMCsArray[i].x = mouseX - clickOffset.x;
cardMCsArray[i].y = mouseY - clickOffset.y;
}
}
}
}
It just returns an error:
TypeError: Error #1010: A term is undefined and has no properties.
at MethodInfo-11()
I'm sure that this error has something to do with the way I'm referencing the cards in the dragCard function, but I'm not sure how else to do it. (Using
myCard in place of the array call results in Flash only performing drag-and-drop with the last card it puts down.)
Any ideas?