Combine ideas without interference

Hi there, I would like to do 2d game, I have Player class I just only show the main reference variable

class Player {
float  Xloc;
float  Yloc;
PVector location;
float  Width;
float  Height;
float playerVelocityX;
float playerVelocityY;
float playerSpeed;
float playerJumpSpeed; 
float gravity;
....
}
// this [code](http://www.hobbygamedev.com/processingplatformer/Platformer.zip) inspired me to put my 
    Keyboard movements in class as well, and instead of "if" statement using "switch" option, like this  
class Keyboard {
Boolean   isLeft,  isRight, isUp, isDown;
void keyPress() {
switch(keyCode) {
case RIGHT: isRight  = true; break;
case LEFT: isLeft = true; break;
case UP: isUp = true; break;
case DOWN: isDown = true; break;
}
}
void keyRelease() {
switch(keyCode) {
case RIGHT: isRight = false; break;
case LEFT: isLeft = false; break;
case UP: isUp = false; break;
case DOWN: isDown = false; break;
}
//Which I can recall in my main tab under void draw function
Player p;
Keyboard k;
 ...
p= new Player();
k= new Keyboard();
...
void draw() {
Keyboard.keyPress();
..
}

How to do it without "Cannot make a static reference to the non-static method" Error message?

Answers

  • Are you missing a { on line 18?

  • not really enough to go on there, especially as you don't indicate the line you get the error on...

    but i guess this:

    Keyboard.keyPress();
    

    is what the problem is. Keyboard (capital K) is a class, not an instance of a class (which would typically start with a lower case letter. which is why java naming standards are important). so Keyboard.keyPress() is an attempt to call keyPress as a static method.

    k.keyPress();
    

    is probably what you want. k IS an instance.

  • thanks TfGuy44 and koogs

    however its not clear how can I use the variables from the Player class in the Keyboard class, that eventually it works?Do I need to initialise the move function first in the Player class, how will the Up/left/right/down interact with the player?

  • however its not clear how can I use the variables from the Player class in the Keyboard class, that eventually it works?

    ???

    if p is global then you can access members of p in Keyboard class. but that's hacky.

    i'd drop the Keyboard class completely, it's just constraining yourself too much, move that code the keyPressed() and keyReleased() methods (processing standard methods). make the flags global so they are available everywhere. (which is also a bit lackadaisical but better)

    using java in processing is a bit problematic for reasons i won't go into (inner classes). it stops you doing certain things.

    Do I need to initialise the move function first in the Player class

    ???

Sign In or Register to comment.