Question

I am trying to create a Flash game using AS3. I am a newb in programming. The problem that I am having has to do with the game's jumping a gravity mechanics.

Currently, the Up Key makes the character move upwards (speed of 20 pixels/frame) if it's down. If the Up Key isn't down, the character stops moving upwards. If the character is off of the ground, then the force of gravity is in play as well. If the character is off of the ground, the gravity variable (originally 0) increases by (1.5) every frame. The gravity variable then counteracts the character until it's pulled to the ground. On the ground, the gravity variable is back at 0.

This works pretty well. However, the smooth and curve-like jump is only achievable if the Up Key is ALWAYS pressed down. Otherwise, you fall down really quickly if you release the Up Key. I know that I could fix this by changing the jump from a hold-down-key jump to a on-tap-key jump. However, I don't want to do that.

TL;DR I want a perfect parabolic jump!

Was it helpful?

Solution

This is a bit of a broad and complex question. It sounds to me that you're not quite going the right way about applying physics to your game. Here are some concepts that I would look into:

  • Update your game every frame if you're not already addEventListener(Event.ENTER_FRAME, frame);
  • Gravity should be a constant acceleration and always have an effect on your character. const GRAVITY:Number = 9.8; // or whatever value
  • You should check for collisions between your character and the ground. When they collide you set the position of the character to that of the ground (so he doesn't fall through).
  • When the character jumps, you could set the starting velocity. On your ENTER_FRAME event gravity should be applied to that velocity so that your character gradually slows down and then starts to fall.
  • Look into the SUVAT maths equations for simple ways to calculate motion. link

If you want physics, collision detection, etc without having to program them yourself then this is a popular choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top