一次性调用具有不同功能的多个Ajax调用

时间:2016-08-13 18:38:56

标签: javascript php jquery ajax

我正在使用PHP / MySQL后端在HTML / Javascript / jQuery中构建一个非常简单的游戏。单击元素后,我无法连续执行两个不同的ajax调用。

我读到第二次调用需要包含在第一次调用的'success'或'beforeSend'或'done()'部分,但它似乎仍然没有工作。

以下是我目前使用的JS代码。该代码要求玩家被怪物攻击,然后玩家攻击怪物。如果有任何需要澄清的话,请告诉我。

Master.js     $(文件)。就绪(函数(){

// 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);
}

});

0 个答案:

没有答案