Jump to content

Is there a way to access a passed variable in a class?


Ninjadoodle
 Share

Recommended Posts

Hi @enpu

Is there a way I can access the 'moving' variable I pass to a class on creating, from inside its onClick function's tween's onComplete?

I'm trying to set the 'moving' var back to false, in the tween onComplete event.

game.createClass('S17PuzzleTile', {
    
    init: function(image, moving, pos) {
        
        this.sprite = new game.Sprite(image);
        
        this.sprite.position.set(448, 768);
        this.sprite.anchorCenter();
        this.sprite.interactive = true;
        this.sprite.addTo(game.scene.mg);
        
        this.sprite.mousedown = function() {
            
            if (!game.scene.solved) {
                
                game.audio.playSound('click.wav');
                
                if (!moving) {
                    
                    moving = true;
                    
                    if (pos === 1) {
                        
                        game.Tween.add(this, {
                            x: 832,
                            y: 768
                        }, 1000, {
                            easing: 'Quadratic.Out'
                        }).onComplete(function() {
                            moving = false;
                        }.bind(this)).start();
                        
                    } else if (pos === 2) {
                        
                        game.Tween.add(this, {
                            x: 832,
                            y: 1152
                        }, 1000, {
                            easing: 'Quadratic.Out'
                        }).onComplete(function() {
                            moving = false;
                        }.bind(this)).start();
                        
                    }
                }
                
                pos ++;
            }
        };
    }
});

 

Link to comment
Share on other sites

@Ninjadoodle

You should really try to avoid nested functions (functions inside functions). Now you have your class init function, inside that you have mousedown function, and inside that onComplete functions. Your code gets really hard to read and understand because of that.

Instead create new functions to your class, and bind your functions into those. Like this:

game.createClass('S17PuzzleTile', {
    init: function(image, moving, pos) {
        // Store variables to class
        this.moving = moving;
        this.pos = pos;

        this.sprite = new game.Sprite(image);

        this.sprite.position.set(448, 768);
        this.sprite.anchorCenter();
        this.sprite.interactive = true;
        this.sprite.addTo(game.scene.mg);

        this.sprite.mousedown = this.mousedown.bind(this);
    },

    mousedown: function() {
        if (game.scene.solved) return;
        
        game.audio.playSound('click.wav');

        if (!this.moving) {

            this.moving = true;

            if (this.pos === 1) {

                game.Tween.add(this, {
                    x: 832,
                    y: 768
                }, 1000, {
                    easing: 'Quadratic.Out'
                }).onComplete(this.movingEnd.bind(this)).start();

            }
            else if (this.pos === 2) {

                game.Tween.add(this, {
                    x: 832,
                    y: 1152
                }, 1000, {
                    easing: 'Quadratic.Out'
                }).onComplete(this.movingEnd.bind(this)).start();

            }
        }

        this.pos++;
    },

    movingEnd: function() {
        this.moving = false;
    }
});

Looks a lot cleaner and easier to read? :)

Link to comment
Share on other sites

Hi @enpu

Thank for that! I do understand what you are doing, but it's still a bit over my head.

In the mousedown function, I did have to change this to this.sprite in order to make the tween work.

I'll try to keep using this method instead of what I was using, hopefully it will become second nature.

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...