Robot Foot Games Helping you improve your games as we improve our own. :)

12May/120

Moving a sprite around the screen – part 1 – point A to point B

We're going back to some basics with this tutorial. I plan on doing a couple of spin-offs of this to address other issues like; facing the direction you're moving, moving along a series of points and some other ways of moving sprites that are a little more complex.

So here's the scenario for this part. You have a top-down point and click game and wherever you click on the screen, your character moves to that point. Nice and simple to begin with.

 

Starting out.

I created a new Windows Game 4.0 named "PointAndClickGame". I also added the image below to the "Content" folder:

 

Code. I tried to comment the code well enough to explain everything but I'll give some extra insight if I believe it'll help.

At the top of the Game1.cs class, lets add a few variables:

 

Now onto our LoadContent()method, should be pretty self-explanatory with the comments:

 

Inside of the Update(GameTime gameTime)method:

This is where the magic happens, so there's quite a bit going on. We get the direction from our player to his destination and then we move in that direction. When we reach our destination we set our direction to 0 in order to stop the character.

 

Now all we need to do is draw the image. Inside of Draw(GameTime gameTime):

We used one of the longer overloads that takes a Vector2 position and a Vector2 origin. This lets us draw the image so that 'spritePosition' is located at the very center of our character ('spriteTexture').

 

That's everything! When you run the game you should be able to click (and hold if you want) anywhere on the screen and the sprite will move to that location. If you want to increase/decrease the speed that the sprite moves you can change the 'speed' variable at the very top. I'm not sure what I'll tackle next for part 2 of this series but you can always follow me on twitter or add our RSS feed to your favorite reader to catch the next post when it goes live.

19Mar/120

Drawing extra symbols (squared/square root) in XNA

I'm starting to work on adding squared (i.e. 6²) and square root (i.e. √169) math to Math Buster 2 and I realized that I wouldn't be able to draw the square/square root symbol out of the box so I needed to modify a spritefont to include these characters.

First off, you need a font that supports these characters. An easy way to check if there is a specific character in your font, and you have the font installed to your windows font folder, is to use the built in Character Map in windows. You can find this at Programs » Accessories » System Tools » Character Map or you can press Windows+R (for the run dialog) then type in charmap and hit enter. This brings up a little windows that allows you to select a font and see all characters available for the selected font. Some fonts, as long as it's not a really obscure one, will even display the keystroke needed to type it in the bottom right of the window. When I double click on the subscript 2 (²) it shows "Keystroke: Alt+0178" in the bottom right. Neat!

Now you should have a font that supports the character we want, we need to modify our spritefont to include this character. By default spritefonts include characters 32-126, which includes most letter/numbers/symbols you can see on your keyboard. If you want extra symbols like ™ and ® you'll have to add them separately. Here's my spritefont that's been modified to include squared and square root symbols:

We add a CharacterRegion for each individual character we want to add to our spritefont (you can also add ranges of characters, like the default 32-126 CharacterRegion). As for where I found the numbers, like "&#8730" for the square root symbol, I just did some Googling to figure out what the square root symbol was (this site was helpful which had the squared symbol, but not the square root symbol). Now that you've modified your spritefont with these extra characters, you should be able to draw them with this spritefont! You just have to remember the specific keystroke so you can actually enter it in code. You can enter these new symbols straight into your DrawString call by using the specific keystroke:

 

1Dec/110

Farseer Physics Platformer 3.3.1 Update

It sucks when updates to a project break existing tutorials out there, so I'm posting the 3.3.1 update to @RoyTries XNA Farseer Platform Physics Tutorial that he wrote. @RabidLionGames updated it when the 3.2 update came out and a few things got broken in the 3.3.1 update so hopefully this helps out anyone who's looking for a working version in 3.3.1!

This post is just going to show the breaking changes from 3.2 to 3.3.1. Make sure you complete the original tutorial to get the project setup and read the 3.2 update to see the changes made in that version. Make sure to read the part about the unit conversion from 2.1.3 to 3.2 otherwise you'll have massively huge objects on your screen! After those 2 are done and set up there's a handful of changes below that you can make below to have a working version with 3.3.1.

 

Changes from 3.2 to 3.3.1:

PhysicsObject.cs

Find the following variable and remove it altogether:

 

Here is what the 3.2 version of our SetupPhysics method looked like:

In 3.3.1 we have a BodyFactory that will create a Body and attach a Fixture to it so we don't have a need for the explicit 'fixture' object that was declared at the top (that's why we removed it!). Your new SetupPhysics method should now look like the following:

 

In StaticPhysicsObject.cs find the following line:

