簡體   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