1

这是我的代码。我希望我的球员有一些统计数据,但我不确定我应该做什么。

我对编码很陌生,我认为我应该向播放器添加“一些东西”(比如 player.life),它似乎有效。但是当我的玩家击中第二个蘑菇(代码末尾的 CFR 战斗)时,它不会在控制台中记录预期的“玩家生命为 470”。

我认为问题可能来自lifeHitted变量。

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
    preload: preload,
    create: create,
    update: update
});
var player;

function preload() {
    game.load.image('square', 'assets/poulpe.png');
    game.load.image('platform', 'assets/platform.png');
    game.load.image('background', 'assets/background.png');
    game.load.image('mushroom', 'assets/mushroom.png');


}

var platforms;

function create() {

    game.add.sprite(0, 0, 'background');
    var text = "Le jeu de Mr Carré.";
    var style = {
        font: "45px Arial",
        fill: "#dddddd",
        align: "center"
    };
    game.add.text(game.world.centerX - 300, 0, text, style);

    //platforms
    platforms = game.add.group();

    ground = platforms.create(0, game.world.height - 90, 'platform');
    ground.scale.setTo(2, 2);
    ground.body.immovable = true;

    //Ledge
    var ledge = platforms.create(145, 350, 'platform');
    ledge.scale.setTo(1, 0.5);
    ledge.body.immovable = true;

    ledge = platforms.create(350, 250, 'platform');
    ledge.scale.setTo(1, 0.5);
    ledge.body.immovable = true;


    //player
    player = game.add.sprite(20, game.world.height - 180, 'square');
    player.scale.setTo(1, 1);
    player.body.bounce.y = 0.2;
    player.body.gravity.y = 6;
    player.body.collideWorldBounds = true;
    player.life = 500;
    currentLife = player.life;

    player.atk = 30;

    //mushroom
    mushroom = game.add.group();
    //mushroom.x= Math.random()*100; 

    mushroom.life = 100;
    mushroom.atk = 15;
    dmg = mushroom.atk;

    mush1 = mushroom.create(200, 250, 'mushroom');
    mush1.body.immovable = true;


    mush2 = mushroom.create(500, 150, 'mushroom');
    mush2.body.immovable = true;

    jumpCount = 0;





}

function update() {

    game.physics.collide(player, platforms);
    game.physics.collide(mushroom, platforms);
    game.physics.overlap(player, mush1, scaleUpPlayer, null, this);
    game.physics.overlap(player, mush2, scaleDownPlayer, null, this);

    function scaleUpPlayer(player, mush) {
        mush.kill();
        player.scale.setTo(2, 2);
        player.x = 210;
        player.y = 200;
    }

    function scaleDownPlayer(player, mush) {
        mush.kill();
        player.scale.setTo(1, 1);
        player.x = 500;
        player.y = 150;
    }


    //jump
    console.log('rrr');
    var jumpKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
    // A chaque fois que UP est pressé, on lance la fonction jumpCheck()
    jumpKey.onDown.add(jumpCheck);

    function jumpCheck() {
        if ((jumpCount < 1) && (player.body.touching.down)) {
            jump1();
            console.log("jumpCount =" + jumpCount);
            console.log("Vitesse =" + player.body.velocity.y);
            //attention, remettre jumpCount à zéro si on touche le sol
//            if(player.body.touching.down) {
//                jumpCount = 0;
//            }
        }

        //double jump
        if ((jumpCount == 1) && (!player.body.touching.down)) {
            jump2();
            console.log("jumpCount =" + jumpCount);
            console.log("Vitesse =" + player.body.velocity.y);

        }

        //debug
        else if (jumpCount >= 2  ) {
            console.log("jumpCount =" + jumpCount);
            console.log("Vitesse =" + player.body.velocity.y);
        }
    }

    function jump1() {
        console.log("jump1");
        jumpCount++;
        player.body.velocity.y = -320;
    }

    function jump2() {
        console.log("jump2");
        jumpCount = 0;
        player.body.velocity.y = -150;

    }

    //movement
    player.body.velocity.x = 0;

    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
        player.body.velocity.x = -350;
    } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
        player.body.velocity.x = 350;
    }

    //Combat
    game.physics.overlap(player, mushroom, testDmg, null, this);

    function testDmg(){
        if(game.physics.overlap(player, mushroom)){
            lifeHitted = player.life - dmg;

            if(lifeHitted <= 0){
            console.log("Le player meurt.");
            player.kill();
            //die();
            }

            if(lifeHitted > 0){
                console.log("player life is:" + lifeHitted);

            }  
        }
    }    
}
4

1 回答 1

0

Phaser的overlap函数由所使用的物理引擎类型定义:

game.physics.arcade.overlap(player, mushroom, testDmg, null, this);

重叠处理函数需要重叠的两个对象作为参数。testDmg应该这样定义:

function testDmg (player, mushroom) {
    // Subtract the mushroom's attack from the player's life
    player.life = player.life - mushroom.atk;

    if(player.life <= 0){
        console.log("Le player meurt.");
        player.kill();
        //die();
    }

    if(player.life > 0){
        console.log("player life is:" + player.life);
    }  
}
于 2014-07-12T14:40:48.303 回答