Sign in to follow this  
arun

Why use "this " keyword in Phaser states

Recommended Posts

As in any other case, this refers to the current object the code is running in, so using this inside phaser state just refers to this particular state object so to get a game object which that state belongs to you must use this.game to access it

Share this post


Link to post
Share on other sites

This is not strictly a Phaser thing. This gives context to whatever you assign it and depends on where you call it first. Read about it a bit more here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

You could use let for instance, but whatever variable you assign with let for instance in "create" cycle it will only be visible there and not in update or render cycle or wherever else.

If you use this just by it self it will represent the object in context of which it has been called.

It's hard to explain this in the first place so I'd suggest you read tutorials about it. Hope it helps.

Share this post


Link to post
Share on other sites

In this link they explain in detail the use of the "this" keyword in javascript:

https://codeburst.io/javascript-the-keyword-this-for-beginners-fb5238d99f85

In my humble opinion (I'm a newbie in javascript and Phaser), your question is more related to javascript than to Phaser framework. Using more time in javascript learning at the end will save you a lot of time when developing in Phaser, and will provide you with other resources that in some cases the framework will not be able to offer.

Regards.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.