Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I have a PacMan clone I am making in XNA 4.0 that is tile-based. Everything is drawing in the correct position - except PacMan. It seems as if he is offset by 16 pixels on both x and y, but I don't know why.

Here is where I draw him:
/// <summary>
        /// Draws PacMan.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch to draw with.</param>
        /// <param name="tileWidth">The width of a tile.</param>
        /// <param name="tileHeight">The height of a tile.</param>
        public void Draw(SpriteBatch spriteBatch, int tileWidth, int tileHeight)
        {
            // Draw PacMan.
            if (openMouth)
            {
                spriteBatch.Draw(pacMan1, new Rectangle(PosX * tileWidth, PosY * tileHeight,
                    tileWidth, tileHeight), null,
                    Color.White, MathHelper.ToRadians(Rotation), new Vector2(tileWidth / 2, tileHeight / 2),
                    SpriteEffects.None, 0f);
            }

            else
            {
                spriteBatch.Draw(pacMan2, new Rectangle(PosX * tileWidth, PosY * tileHeight,
                    tileWidth, tileHeight), null,
                    Color.White, MathHelper.ToRadians(Rotation), new Vector2(tileWidth / 2, tileHeight / 2),
                    SpriteEffects.None, 0f);
            }
        }


tileWidth and tileHeight are used for drawing the ghosts, and they show up fine, so it can't be them.

I tried posting this on App Hub,, but I can't since I'm only 13.

Thanks,
ge-force
Posted
Comments
phil.o 9-Dec-11 15:41pm    
Seeing the Draw method for ghost object could possibly help.

Also, how are constructed your pacMan1 and pacMan2 variables ? What type ? Could it be possible that the problem sits in there ?
LanFanNinja 9-Dec-11 18:11pm    
Check my solution

1 solution

Well you are setting the origin of your pacman texture to half the textures width and height (the center of the texture) and this would offset your texture position 16 pixels on the x and y if your texture is 32x32 pixels.

If my mind is working correctly today I think you will need to add the origin to your position when drawing the texture.
e.g.
C#
Vector2 origin = new Vector2(tileWidth / 2, tileHeight / 2);

then where you pass the position to the draw method
C#
new Rectangle((PosX * tileWidth) + (int)origin.X, (PosY * tileHeight) + (int)origin.Y, tileWidth, tileHeight)


hope this helps
 
Share this answer
 
v3
Comments
ge-force 9-Dec-11 21:17pm    
Thank you SO much LanFanNinja! That did the trick.

And I checked into your suggestion phil.o, and that wasn't it, but good suggestion.
I am considering doing an article soon about PacMan XNA, so this really helped.

Thanks!
LanFanNinja 9-Dec-11 21:20pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900