components used: TDXDraw TDXImageList TDXSprite TDXTimer TDXInput

delphix does NOT work with borland kylix

download source code

This tutorial is not intended to be a complete guide to all things mice with DelphiX, but merely an introduction. It would probably wise to get yourself familiar with General Tutorial 13 first, which covers mice in greater detail, parts of which will be useful to you here. You will find yourself using the same commands for DelphiX, the only difference being that it requires some additional coding to get things working, and it's exactly this code that we will be covering below.

The best way to demonstrate this then, is to open up some previous code we've used and alter it. So, with that in mind let's fire up the code from tutorial four so that we can add the ability to 'blow up' our pacman sprite simply by clicking on him. You'll remember that currently when our pacman encounters the ghost he blows up anyway so this element of the code is already there, we just need to set up a way to make it happen when we click on pacman as well.

Just get on with it
Okay, okay. As some of you have discovered, using DelphiX's GetSpriteAt has limited uses. The best way to do what we want is to get rid of the normal mouse cursor and set up a sprite which represents our cursor and which will have its X and Y coordinates adjusted as we move the mouse. This means we can make use of our sprite's existing DoCollision code to see if the sprite that has collided with the sprite was the cursor. Pretty simple to do, and here's how. First we need to set up the sprite declaration, much as we've done when we previously set up sprites. So, near the beginning of your code, just after the declarations for the pacman, ghost and background sprites, you need to enter the following code.

TCursorSprite = Class(TImageSprite)

end;

Very simple. You'll notice that this is a lot shorter than our previous declarations and if you download the accompanying code you'll see I have commented out some lines that could go with this so that you have an DoMove and DoCollision procedure. In this demonstration we don't need either, so I chose not to include them, but you might want to use them yourself, in which case you can uncomment the code and create your own procedures to match.

So, as we've done in the past, to assign a variable to this new sprite class we need to add the following line in the var section of our code:

MouseCursor : TCursorSprite;

You now need to add the code below to your FormCreate procedure, to actually set up the details of our mouse cursor. This code assumes that there is an image in the ImageList called Cursor which will represent our mouse cursor, so add one now. An image of the cursor I drew is below, you can make one to suit your own tastes.


MouseCursor := TCursorSprite.Create (DXSpriteEngine1.Engine);

MouseCursor.Image := DXImageList1.Items.Find('Cursor');

MouseCursor.Z := 10;

This just creates our cursor sprite and assigns its image, making sure to alter the Z coordinate so that the cursor appears in front of the sprites, and not behind (try removing this last line to see what I mean).

Now we've done all that, we need to find a way to move the cursor sprite about whenever we move the mouse. This luckily, is very simple too. Select the DXDraw component's canvas already on your form, and create an OnMouseMove event. In this you need these two lines.

MouseCursor.X := X;
MouseCursor.Y := Y;

All we do is change the mouse cursor's X and Y coordinates to that of our mouse, every time it is moved. Before we deal with the mouse button presses, let's set up a new variable of Boolean type, called LeftBtnDown, adding it to the var section of our code with its chums in exactly the way we've done this kind of thing in the past - I won't insult your intelligence by showing the code for this, if you're stuck you should have paid better attention during previous tutorials! Now we've done that, add an OnMouseDown event with this code inside.

if Button = mbLeft then LeftBtnDown := True;

This checks to see if the left mouse button is being pressed (you'd use mbRight to check the right mouse button), and sets our LeftBtnDown Boolean to true. If you now add an OnMouseUp event with the following code, we'll be able to set our LeftBtnDown Boolean back to false every time the button is let go.

if Button = mbLeft then LeftBtnDown := False;

Don't point, it's rude...
So, how are we going to make use of this LeftBtnDown variable now that we have it responding to our user's button pressing? Well, we're going to alter our code in our TPacman.doCollison procedure code so that it looks like that shown below, allowing us to tell if our pacman sprite is being pointed at by the mouse cursor.

procedure TPacman.DoCollision(Sprite: TSprite; var Done: Boolean);
begin

 if Sprite is TGhost then
 begin
  // Nothing just now
 end;

 if
Sprite is TCursorSprite then
 begin

  //Also check that left mouse button is down
  if LeftBtnDown then
   begin

    // Set up the explosion
    Pacman.Image := Form1.DXImageList1.Items.Find('Explode');
    Pacman.Width := 28;
    Pacman.Height := Pacman.Image.Height;
    Pacman.AnimCount := 5;
    Pacman.AnimLooped := False;
    Pacman.AnimSpeed := 50/1000;
    Pacman.AnimStart := 0;
    PacmanDead := True;
   end;
 end;
end;

What we've done is taken our code for exploding pacman from the TGhost lines and placed it in our new 'if Sprite is TCursorSprite' code. We then also check to see if the left mouse button is down, by checking that LeftBtnDown Boolean, and if it is down, we blow him up.

As always, there's one more thing to be done, and that's to hide our 'proper' mouse cursor that we've normally seen during our previous tutorials. If you run the code now, you'll see two cursors: our sprite cursor and the proper Windows supplied cursor. Depending on how you've drawn your mouse cursor, you may see that your new sprite isn't quite in the right position, but that's easy to sort out, by adjusting the X and Y coordinates in your code. To get rid of this standard cursor though, you just need to add this line to your DXDraw1Initialize code:

Screen.Cursor := crNone;

So, everything's done now, and you can use the mouse in your DelphiX games. Happy days.

Everything we've covered in this tutorial has individually been very simple, and it should demonstrate to you that if you put together lots of simple ideas, you can achieve quite a lot. Good luck.

download source code

Subscribe to the feed delphigamedev.com NEWS Feed
archived news | mailing list

[ all tutorials found at delphigamedev.com are Copyright © 2008 Ben Watt ]