简体   繁体   English

Phaser Javascript 游戏、此关键字和游戏状态

[英]Phaser Javascript game, this keyword and game states

I have just started using Phaser to create Javascript games and have questions regarding the 'this' keyword.我刚刚开始使用 Phaser 来创建 Javascript 游戏,并且对“this”关键字有疑问。

  • Does this refer to the current state (rainState)?是指当前状态(rainState)吗?

  • I assume this.emitter = this.game.add.emitter(100, 0, 400);我假设this.emitter = this.game.add.emitter(100, 0, 400); creates an emitter property on the rainState object that stores an emitter object ?在rainState 对象上创建一个发射器属性来存储发射器对象?

  • Is game a property of state?游戏是国家的财产吗? why do write this.game.add.emitter as opposed to this.add emitter为什么要写this.game.add.emitter而不是this.add 发射器

//------------------------------------------------- //------------------------------------------------ ——

     var Smash = Smash || {};

    Smash.rainState = {
        create: function() {
            this.emitter = this.game.add.emitter(this.game.world.centerX, 0, 400);

            this.emitter.width = this.game.world.width;
            this.emitter.angle = 30;
            this.emitter.makeParticles('particle');

            this.player = new Smash.Player(this.game, 100, 300, 'player');

            this.intro0();       
        },

        intro0: function() {
          this.player.animations.play("run", 9, true);
        },
    };

Does this refer to the current state (rainState)?这是指当前状态(rainState)吗?

The answer is it depends how it is called.答案是这取决于它是如何调用的。

If it's called as a method of the rainState object, the context ( this ) will be set to the rainState object:如果它作为rainState对象的方法被rainState ,则上下文( this )将设置为rainState对象:

Smash.rainState.create();

Using call , apply , or bind you can pass in whatever scope you want as an argument:使用callapplybind你可以传入任何你想要的作用域作为参数:

Smash.rainState.create.call(foo); //immediately invoked
Smash.rainState.create.apply(foo); //immediately invoked
var bar = Smash.rainState.create.bind(foo); //reference to a newly created bound function

I assume this.emitter = this.game.add.emitter(100, 0, 400);我假设 this.emitter = this.game.add.emitter(100, 0, 400); creates an emitter property on the rainState objects that stores an emitter object ?在rainState 对象上创建一个发射器属性来存储发射器对象?

Yes when you assign a value to this.emitter it is added as a property of the object rainState .是的,当您为this.emitter它被添加为对象rainState的属性。

Smash.rainState = {
    create: function() {
        console.log(Smash.rainState.emitter); //undefined
        this.emitter = this.game.add.emitter(this.game.world.centerX, 0, 400);
        console.log(Smash.rainState.emitter); //[object Object]
    },      
};

Is game a property of state?游戏是国家的财产吗? why do write this.game.add.emitter as opposed to this.add emitter为什么要写 this.game.add.emitter 而不是 this.add 发射器

Based on the code you've provided, it doesn't look like game is a method or property of rainState .根据您提供的代码,看起来game不是rainState的方法或属性。 Furthermore, this.add is not a built-in function of the object type.此外, this.add不是object类型的内置函数。 Unless game has been added as a property of rainState somewhere else, you'll need to refer to it in another way.除非在rainState地方将game添加为rainState的属性,否则您需要以另一种方式引用它。

Globally:全球:

var game = new Phaser.Game();

var Smash = Smash || {};

Smash.rainState = {
    create: function() {
         this.emitter = game.add.emitter(this.game.world.centerX, 0, 400);

As a parameter passed in from a place where game exists in context:作为从上下文game存在game的地方传入的参数:

Smash.rainState = {
    create: function(game) {
         this.emitter = game.add.emitter(this.game.world.centerX, 0, 400);
    }
}

//a context where game is a property
{
    this.game = new Phaser.Game();   

    Smash.rainState.create(this.game);
}

Phaser has some specific settings around how you refer to the game instantiation of the Phaser.Game class in different contexts: Phaser 有一些关于如何在不同上下文中Phaser.Game类的game实例的特定设置:

"Via": If a class has an entry in the via column it means you can quickly access it through a local reference. “Via”:如果一个类在 via 列中有一个条目,这意味着您可以通过本地引用快速访问它。 Ie you can control the camera via this.camera from any state, or game.camera if game has been globally defined.也就是说,您可以通过 this.camera 从任何状态或 game.camera(如果游戏已全局定义)控制相机。

This means if you initialized your game instance in the global scope (outside of any {} ) then you should be able to call it globally.这意味着如果您在全局范围内(在任何{} )初始化了您的game实例,那么您应该能够全局调用它。 Look for something like this in your code and make sure it's not inside another function or object:在您的代码中查找类似的内容,并确保它不在另一个函数或对象中:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });

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

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