1

I'm trying to make a simple game where the player can move a sphere by using either wasd or the arrow keys. I have some code which determines playermovement:

function playerMovement(){
  if (keyIsDown(UP_ARROW) || keyIsDown(087)) {
    print("up");
    player.xpos += 0 * player.v;
    player.ypos += -1 * player.v
  }
  if (keyIsDown(DOWN_ARROW) || keyIsDown(083)) {
    print("down");
    player.xpos += 0 * player.v;
    player.ypos += 1 * player.v;
  }
  if(keyIsDown(RIGHT_ARROW) || keyIsDown(068)) {
    print("right");
    player.xpos += 1 * player.v;
    player.ypos += 0 * player.v;
  }
  if(keyIsDown(LEFT_ARROW) || keyIsDown(065)) {
    print("left")
    player.xpos += -1 * player.v;
    player.ypos += 0 * player.v;
  }
}

The code that is giving me trouble is the

keyIsDown(065)

part! The keycode for the letter a is 065, but when I press a nothing happens. All the other keys work fine! When using print(keyCode) to see what the keyCode for a is, it prints 97. However, changing the code to keyIsDown(097) doesn't work either! I'm very confused what's going on here.

  • Please post all of the relevant code. – Unmitigated Oct 17 '18 at 02:47
  • can we see keyIsDown? – Narendra Oct 17 '18 at 02:51
  • Possible duplicate of [What does an integer that has zero in front of it mean and how can I print it?](https://stackoverflow.com/q/35521278/608639), [Is 00 an integer or octal in Java?](https://stackoverflow.com/q/24031843/608639), [Why is 08 not a valid integer literal in Java?](https://stackoverflow.com/q/7218760/608639), [Why int j = 012 giving output 10?](https://stackoverflow.com/q/23039871/608639), etc. Also see [java octal integer site:stackoverflow.com](https://www.google.com/search?q=java+octal+integer+site:stackoverflow.com) – jww Oct 17 '18 at 04:02

3 Answers3

0

This is probably related your capslock being on or off. The keycodes are:

a = 97
A = 65

See the chart here http://www.asciitable.com/

0

Try using just the values without prefixing it with a 0.And check it with the caps-lock too.

use 65 instead of 065 and it is better if you could do that to others as well.

Check the below link for a description of char codes for key.

Sathiraumesh
  • 5,949
  • 1
  • 12
  • 11
0

A numeric literal starting with a leading zero is interpreted as octal (base 8). The numeric literal 065 thus represents a decimal value of 53. This is not the keycode you are looking for.

The other numeric literals all contain digits greater than 7 and thus cannot be interpreted as octal.

Simply omit the leading zero unless you specifically intend to use octal notation.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190