0

我正在使用 Phaser js 框架构建游戏。它是一个无尽的跑步者,我刚刚开始。这是代码:

<!doctype html>

<html>
<head>
    <title>Game2</title>
    <script type="text/javascript" src="js/phaser.min.js"></script>
</head>

<body>
    <script type="text/javascript">
        window.onload=function()
        {
            var game=new Phaser.Game(800,600,Phaser.AUTO,'',{preload:preload,create:create,update:update});
            var player;
            var platforms;
            var count=0;
            var t;
            var style;

        function preload()
            {
                game.load.image('ground','assets/platform.png');
                game.load.image('sky','assets/sky.png');
                game.load.spritesheet('hero','assets/hero.png',128,128,16);
            }
        function create()
            {
                game.world.setBounds(0,0,800,600);

                //game.add.sprite(0,0,'sky');

                platforms=game.add.group();

                var ground=platforms.create(0,304,'ground');

                ground.scale.setTo(2,2);

                ground.body.immovable=true;

                //var ledge = platforms.create(400, 400, 'ground');

                //ledge.body.immovable = true;

                //ledge = platforms.create(-150, 250, 'ground');

                //ledge.body.immovable = true;

                player=game.add.sprite(390,32,'hero');

                player.body.bounce.y=0.1;
                player.body.gravity.y=10;
                player.body.collideWorldBounds=true;

                player.animations.add('left',[0,1,2,3],10,true);
                player.animations.add('right',[4,5,6,7],10,true);
                player.animations.add('stop',[9],10,true);

                game.camera.follow(player);    

                //var count="0";
                style = { font: "65px Arial", fill: "#ff0044", align: "center" };
                t = game.add.text(game.world.centerX-350, 0, count, style);
                //game.time.events.loop(Phaser.Timer.SECOND,distance,this);
            }

            function update()
            {
                distance();
                game.physics.collide(player,platforms);
                player.body.velocity.x=0;



                /*if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
                {
                    player.body.velocity.x=-250;
                    console.log("Left");
                    player.animations.play('left');
                }

                else if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
                {
                    player.body.velocity.x=250;
                    console.log("Right");
                    player.animations.play('right');
                }*/


                player.animations.play('right',10,true); //this was done since in an infinite runner the character always runs never stops, the                                                                //only key needed to be pressed is the up key to jump when the platform ends.

                if (game.input.keyboard.isDown(Phaser.Keyboard.UP) && player.body.touching.down)
                {
                    player.body.velocity.y = -350;
                }


            }

             function distance()
            {
                count++;            //this value will hold the score of every player. I need to pass it to the leader board using php. How?
                t.content=count;
            }

        };
    </script>
</body>

我希望使用 php 将 distance() 函数中的计数变量的值传递给排行榜。我怎样才能做到这一点?

4

1 回答 1

4

假设你有一个 PHP 文件来保存分数,你可以用 AJAX 来做:

function submitScore(username, count) {

    var data="username="+username+"&count="+count;

    var request = new XMLHttpRequest();
    request.open('POST', '/setScore.php', true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    request.onload = function() {
      if (request.status >= 200 && request.status < 400){
        // Success!
        // here you could go to the leaderboard or restart your game .
      } else {
        // We reached our target server, but it returned an error

      }
    };  
    request.send(data);
}
于 2014-03-19T21:23:09.017 回答