Javascript calling method in same class comes undefined?










0















So I'm relatively new to js and I'm trying to call a method 'lerp' inside my 'recordInputs' class. The recordInputs class is called somewhere else and works fine without the lerp function. The problem is that when the playerImage.x/y is equal to the lerp function a console error appears and says the 'lerp' method is undefined...



Here is the code:



class PlayerMoveClass 

lerp(start, end, time)
return (1-time) * start + time * end;


RecordInputs(event)
currentXPos = playerImage.x;
currentYPos = playerImage.y;

xMousePosition = event.clientX;
yMousePosition = event.clientY;

playerImage.x = lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = lerp(currentYPos, yMousePosition, 0.1);
console.log("X POS: " + playerImage.x + " Y POS: " + playerImage.y);




Thanks in advance to anyone who can help!










share|improve this question
























  • I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

    – ibrahim mahrir
    Nov 14 '18 at 16:53







  • 5





    You need to refer to it as this.lerp

    – Robin Zigmond
    Nov 14 '18 at 16:54











  • maybe use this.lerp() when you call the method?

    – Dexter0015
    Nov 14 '18 at 16:54











  • Need to show how RecordInputs is used. The calling context will be important here

    – charlietfl
    Nov 14 '18 at 16:57
















0















So I'm relatively new to js and I'm trying to call a method 'lerp' inside my 'recordInputs' class. The recordInputs class is called somewhere else and works fine without the lerp function. The problem is that when the playerImage.x/y is equal to the lerp function a console error appears and says the 'lerp' method is undefined...



Here is the code:



class PlayerMoveClass 

lerp(start, end, time)
return (1-time) * start + time * end;


RecordInputs(event)
currentXPos = playerImage.x;
currentYPos = playerImage.y;

xMousePosition = event.clientX;
yMousePosition = event.clientY;

playerImage.x = lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = lerp(currentYPos, yMousePosition, 0.1);
console.log("X POS: " + playerImage.x + " Y POS: " + playerImage.y);




Thanks in advance to anyone who can help!










share|improve this question
























  • I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

    – ibrahim mahrir
    Nov 14 '18 at 16:53







  • 5





    You need to refer to it as this.lerp

    – Robin Zigmond
    Nov 14 '18 at 16:54











  • maybe use this.lerp() when you call the method?

    – Dexter0015
    Nov 14 '18 at 16:54











  • Need to show how RecordInputs is used. The calling context will be important here

    – charlietfl
    Nov 14 '18 at 16:57














0












0








0








So I'm relatively new to js and I'm trying to call a method 'lerp' inside my 'recordInputs' class. The recordInputs class is called somewhere else and works fine without the lerp function. The problem is that when the playerImage.x/y is equal to the lerp function a console error appears and says the 'lerp' method is undefined...



Here is the code:



class PlayerMoveClass 

lerp(start, end, time)
return (1-time) * start + time * end;


RecordInputs(event)
currentXPos = playerImage.x;
currentYPos = playerImage.y;

xMousePosition = event.clientX;
yMousePosition = event.clientY;

playerImage.x = lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = lerp(currentYPos, yMousePosition, 0.1);
console.log("X POS: " + playerImage.x + " Y POS: " + playerImage.y);




Thanks in advance to anyone who can help!










share|improve this question
















So I'm relatively new to js and I'm trying to call a method 'lerp' inside my 'recordInputs' class. The recordInputs class is called somewhere else and works fine without the lerp function. The problem is that when the playerImage.x/y is equal to the lerp function a console error appears and says the 'lerp' method is undefined...



Here is the code:



class PlayerMoveClass 

lerp(start, end, time)
return (1-time) * start + time * end;


RecordInputs(event)
currentXPos = playerImage.x;
currentYPos = playerImage.y;

xMousePosition = event.clientX;
yMousePosition = event.clientY;

playerImage.x = lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = lerp(currentYPos, yMousePosition, 0.1);
console.log("X POS: " + playerImage.x + " Y POS: " + playerImage.y);




Thanks in advance to anyone who can help!







javascript class methods undefined






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:52









ibrahim mahrir

22.2k41951




22.2k41951










asked Nov 14 '18 at 16:47







user7201716



















  • I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

    – ibrahim mahrir
    Nov 14 '18 at 16:53







  • 5





    You need to refer to it as this.lerp

    – Robin Zigmond
    Nov 14 '18 at 16:54











  • maybe use this.lerp() when you call the method?

    – Dexter0015
    Nov 14 '18 at 16:54











  • Need to show how RecordInputs is used. The calling context will be important here

    – charlietfl
    Nov 14 '18 at 16:57


















  • I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

    – ibrahim mahrir
    Nov 14 '18 at 16:53







  • 5





    You need to refer to it as this.lerp

    – Robin Zigmond
    Nov 14 '18 at 16:54











  • maybe use this.lerp() when you call the method?

    – Dexter0015
    Nov 14 '18 at 16:54











  • Need to show how RecordInputs is used. The calling context will be important here

    – charlietfl
    Nov 14 '18 at 16:57

















