So, i'm attempting simple card game. I have player
"class" with draw
function, and public members deck
and hand
, both are Arrays.
I need to draw a card from the deck, put it in hand and show it in player "hand" area. I'm concerned about the way I do "flip" and "play" buttons (through closures).
Here is the code:
littlegame.player.prototype.draw = function() {
if (this.canDrawCard()) {
var card = this.deck.draw(); // this.deck is Array
//Create card element on the playfield
var card_object = $('<div class="card"></div>').append($('<span class="attack">' + card.attack + '</span>')).append($('<span class="defence">' + card.defence + '</span>'));
// Add controls to card
if (this.playerid == 1) {
var flipper = $('<span class="flip">Flip</span>');
flipper.click(function(){
card.flip();
});
var actuator = $('<span class="play">Play</span>');
console.log('Loading actuator closure with id ' + this.playerid + ' and name ' + this.playername);
var player = this;
var old_hand = this.hand.slice(0); // Store a copy of old hand. Stupid trick, i know. It doesn't work
actuator.click(function(){
card.play(player.playerid);
delete card;
player.hand = old_hand;
});
card_object = card_object.append(flipper).append(actuator);
}
this.element.append(card_object);
card.element = card_object;
// Put card in hand
this.hand.push(card);
}
};
What i need is a way to call card.play()
and card.flip()
on corresponding button presses, with card.play
knowing card position in hand, to remove the card. How can I do this?