-2

I have this code, and when I press space, it goes all weird. I have tested it, and jump() is being called when I press space. I don't receive an error for anything.

public static void update(int delta)
{
    if (!grounded && !jumping)
        y += gravity;
        if (y > 275)
        {
            grounded = true;
            jumpTime = 0;
        }
        if (jumping && jumpTime < maxJumpTime)
        {
            y -= jumpPower;
            jumpTime++;
        }
        if (jumpTime > maxJumpTime)
        {
            jumping = false;
            jumpTime = 0;
        }
        if (jumping)
        {
            grounded = false;
        }
}

public static void jump(int power)
{
    if (grounded)
    {
        jumpPower = power;
        grounded = false;
        jumping = true;
    } else
        return;
}

The variables are:

x
y 
gravity (1)
jumpTime
maxJumpTime (5)
jumpPower (1).
CubeJockey
  • 2,209
  • 8
  • 24
  • 31

1 Answers1

1

Without a better idea of what "going weird" means, I would assume it has something to do with you labeling your methods as static. I would think the compiler would get mad when you try and access a non-static variable (x and y) from a static context. Jumping and updating seem like things you want to be individual to the object not something that will affect all of the objects.

I would recommend giving this a read: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

keeslinp
  • 176
  • 1
  • 5
  • x and y are static variables. All my variables are static unless they REALLY can't be. – PineappleLime Mar 23 '14 at 20:47
  • 2
    You have it backwards, you should only use static if you REALLY need to use it, most times it just means it is incorrectly designed. On another note, if you're going to be a jerk to people trying to help you, don't ask questions, let alone poorly phrased questions with little information. You've just taken away any desire I had to help you. Best of luck. – keeslinp Mar 24 '14 at 16:09
  • 2
    H3ckboy is right, and I have noticed on a lot of PinappleLimes questions, he doesn't try very hard after someone suggests something. Such as this. You don't even give his answer a try, you just say "no" right of the bat. I'm +1 H3ckboy because I can almost guarantee he is right. Choosing between static and non static isn't a coding preference. They actually mean something and depending on how you use them is determined by what the variables or methods are used for. More often than not, you will NOT use static for anything in a game other than resource loaders/caches classes. – Samich Mar 27 '14 at 19:03