I'm not sure if you've ever had the event where you were typing code in Visual Studio/Express and the Intellisense window pops out and you can't see what's behind it. This happens to me when I sometimes forget the variable name of something, especially if I'm in a loop at the time.
Well, the solution is to simply press the control button. This works with both the left and right control buttons, in case you are curious. A screenshot of this in action is below:
If you have any questions, comments, or concerns, remember to leave me a comment. If you enjoyed the post, remember to Subscribe to my RSS feed.
Or email me at finaiized(at)gmail.com if you want. I'm welcome to all suggestions you may have, and am willing to help you.
Flatformer
Flatformer is a blog focusing on creating games, tech news, and numerous tutorials about the web. It's all right here.
March 07, 2010
February 21, 2010
Making a character jump in XNA/C# (Basic Concept Included)
In the last tutorial, I briefly went over implementing gravity in games. By the end of the tutorial, we are going to program a character jumping when a key is pressed. I included 'Basic Concept Included' in the title because it doesn't use any XNA/C# specific stuff, just some simple math.
I’m going to define all the variables in one go:
Now, to the Initialize() function, provided by XNA:
And the Update(), where the following is updated every frame:
We can now draw it with spriteBatch:
The value of jumpspeed will look like this:
-14,-13,-12….0,1,2,3….
If you have any questions, comments, or concerns, remember to leave me a comment. If you enjoyed the post, remember to Subscribe to my RSS feed. Or email me at finaiized(at)gmail.com if you want. I'm welcome to all suggestions you may have, and am willing to help you.
Jump?
Well, I'll first start off by telling you want happens in a 'jump'. First, upward force is applied and the character moves up. Somewhere down the road, gravity acts on it and it moves down.I’m going to define all the variables in one go:
/*GraphicsDeviceManager, SpriteBatch, Texture2D and Vector2 may be found only in XNA. They are just used for drawing objects and defining locations.*/ GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D charr; Vector2 charPos;
bool jumping; //Is the character jumping? float startY, jumpspeed = 0; //startY to tell us //where it lands, jumpspeed to see how fast it jumps
Now, to the Initialize() function, provided by XNA:
charr = Content.Load<Texture2D>("ow"); //Load image charPos = new Vector2(230, 450);//Char loc, X/Y startY = charPos.Y;//Starting position jumping = false;//Init jumping to false jumpspeed = 0;//Default no speed
And the Update(), where the following is updated every frame:
//Init keyboard KeyboardState keyState = Keyboard.GetState();
if (jumping) { charPos.Y += jumpspeed;//Making it go up jumpspeed += 1;//Some math (explained later) if (charPos.Y >= startY) //If it's farther than ground { charPos.Y = startY;//Then set it on jumping = false; } }
else { if (keyState.IsKeyDown(Keys.Space)) { jumping = true; jumpspeed = -14;//Give it upward thrust } }
We can now draw it with spriteBatch:
spriteBatch.Begin();
spriteBatch.Draw(charr, charPos, Color.White);
spriteBatch.End();
Explanation
We update charPos’s Y axis by jumpspeed. Since jumpspeed is set to –14 when space is pressed, this makes it go up (or down, depends on the game engine/platform you are using). Now, the magic- by adding 1 to jumpspeed every frame after, it will turn positive after a while and move down instead!The value of jumpspeed will look like this:
-14,-13,-12….0,1,2,3….
If you have any questions, comments, or concerns, remember to leave me a comment. If you enjoyed the post, remember to Subscribe to my RSS feed. Or email me at finaiized(at)gmail.com if you want. I'm welcome to all suggestions you may have, and am willing to help you.
February 20, 2010
Simple 2D Gravity In Games [Explanation]
Introduction
Sometimes understanding gravity can be hard.I mean, yeah, it's always around us, it's always been there. But when trying to fake gravity into games- platformers is my example- it can sometimes get confusing. After this article, I hope you'll know how to implement gravity in any language- and since gravity is basically math, I'm not going to recommend a particular programming language you should use to write it.
What's better to start than pull up a definition of gravity? So here's one, located here:
gravitational force: the attraction due to gravitation that the Earth or another astronomical object exerts on an object on or near its surfaceWe obviously don't have 'Earth' in our game per say, but we know it has to do with attraction. Let's keep this in mind and move on.
(I know the image sucks). Above is a basic representation of gravity. Something is forced up, and gravity acts on it and it moves down gradually. How do we represent this change using math? Well, we’ll need to define gravity of course…
To the basics
I'm going to start be representing super-basic gravity. We'll draw an image and make it move down at an increasingly fast pace, just like in real life.
//Declare gravity and add it to character float gravity = 0.1f; character.Y += gravity; //To make it drop increasingly fast gravity += 0.1f;
Limiting Gravity
The example above is good and all, but it drops increasingly fast until it's wayyy too fast. I'm going to fix that by simply adding a check:if (gravity > 0.4f) { gravity = 0.4f; }
Using Velocity
I’m going to use velocity in the following examples. Velocity is the rate of change in the position of an object as it moves in a particular direction. We can use velocity coupled with gravity so we can actually travel in an ark- as it can take account of the current direction and add gravity on top of that.Here’s some code showing that:
Vector2 velocity; float gravity = 0.1f; if (keyPressed==Keys.Right) { velocity = 3.0f; } velocity.Y += gravity; mainChar.X += velocity.X; mainChar.Y += velocity.Y;
Conclusion
Here concludes my gravity tutorial.
Subscribe to:
Posts (Atom)