相位器:不能访问在其他作用域中定义的变量

Phaser: Cannot Access Variable Defined in Other Scope

本文关键字:作用域 定义 变量 其他 不能 访问      更新时间:2023-09-26

我在Phaser中遇到了一个问题。基本上,在主要校园功能中定义的玩家变量是不可访问的。我首先尝试将它与其他变量一起定义,但由于这不起作用,我尝试在主函数中定义它。当我尝试按速度移动时,我只得到错误"未捕获的类型错误:无法设置未定义的属性'x'"。我希望有人能帮忙,提前谢谢。

var Schoolyard = function() {
    this._player = null;
};
var map;
var backgroundLayer;
var backgroundLayer2;
var collisionLayer;
var cursors;
Schoolyard.prototype = {
    preload: function() {
    },
    create: function() {
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        map = this.game.add.tilemap('schoolyard');
        map.addTilesetImage('tiles');
        map.addTilesetImage('tiles2');
        backgroundLayer = map.createLayer('BackgroundLayer');
        brackgroundLayer2 = map.createLayer('BackgroundLayer2');
        collisionLayer = map.createLayer('CollisionLayer');
        this._player = this.game.add.sprite(400,400,'main');
        this.game.physics.enable(this._player);
        this.game.camera.follow(this._player);
        this._player.frame = 30;
        cursors = this.game.input.keyboard.createCursorKeys();
    },
    update: function() {
        if (cursors.right.isDown)
            this._player.velocity.x = 150;
    }
};

好的,下面是答案:

速度是物理物体的一种属性。

你应该使用:

this._player.body.velocity.x = 150