VG:Using Mouse to Select Different Blocks

From MyProjectFun
Jump to: navigation, search

Student: In my game, there will be many different kinds of blocks. I want to be able to select with my mouse cursor a single block to control. Then I want to be able to select any single one of the other blocks, regardless of type or name, to control. When I choose something new to control, I want to stop controlling the previous block.

Any help or ideas on how to do this would be great.

EDIT: If there is a way to drag and drop my blocks, that may be another possible solution to my problems.

Instructor: I thought I had this figured out, but that idea wasn't going to work. I'll need to do some research, which I'll get to in a moment. Please stand by...

Success! Functionality for click and drag

First, attach this function to your blocks:

/// <summary>
/// Allow a sprite to be clicked and dragged by the cursor
/// </summary>
public static void ClickedOnFN()
{
  // Find the cursor that's colliding with me
  Sprite Cursor = This.Sprite.CollisionWithSprite("Target" );
 
  // If I collide with the cursor and the player triggers left click
  if (Cursor != null && This.Game.IsMouseTriggered(0) && This.Sprite.DisplayList == 0)
  {
    // Activate Hflip for debugging
    This.Sprite.HFlip = !This.Sprite.HFlip;
 
    // Make a temporary variable to hold the cursor's display list
    int temp = Cursor.DisplayList;
 
    // Change the cursor's display list to my display list
    Cursor.DisplayList = This.Sprite.DisplayList;
 
    // Change my display list to the cursor's old display list
    This.Sprite.DisplayList = temp;
  }
  // Else if I collide with the cursor and the player presses left click
  else if (Cursor != null && This.Game.IsMousePressed(0) && This.Sprite.DisplayList != 0)
  {
    // Set my position to that of the cursor
    This.Sprite.Position = new Point2D(Cursor.Position);
  }
  // Else
  else
  {
    // Reset my display list
    This.Sprite.DisplayList = 0;
  }
}

Next, attach this function to your mouse cursor:

/// <summary>
/// Resets the mouse's display list
/// </summary>
public static void MouseResetDisplayListFN()
{
  // If the player is not clicking
  if ((This.Game.IsMouseTriggered(0) || This.Game.IsMousePressed(0)) == false)
  {
    // Reset the display list
    This.Sprite.DisplayList = 1;
  }
}

Note that my cursor was named Target, so it might need to be modified to work with your cursor sprite.

Personal tools
Namespaces
Variants
Actions
Navigation
Engineering
Game Design
Multimedia Production
VG Programming
Downloads
Toolbox