Game Development Reference
In-Depth Information
'
notion of legality; it
s not always possible that anything in your game can be dragged
to anywhere. If a drag fails, you ' ll need a way to set things back to the way they were.
This issue might seem moot when you consider that dragging usually affects the look
of the game
the dragged object needs to appear like it is really moving around, and
it shouldn
t leave a copy of itself in its original location. That might confuse the
player big-time.
The code to support dragging requires three phases:
'
n Detect and initiate a drag event.
n Handle the mouse movement and draw objects accordingly.
n Detect the release and finalize the drag.
The actions that define a drag are typically a mouse press (button down) followed by
a mouse movement, but life in the mouse drag game is not always that simple. Also,
during a double-click event, a slight amount of mouse movement might occur, per-
haps only a single pixel coordinate. Your code must interpret these different cases.
In Windows, a drag event is only allowed on objects that are already selected, which
is why drags usually follow on the second
of the mouse button. The
first click of the left mouse button always selects objects. Many games differ from
that standard, but it
click and hold
'
s one of the easier actions to code since only selected objects are
draggable.
Since a drag event involves multiple trips around the main loop, you must assume
that every mouse button down event could be the beginning of a drag event. I guess
an event is assumed draggable until proven innocent. In your mouse button down
handler, you need to look at the mouse coordinates and determine if they are over
a draggable object. If the object is draggable, you must create a temporary reference
to it that you can find a few game loops later. Since this is the first button down
event, you can
s a bona fide drag event just yet.
The only thing that will make the drag event real is the movement of the mouse, but
only movement outside of a tiny buffer zone. On most screen resolutions, a good
choice is five pixels in either the X or Y coordinate. This is large enough to indicate
that the drag was real, but small enough that small shakes in the mouse during a
double-click won ' t unintentionally initiate a drag. If you were to create a drag on a
Wii game, you
'
t tell if it
'
d want a much sloppier buffer zone since the Wii Remote pointer can
shake quite a bit. If you can set this buffer size while the game is running, like with a
hack or a cheat, you
'
'
ll be able to tune this to suit a majority of players quickly.
Search WWH ::




Custom Search