繁体   English   中英

Phaser:无法访问在其他范围中定义的变量

[英]Phaser: Cannot Access Variable Defined in Other Scope

我在Phaser中遇到了一个问题。 基本上,在主要校园功能中定义的玩家变量是不可访问的。 我首先尝试将其与其他变量一起定义,但是由于这种方法不起作用,因此我尝试在main函数中对其进行定义。 尝试按速度移动时,仅出现错误“未捕获的TypeError:无法设置未定义的属性'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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM