그래서 간단한 카드 게임을 시도하고 있습니다. 내가 player
"클래스"draw
함수를 가지고 있으며 공개 멤버 deck
과 hand
, 모두 배열입니다.자바 스크립트 카드 게임에서 플레이어 핸드의 아키텍처
갑판에서 카드를 가져와 손에 넣고 플레이어의 "손"영역에 표시해야합니다. 나는 "플립 (flip)"과 "플레이 (play)"버튼을 (클로저를 통해)하는 방법에 대해 우려하고 있습니다. 내가 뭘 필요로하는 카드를 꺼내 손에 카드 위치를 알고 card.play
로, 버튼 누름 대응에 card.play()
및 card.flip()
를 호출 할 수있는 방법이
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);
}
};
입니다 : 여기
는 코드입니다. 어떻게해야합니까?
흠. 제 3의 카드를 손에서 떼어 내고 남은 카드가 처음부터 1 씩 이동하지 않는 경우 .delete'는 멋지다, 이제 시도해 보겠습니다. –
네,하지만 설계 문서를 가지고 있지 않아서 당신이 어떤 카드 게임을하고 있는지 모르겠습니다. – Breton
사실, 디자인 문서가 없습니다 :) 배열 요소가 삭제 될 때 작동을 멈추지 않도록 배열의 개체에 대한 영구 링크를 저장하는 것이 문제였습니다. 어쨌든 실제로 접근 방식이 잘 작동하는 것 같습니다. 감사 :) –