Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

A question about classes in Panda 2


Ninjadoodle
 Share

Recommended Posts

Hi @enpu / anyone else who might know how to do this :)

How do I access the 'odd' and 'touched' variables I setup in the following class, from the sprites 'mousedown' function?

game.createClass('S04Hitmap', {

    odd: false,
    touched: false,
    
    init: function(x, y) {
        
        this.sprite = new game.Sprite('s04Hitmap.png');
        
        this.sprite.position.set(x, y);
        this.sprite.anchorCenter();
        this.sprite.interactive = true;
        this.sprite.addTo(game.scene.mg);
        
        this.sprite.mousedown = function() {
            
            if (!this.touched && game.scene.ready) {
                
                game.Tween.add(game.scene.bunny1.sprite, {
                    y: 232
                }, 250, {
                    delay: 0,
                    easing: 'Back.In'
                }).start();
                
                if (odd) {
                    
                    game.scene.passed(this.x, this.y);
                    
                } else {
                    
                    game.scene.failed(this.x, this.y);
                    
                }
            }
        }
    }
});

Thank you in advance?

Link to comment
Share on other sites

You should bind the mousedown function so that then inside of it you can use this keyword to refer back to the class:

game.createClass('S04Hitmap', {
    odd: false,
    touched: false,
    
    init: function(x, y) {
        this.sprite = new game.Sprite('s04Hitmap.png');

        this.sprite.position.set(x, y);
        this.sprite.anchorCenter();
        this.sprite.interactive = true;
        this.sprite.addTo(game.scene.mg);
        
        this.sprite.mousedown = this.mousedown.bind(this);
    },

    mousedown: function() {
        if (!this.touched && game.scene.ready) {
            
            game.Tween.add(game.scene.bunny1.sprite, {
                y: 232
            }, 250, {
                delay: 0,
                easing: 'Back.In'
            }).start();
            
            if (this.odd) {
                game.scene.passed(this.sprite.x, this.sprite.y);
            } else {
                game.scene.failed(this.sprite.x, this.sprite.y);
            }
            
        }
    }
});

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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