I am building a pretty simple game in HTML / Javascript / jQuery with a PHP / MySQL back end. I am having trouble getting two different ajax calls to be performed in succession after the click of an element.
I read that the second call needs to be wrapped in the 'success' or 'beforeSend' or 'done()' part of the first call, but it still doesn't seem to be working.
Below is the JS code I am using at the moment. The code calls for the Player to be attacked by a monster, after which the Player attacks the monster. Please let me know if anything needs to be clarified.
Master.js $(document).ready(function(){
// Battling
$(".attack-icon").click(function() {
$m_name = "" + "<?php echo $cur_monster->name; ?>" + "";
$m_hp = $(".monster-hp").html();
$.ajax({
type: "POST",
url: "./ajax/attack_monster.php",
data: {
name : $m_name,
id : "12345",
hp : $m_hp
},
beforeSend : function() {
attackPlayer(50,false, false);
// Example:
// xhr.overrideMimeType( 'application/json' );
},
success: function(result) {
// Get Attack Info
$dead = result['dead'];
$dmg = result['dmg'];
$m_id = result['m_id'];
$name = result['name'];
$critical = result['critical'];
if ($critical) { $critical = "critical"; } else { $critical = ""; }
// Display Attack Animation
animateAttack($dmg);
// Set Monster HP
setNewMonsterHP($dmg);
// Show DMG Message
showMonsterDmgMessage($dmg, $dead, $critical);
// Attack Player Back
//attackPlayer(50, true, true);
},
dataType:"json",
context : this
});
});
function attackPlayer(dmg, dead, critical) {
/********* ATTACK PLAYER BACK *********/
$.ajax({
url: "./ajax/attack_player.php",
dataType:"json",
context : this
}).done(function(result) {
alert('hello world');
});
$dmg = dmg;
$dead2 = dead;
$critical2 = critical;
$player_total_hp = $(".player-total-hp").html();
$player_current_hp = $(".player-current-hp").html();
$player_new_hp = $player_current_hp - $dmg;
$player_hp_percentage = ($player_new_hp / $player_total_hp) * 100;
$(".stat-bar-subtitle").html(($player_current_hp - $dmg) + " HP");
$(".player-current-hp").html($player_new_hp);
$(".hp-bar-fill").css("width", $player_hp_percentage + "%");
showPlayerDmgMessage(dmg, dead, critical);
}
function showPlayerDmgMessage(dmg, dead, critical) {
// show loss message if player is dead
if (dead) {
$player_hp = ($(".player-current-hp").html("0"));
$(".console-block").append("<h5 class='console-text'>The monster defeats you with a final " + critical + " attack.</h5>");
$(".console-block").scrollTop($(".console-block")[0].scrollHeight);
setTimeout(function() { document.location.href = './travel.php'; }, 2000);
} else {
// show damage dealt message, if monster is not dead
if (critical) {
$(".console-block").append("<h5 class='console-text red-text'>Critical hit! The monster dealt " + dmg + " damage.</h5>");
} else {
$(".console-block").append("<h5 class='console-text'>The monster attacked you. It Dealt " + dmg + " damage.</h5>");
}
$(".console-block").scrollTop($(".console-block")[0].scrollHeight);
}
}
function showMonsterDmgMessage(dmg, dead, critical) {
// show win message if monster is dead
if ($dead) {
$monster_hp = ($(".monster-hp").html("0"));
$(".console-block").append("<h5 class='console-text'>You defeat the monster with a final " + $critical + " attack.</h5>");
$(".console-block").scrollTop($(".console-block")[0].scrollHeight);
setTimeout(function() { document.location.href = './travel.php'; }, 2000);
} else {
// show damage dealt message, if monster is not dead
if ($critical) {
$(".console-block").append("<h5 class='console-text red-text'>Critical hit! Dealt " + dmg + " damage.</h5>");
} else {
$(".console-block").append("<h5 class='console-text'>Attacked Monster. Dealt " + dmg + " damage.</h5>");
}
$(".console-block").scrollTop($(".console-block")[0].scrollHeight);
}
}
function setNewMonsterHP() {
// set new monster HP
$monster_hp = ($(".monster-hp").html() - $dmg);
$monster_hp = $(".monster-hp").html($monster_hp);
}
function animateAttack(dmg) {
// Display attack animation
//$(".monster-image").fadeOut(100).fadeIn(100);
$(".monster-dmg").html(dmg).show();
setTimeout(function() {
$(".monster-dmg").hide();
}, 500);
}
});