Javascript calling method in same class comes undefined?
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
add a comment |
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
I'm guessingRecordInputs
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 asthis.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 howRecordInputs
is used. The calling context will be important here
– charlietfl
Nov 14 '18 at 16:57
add a comment |
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
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
javascript class methods undefined
edited Nov 14 '18 at 16:52
ibrahim mahrir
22.2k41951
22.2k41951
asked Nov 14 '18 at 16:47
user7201716
I'm guessingRecordInputs
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 asthis.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 howRecordInputs
is used. The calling context will be important here
– charlietfl
Nov 14 '18 at 16:57
add a comment |
I'm guessingRecordInputs
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 asthis.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 howRecordInputs
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
add a comment |
1 Answer
1
active
oldest
votes
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);
1
Very possibly wrong context based on argumentevent
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
1
Very possibly wrong context based on argumentevent
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
add a comment |
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);
1
Very possibly wrong context based on argumentevent
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
add a comment |
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);
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);
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 argumentevent
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
add a comment |
1
Very possibly wrong context based on argumentevent
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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