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 に答える