components used: TDXInput TTimer

delphix does NOT work with borland kylix

In this tutorial we'll learn how to make use of the keyboard to move around our character, with the help of the DelphiX components. If you don't already have these components then the links on the left will allow you to download the latest version. Once you've got DelphiX running you can now start to work on this tutorial. The specific component we'll be dealing with is the DXInput component as this allows us to deal with keyboard (and indeed joystick control).

So, to begin with we need to load up our code from tutorial three. Basically, we need to get rid of our buttons on the form, while keeping our original code from the BtnClick procedures and if you follow these steps you should achieve just that. Firstly, we need to change the first line of each of our BtnClick procedures to essential make them no longer connected to the buttons as shown below.

procedure TForm1.LeftBtnClick(Sender: TObject);

should become:

procedure TForm1.Left

procedure TForm1.UpBtnClick(Sender: TObject);

should become:

procedure TForm1.Up;

procedure TForm1.RightBtnClick(Sender: TObject);

should become:

procedure TForm1.Right;

procedure TForm1.DownBtnClick(Sender: TObject);

should become:

procedure TForm1.Down;

Now we need to delete the four direction buttons on the form. Once that's done, we need to finally delete all record of the buttons having been there by deleting the following lines in the type section:

procedure LeftBtnClick(Sender: TObject);
procedure UpBtnClick(Sender: TObject);
procedure RightBtnClick(Sender: TObject);
procedure DownBtnClick(Sender: TObject);

Before we can use our Left, Right, Up and Down procedures, we'll need to put the following lines in to declare them again, in the private section just before any other procedures you may have declared from previous tutorials.

procedure Left;
procedure Right;
procedure Up;
procedure Down;

We're now ready to make use of the DXInput component. Start by double-clicking on its icon to create the component on the form. If you're unsure which component it is, refer to the list of components used to the left of this page. As well as this component, we're going to need the Timer component which can be found on the System tab of the toolbar, as a picture of a clock. Again, double-click on it to create it on the form. We'll use this to keep checking in regular time intervals, if one of the cursor keys on the keyboard has been pressed. You'll need to change the default Interval of the Timer component from 1000 to something around 50 so that the computer checks frequently enough if a key has been pressed. You may want to increase this number a little if the keys seem a little over 'sensitive' when you run the program later. Now we need to create the code for our DXInput component. All the code will be placed in our Timer procedure so double-click on the Timer on the form.

After the line begin you need to enter the following lines:

DXInput1.Update;
if isLeft in DXInput1.States then Left;
if isRight in DXInput1.States then Right;
if isUp in DXInput1.States then Up;
if isDown in DXInput1.States then Down;

This simply tells the program to check for any updates from the keys and if so, when the left cursor key has been pressed run the Left procedure, if the right cursor key has been pressed, run the Right procedure and so on. You can now run the program and control the character (or characters if you completed the last tutorial), with the use of the keyboard.

Finally, if you've got a joystick attached and set up to your PC, you should find that when you move the joystick up, down, left and right your character(s) will move this way as well.

In the next tutorial we'll continue looking at the various DelphiX components and how they are useful to us. In the meantime, as always, if you have any problems, questions or suggestions for tutorials, e-mail me at ben@delphigamedev.com

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

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