处理游戏项目,我正在尝试创建一个onclick事件,将.character div从一个数组移动到另外两个数组。我正在尝试移动最多4个字符。由于它是两个独立的屏幕,我可以从字符数组转换到聚会数组,然后将聚会移动到inCombat数组。我正在使用jQuery与javascript。代码现在很乱,但我会告诉你我有什么。
HTML:
<section id="team-character-select">
<p class="select-button-contain"><button id="char-select">Char Select</button></p>
<article id="selected-characters"></article>
</section>
<section id="combat-arena">
<article id="player-section">
<div class="player-characters" id="attacker-one"></div>
<div class="player-characters" id="attacker-two"></div>
<div class="player-characters" id="attacker-three"></div>
<div class="player-characters" id="attacker-four"></div>
</article>
JS:
var characters = {
"Guy1": {
name: "Guy1",
health: 120,
attack: 8
}
};
var attacker1;
var attacker2;
var attacker3;
var attacker4;
var party = ["", "", "", ""]; // 4
var unUsed = [];
var renderCharacter = function (character, renderArea) {
// this block of code builds the character card, and renders it to the page.
var charDiv = $("<div class='character' data-name='" + character.name + "'>");
var charName = $("<div class='character-name'>").text(character.name);
var charImage = $("<img alt='image' class='character-image'>").attr("src", character.imageUrl);
var charHealth = $("<div class='character-health'>").text(character.health);
charDiv.append(charName).append(charImage).append(charHealth);
$(renderArea).append(charDiv);
};
var initializeGame = function () {
$("#start-game").click(function () {
// Loop through the characters object and call the renderCharacter function on each character to render their card
for (var key in characters) {
renderCharacter(characters[key], "#team-character-select");
}
}, )
};
initializeGame();
$(".character").click(function () {
party.push(".character");
document.getElementById("#selected-characters").innerHTML = party;
console.log(party);
});