I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

– ibrahim mahrir
Nov 14 '18 at 16:53






I'm guessing RecordInputs is attached as an event listener somewhere, in which case your code won't work. Please provide that code.

– ibrahim mahrir
Nov 14 '18 at 16:53





5




5





You need to refer to it as this.lerp

– Robin Zigmond
Nov 14 '18 at 16:54





You need to refer to it as this.lerp

– Robin Zigmond
Nov 14 '18 at 16:54













maybe use this.lerp() when you call the method?

– Dexter0015
Nov 14 '18 at 16:54





maybe use this.lerp() when you call the method?

– Dexter0015
Nov 14 '18 at 16:54













Need to show how RecordInputs is used. The calling context will be important here

– charlietfl
Nov 14 '18 at 16:57






Need to show how RecordInputs is used. The calling context will be important here

– charlietfl
Nov 14 '18 at 16:57













1 Answer
1






active

oldest

votes


















4














Referencing class members requires the use of the this keyword.



In your case:



playerImage.x = this.lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = this.lerp(currentYPos, yMousePosition, 0.1);



If you are using RecordInputs as an event listener (as suggested in the comments), you may also need to add this constructor to your class to bind this correctly:



constructor() 
this.RecordInputs = this.RecordInputs.bind(this);






share|improve this answer




















  • 1





    Very possibly wrong context based on argument event likely from a dom event listener

    – charlietfl
    Nov 14 '18 at 16:56






  • 1





    Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

    – user7201716
    Nov 14 '18 at 17:04










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305090%2fjavascript-calling-method-in-same-class-comes-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Referencing class members requires the use of the this keyword.



In your case:



playerImage.x = this.lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = this.lerp(currentYPos, yMousePosition, 0.1);



If you are using RecordInputs as an event listener (as suggested in the comments), you may also need to add this constructor to your class to bind this correctly:



constructor() 
this.RecordInputs = this.RecordInputs.bind(this);






share|improve this answer




















  • 1





    Very possibly wrong context based on argument event likely from a dom event listener

    – charlietfl
    Nov 14 '18 at 16:56






  • 1





    Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

    – user7201716
    Nov 14 '18 at 17:04















4














Referencing class members requires the use of the this keyword.



In your case:



playerImage.x = this.lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = this.lerp(currentYPos, yMousePosition, 0.1);



If you are using RecordInputs as an event listener (as suggested in the comments), you may also need to add this constructor to your class to bind this correctly:



constructor() 
this.RecordInputs = this.RecordInputs.bind(this);






share|improve this answer




















  • 1





    Very possibly wrong context based on argument event likely from a dom event listener

    – charlietfl
    Nov 14 '18 at 16:56






  • 1





    Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

    – user7201716
    Nov 14 '18 at 17:04













4












4








4







Referencing class members requires the use of the this keyword.



In your case:



playerImage.x = this.lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = this.lerp(currentYPos, yMousePosition, 0.1);



If you are using RecordInputs as an event listener (as suggested in the comments), you may also need to add this constructor to your class to bind this correctly:



constructor() 
this.RecordInputs = this.RecordInputs.bind(this);






share|improve this answer















Referencing class members requires the use of the this keyword.



In your case:



playerImage.x = this.lerp(currentXPos, xMousePosition, 0.1);
playerImage.y = this.lerp(currentYPos, yMousePosition, 0.1);



If you are using RecordInputs as an event listener (as suggested in the comments), you may also need to add this constructor to your class to bind this correctly:



constructor() 
this.RecordInputs = this.RecordInputs.bind(this);







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 16:59

























answered Nov 14 '18 at 16:55









MTCosterMTCoster

3,83922141




3,83922141







  • 1





    Very possibly wrong context based on argument event likely from a dom event listener

    – charlietfl
    Nov 14 '18 at 16:56






  • 1





    Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

    – user7201716
    Nov 14 '18 at 17:04












  • 1





    Very possibly wrong context based on argument event likely from a dom event listener

    – charlietfl
    Nov 14 '18 at 16:56






  • 1





    Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

    – user7201716
    Nov 14 '18 at 17:04







1




1





Very possibly wrong context based on argument event likely from a dom event listener

– charlietfl
Nov 14 '18 at 16:56





Very possibly wrong context based on argument event likely from a dom event listener

– charlietfl
Nov 14 '18 at 16:56




1




1





Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

– user7201716
Nov 14 '18 at 17:04





Yea you were right thanks! Yea was using it as an event listener as well, added that constructor and works like a charm!

– user7201716
Nov 14 '18 at 17:04



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305090%2fjavascript-calling-method-in-same-class-comes-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo