Phaser.js - 将游戏变量传递给自定义对象的更新函数

Phaser.js - passing game variable to custom object's update function

所以我遇到的问题是我正在尝试创建一个 Player 对象并将游戏变量传递给它,除非我尝试访问游戏变量(特别是 game.time),否则它工作正常在播放器的更新功能中。我使用的是 Phaser 版本 6.2.6,我使用这个例子来创建我的自定义播放器对象:https://phaser.io/examples/v2/sprites/extending-sprite-demo-2

我确实删除了不相关的代码,但请随时询问您是否需要更多(例如加载状态,我加载播放器资产的位置)。事不宜迟,这是代码。

index.html(头):

<script type="text/javascript" src="asset/js/phaser.min.js"></script>
<script type="text/javascript" src="asset/js/Player.js"></script>
<script type="text/javascript" src="asset/js/Level1.js"></script>

index.html(正文):

<script type="text/javascript">

    window.onload = function() {
        var game = new Phaser.Game(800, 600, Phaser.CANVAS, '');

        game.state.add('Level1', Game.Level1);

        game.state.start('Level1');
    };
</script>

Level1.js:

var Game = {};  // This line is actually only in my Boot.js file but it for this question I put it here

Game.Level1 = function(game) {};

Game.Level1.prototype = {
    preload : function() {

    },

    create : function(game) {

        game.stage.backgroundColor = '#42f4d9';

        this.physics.arcade.gravity.y = 1400;

        game.player = new Player(game, 'henri', 100, 100);
    },

    update : function(game) {
        console.log(game);  // here the game object gets printed like it should
    },
};

Player.js:

Player = function (game, name, x, y) {

    // accessing 'game' in here works fine.

    Phaser.Sprite.call(this, game, x, y, 'player');
    this.anchor.setTo(0.5, 0.5);

    this.speed = 100;
    this.jumpVelocity = -600;
    this.jumpTimer = 0;

    this.animations.add('idle', [0], 1, false);
    this.animations.add('jump', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 20, true);

    game.physics.arcade.enable(this);
    this.body.collideWorldBounds = true;

    this.controls = {
        jump: game.input.keyboard.addKey(Phaser.Keyboard.W)
    };

    game.add.existing(this);
};

Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;

Player.prototype.update = function(game) {

    console.log(game) // here it prints 'undefined'

    this.body.velocity.x = 0;

    // this is where I actually get the problem, trying to do game.time.now throws the error "Uncaught TypeError: Cannot read property 'time' of undefined"
    if(this.controls.jump.isDown && (this.body.onFloor() || this.body.touching.down) && game.time.now > this.jumpTimer) {
        this.body.velocity.y = this.jumpVelocity;
        this.jumpTimer = this.time.now +750;
        this.animations.play('jump');
    }

    if(this.body.velocity.x == 0 && this.body.velocity.y == 0) {
        this.animations.play('idle');
    }
};

在Player.js中不知道什么时候用参数名,我假设是用Preload加载的资源的key:

Phaser.Sprite.call(this, game, x, y, name);

我知道 Update 不接收参数,要从 Update 函数中引用 'game' 对象将使用 'this' in Player.js:

Player.prototype.update = function() {

    console.log(this.game);

    this.body.velocity.x = 0;

   if (this.controls.jump.isDown && (this.body.onFloor() || this.body.touching.down) && this.game.time.now > this.jumpTimer) {
        //Code
    }
}