Day 8 – Tilemaps, Collisions and a Partner

Here’s the latest version of Run and Gun:
http://www.mediafire.com/?ai2sdw32ve1aiw1

There have been a few occasions where I wanted to smash my computer and other objects around me out of anger. First when I was trying to do collision detections with a dynamic boundary rectangle. I was trying to use one rectangle for collision detection along with drawing co-ordinates. It didn’t work because when changing from an Idle animation to a shooting animation, the rectangle increases in size, and this makes the player no longer standing on the platform, causing them to fall. I fixed this by having a static collision rectangle and a dynamic draw rectangle.

Now that is what I call a PLATFORMER

Then there was the bullet positioning. It was pretty basic 2D geometry. Get the distance from the center of the sprite, to the position of the muzzle of the gun on the sprite sheet. Add this to the screen position of the center of the character. It worked well when the character was facing right. It was off by 10 pixels when he was facing left. I couldn’t work it out. I checked my code over and over again for at least three hours. I rewrote it different ways. I referenced the center of the sprite, the left margin, the right margin. It was always the same.

Finally I gave up and just added an amount to the position until it looked correct. It looked best when adding ten pixels. Ten pixels. The width of the bullet.

"I'll clean this ugly code up later". This code will never ever change.

The code basically figures out where to position the bullet. It will have to change eventually, because I hard-coded in a scale of 2. I’ll be really surprised if all the sprites in this game are scaled up by a factor of two. Also this code only works for the main character. I’ll have to change it for when bad guys want to shoot bullets. Another problem is it isn’t very efficient.

Each state that can fire a bullet has a Point property, that defines where on the sprite sheet the bullet is. The code then calculates the distance between the centre of the sprite and the bullet pos. This value does not change. So each state could instead store the distance from the center of the sprite to the bullet, and there would be a lot less calculations going on.

I’ll code it later though. Also I implemented tile maps with the help of legendary programmer JONATHAN TREE. I’ll write about those later. I also implemented them in quite an ugly way.