简体   繁体   English

Main.js定义与模块不一致

[英]Main.js Definitions Inconsistent from Module

I am using the Phaser framework to create a game. 我正在使用Phaser框架来创建游戏。 I have two files, a main.js and a Player.js that is meant to hold the Player class. 我有两个文件,一个main.js和一个Player.js ,用于保存Player类。

main.js: 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: 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); returns Player object 返回Player对象

console.log("player body is\\t", player.body); returns Player body 返回玩家身体

console.log("player body is enabled\\t", player.body.enable); returns true 返回true

However, player.thisIsMyBody returns the game Object and undefined 但是, player.thisIsMyBody返回game对象,并且undefined

Is there anything I am missing or some misuse of this ? 有什么我丢失或误用一些this

You're correct, you're misusing the this when binding to input.onDown . input.onDown在绑定到input.onDown时,您滥用了this

Haven't tested it, but try changing 尚未测试,但尝试更改

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

to

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

or possibly just 或者只是

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

When adding a Phaser.Signal listener, the second parameter basically answers the question "What should this mean when calling the callback?". 添加Phaser.Signal侦听器时,第二个参数基本上回答问题“调用回调时this意味着什么?”。

After some tinkering, I found the following to work: 经过一番修补后,我发现以下工作有效:

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

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

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