我主要使用jquery制作了一个onclick rpg。我在随机选择被敌方机器人攻击的英雄时遇到了一个主要问题
I made an onclick rpg mainly using jquery. I'm having a major issue with the random selection of a hero being attacked by the enemy bots
这是我的第一个堆栈溢出问题,但我会尽量简洁。我制作了一个老式角色扮演游戏风格的游戏。基于漫威宇宙,您可以选择三位英雄,并从三个敌人中选择一个进行攻击。发起攻击后,敌方会发起随机攻击。我的主要问题是,如果其中一个英雄角色死亡并从游戏中移除,敌人仍然有可能随机攻击该角色。以下是所述游戏的一些来源:http://marvel-jquery-rpg.herokuapp.com/ or my github: https://github.com/jdestiny92/jquery_rpg 或者这里是有问题的主要 js 函数:
// Boss Attack Sequence
function bossAttackSequence(){
heroes = ["cap", "ironman", "deadpool"];
choice = heroes[Math.floor(Math.random()*3)];
ultronAttack = Math.floor(Math.random()*(300-200+1)+200);
ultronCrit = Math.floor(Math.random()*100);
if(ultronCrit <= 15){
ultronAttack = Math.floor(ultronAttack + .25*ultronAttack);
};
if(choice=="cap"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Captain America for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Captain America for ' + ultronAttack + ' damage');
};
reset();
capHealth = capHealth - ultronAttack;
$('#capbox').html('Health: ' + capHealth);
if(capHealth < 0){
$('#cap').remove();
$('#capbox').remove();
$('#capHealth').remove();
new Audio('letdown.mp3').play();
};
if(window.capHealth <=0 && window.ironmanHealth <=0 && window.deadpoolHealth <=0){location.replace('loss.html');};
});
};
if(choice=="ironman"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Ironman for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Ironman for ' + ultronAttack + ' damage');
};
reset();
ironmanHealth = ironmanHealth - ultronAttack;
$('#ironmanbox').html('Health: ' + ironmanHealth);
if(ironmanHealth < 0){
$('#ironman').remove();
$('#ironmanbox').remove();
$('#ironmanHealth').remove();
new Audio('impossible2.mp3').play();
};
});
};
if(choice=="deadpool"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Deadpool for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Deadpool for ' + ultronAttack + ' damage');
};
reset();
deadpoolHealth = deadpoolHealth - ultronAttack;
$('#deadpoolbox').html('Health: ' + deadpoolHealth);
if(deadpoolHealth < 0){
$('#deadpool').remove();
$('#deadpoolbox').remove();
$('#deadpoolHealth').remove();
new Audio('wrongButton.mp3').play();
};
});
};
};
这是我修复敌人攻击选择的尝试,但是一旦我将它插入到之前的函数中它就不起作用了:
if(capHeatlh<0 && ironmanHeatlh<0){
heroes = ['deadpool', 'deadpool', 'deadpool'];
};
if(capHeatlh<0 && deadpoolHeatlh<0){
heroes = ['ironman', 'ironman', 'ironman'];
};
if(deadpoolHeatlh<0 && ironmanHeatlh<0){
heroes = ['cap', 'cap', 'cap'];
};
if(capHeatlh<0){
var option1 = ['ironman', 'ironman', 'deadpool'];
var option2 = ['deadpool', 'ironman', 'deadpool'];
var coinflip1 = Math.floor(Math.random()*2);
if(coinflip1==0){
heroes = option1;
}
else{
heroes = option2;
};
};
if(ironmanHeatlh<0){
var option1 = ['cap', 'cap', 'deadpool'];
var option2 = ['cap', 'deadpool', 'deadpool'];
var coinflip2 = Math.floor(Math.random()*2);
if(coinflip2==0){
heroes = option1;
}
else{
heroes = option2;
};
};
if(deadpoolHeatlh<0){
var option1 = ['cap', 'ironman', 'cap'];
var option2 = ['cap', 'ironman', 'ironman'];
var coinflip3 = Math.floor(Math.random()*2);
if(coinflip3==0){
heroes = option1;
}
else{
heroes = option2;
};
};
所有 help/feedback 将不胜感激!谢谢!
您可以将 heroes 数组赋值移出函数的范围。然后由你的英雄数组中剩下的人控制你的选择:
heroes = ["cap", "ironman", "deadpool"]; //move this outside of the function scope, then
你选择的随机索引应该由英雄数组的长度决定:
您可以使用 .length 代替硬编码 3:
choice = heroes[Math.floor(Math.random()*heroes.length)];
当英雄死亡时,您可以使用 .splice(index, 1);
将他从数组中删除
所以 heroes.splice(0, 1) 将删除 "cap", heroes.splice(1,1) 将删除 "ironman", heroes.splice(2, 1) 将删除 "deadpool"
这样下次你 运行 bossSequence 时,你的英雄数组可能只剩下 2 个英雄,或者只剩下 1 个英雄,但你的随机选择仍然只是活着的角色之一。
这是我的第一个堆栈溢出问题,但我会尽量简洁。我制作了一个老式角色扮演游戏风格的游戏。基于漫威宇宙,您可以选择三位英雄,并从三个敌人中选择一个进行攻击。发起攻击后,敌方会发起随机攻击。我的主要问题是,如果其中一个英雄角色死亡并从游戏中移除,敌人仍然有可能随机攻击该角色。以下是所述游戏的一些来源:http://marvel-jquery-rpg.herokuapp.com/ or my github: https://github.com/jdestiny92/jquery_rpg 或者这里是有问题的主要 js 函数:
// Boss Attack Sequence
function bossAttackSequence(){
heroes = ["cap", "ironman", "deadpool"];
choice = heroes[Math.floor(Math.random()*3)];
ultronAttack = Math.floor(Math.random()*(300-200+1)+200);
ultronCrit = Math.floor(Math.random()*100);
if(ultronCrit <= 15){
ultronAttack = Math.floor(ultronAttack + .25*ultronAttack);
};
if(choice=="cap"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Captain America for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Captain America for ' + ultronAttack + ' damage');
};
reset();
capHealth = capHealth - ultronAttack;
$('#capbox').html('Health: ' + capHealth);
if(capHealth < 0){
$('#cap').remove();
$('#capbox').remove();
$('#capHealth').remove();
new Audio('letdown.mp3').play();
};
if(window.capHealth <=0 && window.ironmanHealth <=0 && window.deadpoolHealth <=0){location.replace('loss.html');};
});
};
if(choice=="ironman"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Ironman for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Ironman for ' + ultronAttack + ' damage');
};
reset();
ironmanHealth = ironmanHealth - ultronAttack;
$('#ironmanbox').html('Health: ' + ironmanHealth);
if(ironmanHealth < 0){
$('#ironman').remove();
$('#ironmanbox').remove();
$('#ironmanHealth').remove();
new Audio('impossible2.mp3').play();
};
});
};
if(choice=="deadpool"){
$('#attackMessage').show(1000, function(){
document.getElementById('ultronAttack').play();
if(ultronCrit <= 15){
document.getElementById('ultronAttack').pause();
document.getElementById('ultronCrit').play();
$('#attackMessage').html('Ultron did a critical hit on Deadpool for ' + ultronAttack + ' damage');
}
else{
$('#attackMessage').html('Ultron attacked Deadpool for ' + ultronAttack + ' damage');
};
reset();
deadpoolHealth = deadpoolHealth - ultronAttack;
$('#deadpoolbox').html('Health: ' + deadpoolHealth);
if(deadpoolHealth < 0){
$('#deadpool').remove();
$('#deadpoolbox').remove();
$('#deadpoolHealth').remove();
new Audio('wrongButton.mp3').play();
};
});
};
};
这是我修复敌人攻击选择的尝试,但是一旦我将它插入到之前的函数中它就不起作用了:
if(capHeatlh<0 && ironmanHeatlh<0){
heroes = ['deadpool', 'deadpool', 'deadpool'];
};
if(capHeatlh<0 && deadpoolHeatlh<0){
heroes = ['ironman', 'ironman', 'ironman'];
};
if(deadpoolHeatlh<0 && ironmanHeatlh<0){
heroes = ['cap', 'cap', 'cap'];
};
if(capHeatlh<0){
var option1 = ['ironman', 'ironman', 'deadpool'];
var option2 = ['deadpool', 'ironman', 'deadpool'];
var coinflip1 = Math.floor(Math.random()*2);
if(coinflip1==0){
heroes = option1;
}
else{
heroes = option2;
};
};
if(ironmanHeatlh<0){
var option1 = ['cap', 'cap', 'deadpool'];
var option2 = ['cap', 'deadpool', 'deadpool'];
var coinflip2 = Math.floor(Math.random()*2);
if(coinflip2==0){
heroes = option1;
}
else{
heroes = option2;
};
};
if(deadpoolHeatlh<0){
var option1 = ['cap', 'ironman', 'cap'];
var option2 = ['cap', 'ironman', 'ironman'];
var coinflip3 = Math.floor(Math.random()*2);
if(coinflip3==0){
heroes = option1;
}
else{
heroes = option2;
};
};
所有 help/feedback 将不胜感激!谢谢!
您可以将 heroes 数组赋值移出函数的范围。然后由你的英雄数组中剩下的人控制你的选择:
heroes = ["cap", "ironman", "deadpool"]; //move this outside of the function scope, then
你选择的随机索引应该由英雄数组的长度决定:
您可以使用 .length 代替硬编码 3:
choice = heroes[Math.floor(Math.random()*heroes.length)];
当英雄死亡时,您可以使用 .splice(index, 1);
将他从数组中删除所以 heroes.splice(0, 1) 将删除 "cap", heroes.splice(1,1) 将删除 "ironman", heroes.splice(2, 1) 将删除 "deadpool"
这样下次你 运行 bossSequence 时,你的英雄数组可能只剩下 2 个英雄,或者只剩下 1 个英雄,但你的随机选择仍然只是活着的角色之一。