Jump to content

(FIXED) Method called by mousedown event can't call a different object method


InfinitySamurai
 Share

Recommended Posts

Hi guys,

I'm trying to create a class/object that handles all the logic and input for a sprite in my game. When I call a method from the `sprite.on` method, it calls it fine, but it is then unable to call another method inside the same object, it can't find it.

I'm not really sure what I'm doing wrong, can anyone shed some light for me from the code below?

The click event should call the `onClick` method, which it does. Then, when it tried to call the `changeSpeed` method, it gives me the error `TypeError: this.changeSpeed is not a function`

function Spinner(start_speed, decay, max_speed, sprite){
	this.speed = start_speed;
	this.decay = decay;
	this.max_speed = max_speed;
        this.sprite = sprite;
	this.angle = 0;
        this.click_strength = 0.1;


    this.update = function(delta){
        this.changeSpeed(-this.decay*delta);
        this.angle += this.speed;
        this.sprite.rotation += spinner.speed * delta;
    };

    this.changeSpeed = function(amount){
        this.speed += amount;

        if(this.speed > this.max_speed){
            this.speed = this.max_speed;
        }
        else if(this.speed < 0){
            this.speed = 0;
        }
    };

    this.onClick = function(){
        this.changeSpeed(this.click_strength);
        console.log("spinner got clicked");
    };

    this.sprite.on('mousedown', this.onClick);
};

Error traceback:

Spinner/this.onClick http://127.0.0.1:8080/src/spinner.js:28:9
	[3]</o.prototype.emit https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:8:9704
	[156]</T</e.prototype.dispatchEvent https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:19:5168
	[156]</T</e.prototype.processPointerDown https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:19:7256
	bound  self-hosted:917:17
	[156]</T</e.prototype.processInteractive https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:19:6184
	[156]</T</e.prototype.processInteractive https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:19:5846
	[156]</T</e.prototype.onPointerDown https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.2/pixi.min.js:19:6567
	bound  self-hosted:913:17

I hope that's enough info, let me know if I've breached any conduct and I look forward to your help!

Cheers.

Link to comment
Share on other sites

I'm guessing that this has something to do with the `onClick` function no longer having any reference to what object it is a part of after the event has been fired. In the click example, the onClick function refers directly to a global variable which is not particularly viable in a larger solution. Is there a way I can pass the object along with the event?

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