Dragging, dropping, and sliding
If you're writing a game for a platform that has either touch or mouse support, then offering the ability for players to drag, drop, and slide elements around the screen can be a surprisingly compelling motivation to play in and of itself.
Getting ready
This recipe requires a selection of 100 by 100 pixel images to form both the background areas and the foreground draggable items. In my example, I created a blank, white texture named "block" for the background and a series of simple, white-lettered textures for the draggable item textures, as shown in the following illustration:
How to do it...
To start dragging and dropping items in your own games:
1. Begin by adding a new class to hold the state of what has been dropped onto the board and where:
class Map { private Dictionary<Point, Icon> iconByPosition = new Dictionary<Point, Icon>(); public bool TryGetIcon(Point position, out Icon icon) { return iconByPosition.TryGetValue(position, out icon); ...