and replace it with this (once again, 'fixture' is gone so we update it):

 

In CompositePhysicsObject.cs the following:

Gets changed to:

Once again, we simply removed anything that referred to the 'fixture' object since we're not using it.

 

In SpringPhysicsObject.cs find the following line:

and replace it with this (guess what... no 'fixture' object so we cut it out!):

 

Character.cs remains the same as the 3.2 version.

Now for the bread and butter, CompositeCharacter.cs. Change:

to:

 

Inside of the SetupPhysics method you can remove these two lines:

and replace them with this line:

 

a little further down in the SetupPhysics method you'll find these lines:

and you can change them to this (using the BodyFactory to create the Body and attach a Fixture. Since wheel is a Body instead of a Fixture now, we remove the extra ".Body" found in the bottom two lines):

 

The last change near the bottom of the SetupPhysics method is to update these two lines since we're not using the fixture variable anymore:

becomes:

 

You can download a ZIP file of all of these updated classes here to check for differences if something didn't work.

30May/112

Adding melee to PSK

Update: Went through the entire tutorial in a fresh solution and fixed one bug where the enemies no longer killed you. The tutorial has been updated (UpdateEnemies(...) method in Level.cs) and now works as-is. If you have problems, make sure to download the file just below and compare it with yours to see where you may have messed up!

You can download a working version below.

 

Here's a video showing off the final product of this tutorial:

Here is what I used for his attack animation as a placeholder, feel free to use it. Place this in the same place as the other animations: “HighResolutionContent>Sprites>Player”. Also make sure to add it to your project: Right click on the Player folder and Add>Existing Item>Attack.png

 

 


 

Player.cs

Find the following:

After it add the following:

 

Next find the following:

After that add the following:

 

At the top of Player.cs find the following line:

After that add the following line:

 

Now find the LoadContent method in Player.cs and find the following line:

After that add the following:

 

Find the DoJump method and add a new method after it:

 

Now our attack is setup but we need a keybind so we can actually use it! At the very top of our player class add just the KeyboardState line below:

Now add the following in the GetInput method:

 

Now onto the Update method, after the GetInput() call add the following:

 

Find the following in the Update method:

And change it to look like the following:

 

BUILD THE SOLUTION AT THIS POINT TO MAKE SURE YOU DON’T HAVE ANY ERRORS.

 

Enemy.cs

In Enemy.cs find the following:

and add the following after it:

 

Here we added a death animation, death sound, and an IsAlive variable that will allow our enemies to die. We also added a timer so we can make our enemy disappear a certain amount of time after he dies. If you want to change how long they're alive simply change the deathTimerMax variable.

To initialize the IsAlive variable find the constructor for our Enemy, and after,

add:

 

First we need to load the sound into the project because it isn’t there by default. In the solution explorer go to “SharedContent” right click on Sounds>Add>Existing Item and add “MonsterKilled”. Right click on MonsterKilled and go to Properties. In the Properties window find ContentProcessor and from the drop down menu to the right of that choose Sound Effect – XNA Framework. At the very top add another using statement:

Once you add the sound, you'll need to change it's properties because it's considered a 'Song' instead of a 'Sound Effect'. Right click on the sound after you add it to your solution and hit properties. Under 'Content Processor' change it from Song to Sound Effect. We need to load the new sound so find the LoadContent method and add the following at the bottom:

While we’re in the LoadContent method add the following by the other animations: (NOTE: You also need to add this animation to your solution because it isn't there by default. Navigate to HighResContent>Sprites and then in each folder (MonsterA, MonsterB, etc) you'll have to add the existing item like you did above with the sound)

 

Inside the Update method add this block of code.

 

Inside of the Draw method let's change the code a little bit. Change the if/else statement to look like the following:

 

Under the Draw method add a new method:


 

Level.cs

Next in Level.cs find the following in the UpdateEnemies method (and add the bold part)

Now add this if statement after the one above:

 

Now under the UpdateEnemies method add a new method:

 

Now in the Draw(...) method find the block of code to draw the enemies and change it to this:

 

And that's it! Give it a try and see if you can finally kill those bad guys!

30May/110

Double Jump in PSK

Double jumping is pretty common in most platformers so I thought I'd share some code I use for it.

Here's a video showing the final product:

In Player.cs add the following underneath the other jumping code. (MaxJumpTime, JumpControlPower, etc.)

 

Now find this line in Update(...)

and add this after it:

 

Now in the DoJump(...) method find this block of code

and change it to this:

 

And that's it! You're able to double jump slightly after the first jump and whenever you're on the ground your jump count gets reset so you're able to double jump again.