【问题标题】:Phaser.js - passing game variable to custom object's update functionPhaser.js - 将游戏变量传递给自定义对象的更新函数
【发布时间】:2017-03-05 12:52:04
【问题描述】:

所以我遇到的问题是我正在尝试创建一个 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');
    }
};

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

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

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

    我了解 Update 不接收参数,从 Update 函数中引用“游戏”对象将在 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2014-08-18
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 2017-06-10
      • 2013-03-08
      相关资源
      最近更新 更多