XNA 4.0: Moving PinkGirl around using the mouse

Share this post, pretty please! :

You might be thinking: “How do I move my game characters around?”

Let’s talk about moving characters, I think about movement in the following manner:

  1. Your game program needs to move the characters/actors/objects
  2. The player needs to move the character/actors/objects
  3. Need to test for the edge of the screen

Movement can be implemented in the Update method, but that gets messy real quick if you are moving even a few characters. Usually you would create a character and then provide a process of randomizing the movement or use an Artificial Intelligence. Or the user will use a multi-touch, mouse, gamepad, or keyboard to move the characters. Finally you will need to test for the edge of the screen.

I use an image rectangle when moving the object with the mouse, you can use vectors if you wish.

Here I show a code snippet that moves PinkGirl around on the screen from a previous post.

To keep it simple, the code upon initialization move PinkGirl to the top of the screen.

Why? The mouse initialization places it at the top of the screen. You can add code to fix it, but you should be able to see PinkGirl right at the top and pull the image down using your mouse.

From the post: https://bit.ly/pinkgirlepisode1, I included a mouse input (that post also has the PinkGirl, PlanetBoy and other images attached) :

//Start Code

private void HandleInput()
{
MouseState currentMouse =

Mouse.GetState();

r_PinkGirl.X = currentMouse.X;

r_PinkGirl.Y = currentMouse.Y;

}

//End Code

PhonePinkGirl