简体   繁体   中英

Phaser: Cannot Access Variable Defined in Other Scope

I've ran into a problem in Phaser. Basically, the player variable defined in the main schoolyard function isn't accesible. I first tried to define it together with the other variables, but since that didn't work I tried defining it in the main function. I only get the error "Uncaught TypeError: Cannot set property 'x' of undefined" when I try to move by velocity. I hope someone can help, thanks in advance.

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

Ok, here as an answer:

Velocity is a property of a physics body.

You should use:

this._player.body.velocity.x = 150

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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