So i actully thougt Javascript were easier but nope it insn't

So well i am now at the end of Chapter 6 and my Character won’t stop swinging his Arms.
Yea thats it my script url:
https://is.gd/vxlwvo
:smiley: Yea. that’s it.
Great day!

The trouble is in the sendKeyDown() function:

function sendKeyDown(event) {
  var code = event.code;
  if (code == 'ArrowLeft') marker.position.x = marker.position.x - 5;
  isMovingLeft = true;
  if (code == 'ArrowRight') marker.position.x = marker.position.x + 5;
  isMovingRight = true;
  if (code == 'ArrowUp') marker.position.z = marker.position.z - 5;
  isMovingForward = true;
  if (code == 'ArrowDown') marker.position.z = marker.position.z + 5;
  isMovingBack = true;
}

As written, any time a key is pressed (any key – letters, numbers, arrows), this function will

  • set isMovingLeft to true
  • set isMovingRight to true
  • set isMovingForward to true
  • set isMovingBack to true

The if statements are only working on the line they are written on. That is, these two lines:

  if (code == 'ArrowLeft') marker.position.x = marker.position.x - 5;
  isMovingLeft = true;

… say that if the left arrow is pressed, then subtract 5 from the X position and ALWAYS set isMovingLeft to true.

What we want to say is if the left arrow is pressed, then subtract 5 from the X position and if the left arrow is pressed, then set isMovingLeft to true.

In JavaScript, that is done with “curly braces”:

  if (code == 'ArrowLeft') {
    marker.position.x = marker.position.x - 5;
    isMovingLeft = true;
  }

That says, if the left arrow is pressed, then run the two lines of code inside the curly braces (subtract 5 from the X position and set isMovingLeft to true).

After adding curly braces around all of the if statements in sendKeyDown(), that function will look like this:

function sendKeyDown(event) {
  var code = event.code;
  if (code == 'ArrowLeft') {
    marker.position.x = marker.position.x - 5;
    isMovingLeft = true;
  }
  if (code == 'ArrowRight') {
    marker.position.x = marker.position.x + 5;
    isMovingRight = true;
  }
  if (code == 'ArrowUp') {
    marker.position.z = marker.position.z - 5;
    isMovingForward = true;
  }
  if (code == 'ArrowDown') {
    marker.position.z = marker.position.z + 5;
    isMovingBack = true;
  }
  
  if (code == 'KeyC') isCartwheeling = !isCartwheeling;
  if (code == 'KeyF') isFlipping = !isFlipping;
}

And then it will work as expected. EXCEPT for pressing the down arrow. There’s a bug in the sendKeyUp() function that’s preventing that from working. I’m betting you can find that problem, though, so I won’t tell. For now – ask if you can’t track it down :slight_smile:

-Chris

I found it, at the line 86 was KeyDown instead of ArrowDown.
I finally found it, i spent like 1 Hour on it Debugging it.

Yea i hope i will find it, bc. i am not so good at debugging

Yay! Small mistakes like that are such a pain sometimes. I still do things like that all the time – and it sometimes takes me an hour to track down too!

But nice work tracking it down on your own – it’s good practice, and it eventually gets easier :slight_smile:

-Chris