在Javascript/JQuery中锁定/确认选择
我正在用Javascript和jQuery创建一个基于文本的格斗游戏程序。目前我的jQuery知识是基本的(大约1-2周) 格斗游戏程序将包括:在Javascript/JQuery中锁定/确认选择,javascript,jquery,locking,selection,Javascript,Jquery,Locking,Selection,我正在用Javascript和jQuery创建一个基于文本的格斗游戏程序。目前我的jQuery知识是基本的(大约1-2周) 格斗游戏程序将包括: 定义角色名称、hp和攻击强度的对象函数 一个对象数组,包含存储的名称、hp等信息 玩家和敌人(对手) 和一个连接到html按钮的攻击函数 目前,我可以在for循环中使用.html()和.append()将我的所有字符信息(对象数组)移植到html文档中,同时使用.attr()为它们提供一个(id=“champion[I].name”)及其hp、攻击
- 定义角色名称、hp和攻击强度的对象函数
- 一个对象数组,包含存储的名称、hp等信息
- 玩家和敌人(对手)
- 和一个连接到html按钮的攻击函数
如果有必要的话,我也有html代码。经过大量研究,我找到了这个问题的答案。您需要的是创建一个布尔型的if语句
var $champLocked = false;
$('.champ').on('click', function(event){ // when the button champ is clicked
if ($champLocked === false) { // check if champLocked is false. It is, because we said it is at the top. Now go to the next step.
var champSelection = $('<div>'); // create your div
var champInfo = $(this); // give it its data
var data = {
name: champInfo.attr('id'),
hp: champInfo.data('hp'),
attack: champInfo.data('attack')
}; // fun data .. End of function 1
现在,你的冠军被正式禁止重选。*“这是正确的方向吗?*”我感觉很糟糕,因为你在这个问题上做了很多工作,但这个问题是基于意见的,因此将导致接近票数。也许只需删除该部分,并明确提出3个要点问题?谢谢@freedomn-m。我不知道那会发生。我会编辑这篇文章使它更清楚。
$(".champ").on("click", function(event){ // adding the champ to the selected champ section
var champSelection = $("<div>"); // deciding that I will make the section a new Div
champSelection.append($(this).clone().addClass("clone").removeClass("hoverAnimation")); // "this" is equal to the champBtn, which contains the stored data of my champions
// .clone makes a new icon in the champion div
// .addClass lets me adjust the clone to look different from the other champion icons
// removeClass removes the hover animation from the clone
// without .clone() the image will delete its original position and move it to the champ selection space
$("#playerChamp").html(champSelection); // this ports the cloned image to the html
});
// Confirm Champ Pseudo Code: selecting the player and the opponent
// (var playerState = false) as default
// When the "confirm-selection Champion button" is clicked:
// (var player = champions[i]) and (var champState = true)
// -- I'm hoping that the data-sets carry over here and that you don't get every data-set of the champions[i]. Just the one you selected -- //
// when playerState is true:
// champions[i] in the champion select section becomes onclick: Null
// end player selection part.
// Champion selection then moves to: Opponent selection
// (var enemyState = false) as default
// when "confirm-selection Opponent button" is clicked:
// (var enemy = champions[i]) and (var enemyState = true)
// champions[i] in the champion select section becomes onclick: Null
// end Opponent selection part
var $champLocked = false;
$('.champ').on('click', function(event){ // when the button champ is clicked
if ($champLocked === false) { // check if champLocked is false. It is, because we said it is at the top. Now go to the next step.
var champSelection = $('<div>'); // create your div
var champInfo = $(this); // give it its data
var data = {
name: champInfo.attr('id'),
hp: champInfo.data('hp'),
attack: champInfo.data('attack')
}; // fun data .. End of function 1
$('#confirmChamp').on('click', function() { // new button to click
if(!champLocked) { // same as above, (if champ === false) just written shorter.
champLocked = true; // we then set it to true
$(this).prop('onclick',null).off('click'); // we set the onclick button of "this", which is, I believe #confirmChamp, to the property of an onlclick with a null value, and then attached an .off() method to the click.
});
} // end second functin