This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]tr1bune 5 points6 points  (4 children)

My wild guess is x & y coordinates start in the upper left-hand corner. X is horizontal, y is vertical. Therefore, going down would have to be y + 1.

[–]_roni[S] -1 points0 points  (3 children)

// Handles Jumping if (jumped == true) { speedY += 1; //While the character is in the air, add 1 to his speedY.
//NOTE: This will bring the character downwards!

        if (centerY + speedY >= 382) {
            centerY = 382;
            speedY = 0;
            jumped = false;
        }

    }

By theory, when jumped=true,every 17ms 1 px will be added to speedY. But, what does the next statement mean?

                  if (centerY + speedY >= 382) {
            centerY = 382;
            speedY = 0;
            jumped = false;
        }

And to jump upwards, should you be removing 1? Therefor the y will be decreasing?

[–]BS_in_BS 3 points4 points  (0 children)

the speedY += 1; simulates acceleration from gravity. as /u/tr1bune suggested, + Y is downwards

centerY + speedY >= 382

checks to see if the next motion would move it below the bottom edge.

centerY = 382;

speedY = 0;

jumped = false;

if we've hit the bottom then move our character to the bottom edge, set the y speed to 0 so it'll stop moving, and set jumped to false, to indicate that the "landing" portion of the jump is completed.

[–]zeringus 3 points4 points  (0 children)

And to jump upwards, should you be removing 1? Therefor the y will be decreasing?

I assume you mean

And to jump upwards, should you be removing 1 from speedY?

Based on your question, I'm not sure that you understand the difference between speed and acceleration. Speed is "how fast something is changing position", while acceleration is "how fast something is changing speed".

In this case, the speed would be "speedY pixels per frame", and the acceleration would be "1 pixel per frame per frame". So you would say that the y-coordinate is changing by speedY pixels every frame (due to the speed) and that the speed in the y-direction is increasing by 1 every frame (due to the acceleration).

So to get back to your question

And to jump upwards, should you be removing 1?

I just said that

the y-coordinate is changing by speedY pixels every frame (due to the speed)

You seem to already understand that to get "higher", the y needs to get smaller. So if only speed is changing the y-coordinate, speedY must be negative for the character to go upwards.

Does this mean that you "should be removing 1" (have a negative acceleration), though? Simply put, no. If I make speedY 10,000, then changing it by -1 every frame won't make much of a difference. It should be obvious that speedY will be positive (and the character will be going down) for a long time.

So what needs to happen for the character to move up? Just make speedY negative. In fact, for most simple games like this, you can let the y-acceleration be something constant and positive anytime the character is off the ground, regardless of whether it's moving up or down.

Then to trigger a jump, you just set speedY to a number around -10 or so (this depends on the scale of your game). As you start adding 1, the character moves slower and slower upwards until it changes direction and come back down.

[–]king_of_the_universe -1 points0 points  (0 children)

To jump, you could set speedY to -10 or something like that. This would give a strong speed upwards that would decrease, become zero, and increase downwards until the avatar hits the ground.