dongying3744 2016-08-13 18:38
浏览 36

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

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

});

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)