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

时间:2017-03-05 12:52:04

标签: javascript phaser-framework

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

我确实删除了不相关的代码,但随时可以询问您是否需要更多代码(例如加载状态,我加载了播放器资产)。但不用多说,这就是代码。

index.html(head):

<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');
    }
};

1 个答案:

答案 0 :(得分:1)

在Player.js中我不知道何时使用参数名称,但我认为它是加载Preload的资源的键:

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

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

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
    }
}