Skip to main content
jeffery wright
Inspiring
April 29, 2021
Answered

Defined things, not defined again... HTML5 Canvas

  • April 29, 2021
  • 1 reply
  • 480 views

Cannot read property 'b_Title' of undefined ... yet it is indeed defined. 

 

This is the offending code:

	this.p_Button.addEventListener("click", APDHMIDM.bind(this)); // error-free

		function b_Lang(){
			if( IsRussia == 1){
			this.p_Button.b_Title.text = ru_APDHMIDM;
			}
			else{
			this.p_Button.b_Title.text = en_APDHMIDM;
			}
		}
		
		b_Lang();

 

I am differentiating between two languages for a button label to display. This is the code I have been using all along with Zero issues:

this.p_Button.addEventListener("click", APDHMIDM.bind(this));
this.p_Button.b_Title.text = en_APDHMIDM;

 

So, if property 'b_Title' is defined there, why isn't it in a simple else if function? 

 

Ideas, anyone? Thanks. 

    This topic has been closed for replies.
    Correct answer avid_body16B8

    I think this is a problem with scope.

    I would use 

     

    var _this = this

     

    and then use

    function b_Lang(){
    			if( IsRussia == 1){
    			_this.p_Button.b_Title.text = ru_APDHMIDM;
    			}
    			else{
    			_this.p_Button.b_Title.text = en_APDHMIDM;
    			}
    		}
    		
    		b_Lang();

     

     

    1 reply

    avid_body16B8
    avid_body16B8Correct answer
    Legend
    April 29, 2021

    I think this is a problem with scope.

    I would use 

     

    var _this = this

     

    and then use

    function b_Lang(){
    			if( IsRussia == 1){
    			_this.p_Button.b_Title.text = ru_APDHMIDM;
    			}
    			else{
    			_this.p_Button.b_Title.text = en_APDHMIDM;
    			}
    		}
    		
    		b_Lang();

     

     

    jeffery wright
    Inspiring
    April 29, 2021

    These are the sort of things that completely baffle people like me, it does my head in. 

     

    Why var _this = this when you could simply use this in the first place, as I did? 

     

    How did that change any scope when this = the same things as var _this = this?

     

    The value for _this is this, which is what I used in the first place, what does making it the value of a variable do exactly? And how would anyone know to to that? It's black magic. 

     

    Thank you, your suggestion is the solution, I appreciate it.