🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

jumping sprite

Started by
14 comments, last by SuperVGA 2 years, 10 months ago

@pbivens67 It doesn't look like much of an improvement over the original, I think. (and you didn't apply the input from me nor @nielsvermeiren )
You should also make sure to define your functions and constant names logically, as in “perform Loop when 32 is pressed” doesn't make the intention as clear as “perform jump when space is pressed”.
In the same vein, having something called “jumpsprite” alter the horizontal offset of a sprite could also be improved.

How about making a dedicated update -function that runs once every in-game tick?

That function would apply the velocity of the player to the sprite, and check for any collisions, as well as altering the vertical velocity according to gravitational acceleration.
You could then make it so that the jump function sets the vertical velocity.

How about doing that? Let Loop be triggered often - perhaps call it update instead, and do the posY and vY changes here.
Then let your jump function set vY, and you're done!

Advertisement

Keyboard codes consist of Key-down codes and Key-up codes. I'm guessing 32 is a key down code, which can be triggered several times per millisecond. What's the key-up code for the spacebar? Try using that.

I also noticed none of your functions had return calls. Each one would drop into the next… Or is that a C# thing?? I have to admit I haven't messed with C# yet.

Winternight said:

I also noticed none of your functions had return calls.

Could you clarify what you mean by this? Are you used to explicitly returning void, or am I completely misreading you?

can you go into more detail about vY or the velocity variable

Try this:

void jumpsprite()
{
		posY = 0.0625f*(x*x);
		x += delta;
		move_sprite += 1.0f;
		if (move_sprite >= 40.0f)
		{
			move_sprite = 40.0f;
			posY = 0.0f;
		}
		if (x == min || x == max)
		{
			delta = -delta;
		}
		return(0);
}

void Loop(int value)
{
	jumpsprite();
	glutPostRedisplay();
	glutTimerFunc(50, Loop, 0);
	return(0);
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
		
	case 32:
		Loop(0);
		break;
	}
	return(0);
}

Now it should do something when you press the spacebar, and only when you press the spacebar.

@winternight No need for the return calls from functions returning void. Not here, and not in C# either. You're probably thinking of case-labels in a switch-statement.
These work differently from functions…

Winternight said:

Keyboard codes consist of Key-down codes and Key-up codes. I'm guessing 32 is a key down code, which can be triggered several times per millisecond. What's the key-up code for the spacebar? Try using that.

In this case (and many others) the keycodes are valid for both events.

@pbivens67

pbivens67 said:

can you go into more detail about vY or the velocity variable

Yes: vertical velocity would be shortened vY, and is the value for the change in the vertical position.

As you might already know, velocity can be derived from the position, and the acceleration can be derived from the velocity. (in both cases, it's the difference across an interval of time)
So what you do with your vertical sprite position is to add vY to it at every tick. Perhaps ticks and frames are the same in your game - that's fine. Try it there and you'll se what I mean.

What you absolutely need to remember is that you're not going to get anywhere by binding the change of the vertical position to your spacebar-handler (unless your game is turn based, which I don't think it is)
You need a separate function to conduct updates in. It can be parallel to the drawing as I said, but it can also just be the same function as you swap buffers in.

Another thing I mentioned earlier is the case values in the switch statement. If you somewhere define, for instance

struct GameKeyCodes {
 static const int ESCAPE = 27;
 static const int SPACE = 32;
};

You can use case GameKeyCodes.SPACE instead of case 32. It will be easier to read that way.

Another thing about this Loop() that runs over and over again, also causes jumpsprite() to be invoked over and over again. How about removing that jumpsprite() from there, so it won't be run over and over again?

The space bar handling can still trigger jumpsprite(), but it needs to do that directly, instead of calling Loop(). You should tell glut to call Loop() in a timer function somewhere downstream from main(). This decouples the key stuff from the drawing+position update.
The next thing is to alter vY upon jumpsprite() and also to reset vY when your character lands, but I'm sure that'll be obvious once you get the fundamentals worked out.

This topic is closed to new replies.

Advertisement