Freckles

Accessing variables in PIXI*

Recommended Posts

 this.cursor = new PIXI.Graphics(); 
 /****some code ****/
 this.addChild(this.cursor);

 setInterval(function () {
    this.cursor.alpha = 1;  //alpha is undefined
    setTimeout(function () {
       this.cursor.alpha = 0;
    }, 500);
 }, 1000);


/*************************************************************************************/

 let a = this.cursor = new PIXI.Graphics();
 /****some code ****/
 this.addChild(this.cursor);

 setInterval(function () {
    a.alpha = 1;
    setTimeout(function () {
       a.alpha = 0;
    }, 500);
 }, 1000);

Hey guys, could you please explain why I get ''alpha is undefined'' in the first case and it works alright in the second case? TBH I don't know if this question is pixi related or js related.

 

Also


            this.realInput.setAttribute(
                `style`, `position: relative; opacity: 0`,
                `type`, `password`,
            );
            
            this.realInput.setAttribute(
                `inputmode`, `${this.inputMode}`
            )

////////////////////////////////////////////////////////////////////////////////

            this.realInput.setAttribute(
                `style`, `position: relative; opacity: 0`,
                `type`, `password`,
                `inputmode`, `${this.inputMode}`
            );
            
           

Why is the first case working and the second one doesn't, isn't that the same somehow?

Share this post


Link to post
Share on other sites
 setInterval(() => {
    this.cursor.alpha = 1;  //alpha is undefined
    setTimeout( ()=> {
       this.cursor.alpha = 0;
    }, 500);
 }, 1000);

You could use arrow functions instead. The scope to which this refers in javascript changes in your first example to functions scope.

For more info on how this gets scoped check out this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Edited by Exca
accidentally removed the first part

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...

  • Recently Browsing   0 members

    No registered users viewing this page.