Jump to content

How to send 'this' object reference after mouse click


Glenn Marshall
 Share

Recommended Posts

I have an interactive node, which when the user clicks, it calls a function inside or outside by passing 'this' so that the function knows what object instance it came from.

In this test - it should print the value '8' but this.val is undefined.

 

any ideas / help !

 

function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', hit);

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    function hit(){
        console.log(this.val);
    }

}

 

Link to comment
Share on other sites

sill having problems - the hit function isn't being called..

function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', this.hit);

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    this.hit=function(){
        console.log('hit');
    }

}

 

Link to comment
Share on other sites

this doesn't do anything either..

function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', hit.bind(this));

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    function hit(n){
        console.log('hit');
    }

}

 

Link to comment
Share on other sites

have you tried this variant:


function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', this.hit.bind(this);

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    this.hit=function(){
        console.log('hit');
    }

}

 

Link to comment
Share on other sites

34 minutes ago, charlie_says said:

have you tried this variant:



function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', this.hit.bind(this);

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    this.hit=function(){
        console.log('hit');
    }

}

 

TypeError: undefined is not an object (evaluating 'this.hit.bind')

Link to comment
Share on other sites

done that - it's calling the function but n.val is showing as undefined..

function Node (_x,_y){

  this.val=8;
  this.chart = new PIXI.Graphics();
  this.chart.interactive=true;
  this.chart.buttonMode = true;

  this.hit=function(n){
    console.log(n.val);
  }

  this.chart.on('pointerdown', this.hit.bind(this));

  this.drawChart = function(){

    this.chart.beginFill(rgb([.5,.5,.5]), 1);
    this.chart.drawCircle(0,0,50);
    this.chart.endFill();
    tree.addChild(this.chart);
  }


}

 

Link to comment
Share on other sites

yeah that works but I need to access the passed in 'this' object.. here's a better example of what I need (shows as undefined)
 

function Node (_x,_y){

  this.val=8;
  this.chart = new PIXI.Graphics();
  this.chart.interactive=true;
  this.chart.buttonMode = true;

  this.hit=function(n){
    console.log(n.val);
  }

  this.chart.on('pointerdown', test.bind(this));

  this.drawChart = function(){

    this.chart.beginFill(rgb([.5,.5,.5]), 1);
    this.chart.drawCircle(0,0,50);
    this.chart.endFill();
    tree.addChild(this.chart);
  }


}

function test(n){
  console.log(n.val);
}

 

Link to comment
Share on other sites

figured it out...

function Node (_x,_y){

    this.val=8;
    this.chart = new PIXI.Graphics();
    this.chart.interactive=true;
    this.chart.buttonMode = true;
    this.chart.on('pointerdown', hit.bind(null,this));

    this.drawChart = function(){

        this.chart.beginFill(rgb([.5,.5,.5]), 1);
        this.chart.drawCircle(0,0,50);
        this.chart.endFill();
        tree.addChild(this.chart);
    }
    function hit(n){
        console.log(n.val);
    }

}

 

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