为什么不将我重定向到新页面?

lmgguy

所以这是一个飞扬的小鸟游戏。用户点击得分5时,我一直试图将用户移动到新的网址。游戏运行正常,但我没有被重定向到页面,并且游戏继续运行。

脚本 :

$(function () {

    //saving dom objects to variables
    var container = $('#container');

var bird = $('#bird');

var pole = $('.pole');

var pole_1 = $('#pole_1');
var pole_2 = $('#pole_2');

var score = $('#score');

var speed_span = $('#speed');

var restart_btn = $('#restart_btn');


//saving some initial setup
    var container_width = parseInt(container.width());

var container_height = parseInt(container.height());

var pole_initial_position = parseInt(pole.css('right'));

var pole_initial_height = parseInt(pole.css('height'));

var bird_left = parseInt(bird.css('left'));

var bird_height = parseInt(bird.height());

var speed = 10;

    //some other declarations

var go_up = false;

var score_updated = false;

var game_over = false;



var the_game = setInterval(function () {


        if (collision(bird, pole_1) || collision(bird, pole_2) || parseInt(bird.css('top')) <= 0 || parseInt(bird.css('top')) > container_height - bird_height) 
    {


            stop_the_game();


        } else {


            var pole_current_position = parseInt(pole.css('right'));



//update the score when the poles have passed the bird successfully

if (pole_current_position > container_width - bird_left) {

    if (score_updated === false) {

        score.text(parseInt(score.text()) + 1);

        score_updated = true;

                if(score.text(parseInt(score.text()))==5){
            window.location.href = "http://fb.com";

        }
    }

}


    //check whether the poles went out of the container

    if (pole_current_position > container_width) {

        var new_height = parseInt(Math.random() * 100);


        //change the pole's height

        pole_1.css('height', pole_initial_height + new_height);

        pole_2.css('height', pole_initial_height - new_height);


        //increase speed


        speed = speed + 1;

        speed_span.text(speed);


        score_updated = false;


        pole_current_position = pole_initial_position;

        if(score.text(parseInt(score.text()))==5){
            window.location.href = "http://fb.com";

        }          
    }


    //move the poles

    pole.css('right', pole_current_position + speed);


    if (go_up === false){

                go_down();

            }

        }


    },
 40);


$(document).on('keydown', function (e) {
        var key = e.keyCode;
        if (key === 32 && go_up === false && game_over === false) {
            go_up = setInterval(up, 50);
        }
    });

    $(document).on('keyup', function (e) {
        var key = e.keyCode;
        if (key === 32) {
            clearInterval(go_up);
            go_up = false;
        }
    });


    function go_down() {
        bird.css('top', parseInt(bird.css('top')) + 5);
    }

    function up() {
        bird.css('top', parseInt(bird.css('top')) - 10);
    }

    function stop_the_game() {
        clearInterval(the_game);
        game_over = true;
        restart_btn.slideDown();
    }

    restart_btn.click(function () {
        location.reload();
    });

    function collision($div1, $div2) {
        var x1 = $div1.offset().left;
        var y1 = $div1.offset().top;
        var h1 = $div1.outerHeight(true);
        var w1 = $div1.outerWidth(true);
        var b1 = y1 + h1;
        var r1 = x1 + w1;
        var x2 = $div2.offset().left;
        var y2 = $div2.offset().top;

var h2 = $div2.outerHeight(true);

var w2 = $div2.outerWidth(true);

var b2 = y2 + h2;

var r2 = x2 + w2;


if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;

return true;

}



});

HTML:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />

 <title>FlattyBird</title>

 <link href="style.css" rel="stylesheet" />

</head>


<body>
<!--     Testing the git command line tool-->

<div id="container">


<div id="bird"></div>


<div id="pole_1" class="pole"></div>

<div id="pole_2" class="pole"></div>


</div>


<div id="score_div">

<p><b>Score: </b><span id="score">0</span></p>

<p><b>Speed: </b><span id="speed">10</span></p>

</div>


<button id="restart_btn">Restart</button>


<script src="jquery.min.js"></script>

<script src="script.js"></script>


</body>


</html>
穆纳威尔

尝试

if(score.text(parseInt(score.text()))=="5"){
   window.location.href = "http://fb.com";
}

或者

if(parseInt(score.text())==5){
   window.location.href = "http://fb.com";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的ASCX控件上的Javascript无法将我重定向到新页面/控件

为什么onchange事件将我带到新页面?

@ nuxtjs / auth为什么刷新页面总是重定向到登录

Django提交表单->重定向到新页面->自动更新新页面

发布操作后重定向到新页面

显示加载图标,然后重定向到新页面

使用 ValidationError 而不是重定向到新页面

按钮值重定向到新页面

如何重定向到FreeMarker模板中的新页面?

如何使用javascript重定向到新页面

javascript中的onclick重定向到新页面

表单未重定向到新页面

重定向到新页面时存在状态

通过对话框重定向到新页面

如何从servlet重定向到新页面

如何连续重定向到 rails 中的新页面?

Javascript重定向并注入新页面

为什么我无法使用 JQuery 和 PHP 将我的数据提交到我的数据库,以及为什么我的页面不断重定向到主页?

点击图片并重定向到新页面,将点击图片动态添加到新页面?

为什么我的表单提交仍然刷新页面?

重定向到表单提交页面并在新页面中使用表单数据

如何检测刷新页面并重定向到另一个页面

htaccess,从旧页面重定向到新页面(php和post变量)

在GWT中打开新页面或重定向页面的最佳实践是什么?

当我不将stderr重定向到stdout时,为什么grep返回不匹配的行?

Spring Security不断将我重定向到登录页面

Angularjs 路由将我重定向到默认页面

将我的jsp表单重定向到成功页面

为什么Nginx一直将我重定向到localhost?