Main.js Определения, несовместимые с модулем - PullRequest
0 голосов
/ 03 мая 2018

Я использую инфраструктуру Phaser для создания игры. У меня есть два файла: main.js и Player.js, предназначенные для хранения класса Player.

main.js:

import 'pixi'
import Phaser from 'phaser'

import Player from './controllers/Player.js'

var game = new Phaser.Game(1280, 703, Phaser.AUTO, '', {
    preload: preload, 
    create: create, 
    update: update
});

var player;

function preload() {
    this.stage.backgroundColor = '#eee',
    this.scale.pageAlignHorizontally = true,
    this.scale.pageAlignVertically = true,
    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL,

    game.load.spritesheet( 'idle', '../../assets/sheet_hero_idle.png', 64, 64 ),
    game.load.spritesheet( 'wall', '../../assets/roguelike-cave-pack/Spritesheet/roguelikeDungeon_transparent.png', 16, 16 )
}

function create() {
    //Player Controller
    //===========================
    //Enable Physics on the world
    game.physics.startSystem( Phaser.Physics.ARCADE );
    game.physics.arcade.setBoundsToWorld();

    //Enable input
    game.inputEnabled = true;

    //create player
    player = new Player( game, game.world.centerX, game.world.centerY, 'idle' );
    game.add.existing( player ); //Create on screen
    game.physics.arcade.enable( player ); //Create physics body on this object

    console.log("player is\t", player); //TEST
    console.log("player body is\t", player.body); //TEST
    console.log("player body is enabled\t", player.body.enable); //TEST

    player.playAnim( 'idle', 10, true );

    game.input.onDown.add( player.thisIsMyBody, this ); //When a click input occurs, call player.thisIsMyBody()

Player.js:

export default function Player( game, x, y, animKey ) {
    Phaser.Sprite.call( this, game, x, y, animKey),
    this.anchor.setTo( 0.5 ),
    this.inputEnabled = true,
    this.assets = {
        animations: {}
    },
    this.animations.add( 'idle' ),
    this.animations.play( 'idle', 10, true ), 
    this.alive = true
};

Player.prototype = Object.create( Phaser.Sprite.prototype );

Player.prototype.constructor = Player;

Player.prototype.thisIsMyBody = function(){ //TEST
    console.log( "I am ", this );
    console.log( "My body is ", this.body);
},

Player.prototype.playAnim = function( key, speed, isLoop ) {
    this.animations.play( key, speed, isLoop );
},

Player.prototype.move = function() {
    this.body.velocity.x = 30;
}

console.log("player is\t", player); возвращает объект Player

console.log("player body is\t", player.body); возвращает тело игрока

console.log("player body is enabled\t", player.body.enable); возвращает true

Однако player.thisIsMyBody возвращает объект game и undefined

Есть ли что-то, что я пропускаю или неправильно использую this?

Ответы [ 2 ]

0 голосов
/ 08 мая 2018

После некоторой обработки я нашел следующее:

this.player = new Player( game, game.world.centerX, game.world.centerY, 'idle' );
game.add.existing( this.player ); //<--WILL NOT WORK WITHOUT
game.physics.arcade.enable( this.player );

game.camera.follow( this.player );
game.input.onDown.add( this.player.thisIsMyBody, this.player );
0 голосов
/ 07 мая 2018

Вы правы, вы неправильно используете this при привязке к input.onDown.

Не проверял, но попробуйте изменить

game.input.onDown.add(player.thisIsMyBody, this);

до

game.input.onDown.add(this.thisIsMyBody, player);

или, возможно, просто

game.input.onDown.add(thisIsMyBody, player);

При добавлении слушателя Phaser.Signal второй параметр в основном отвечает на вопрос «Что должно означать this при вызове обратного вызова?».

...