Controlling a sprite with the keyboard

Let's add some game logic to the game. Double-click on the screen in the first layout and add the keyboard and mouse objects, as shown in the following screenshot. This allows us to use the Keyboard and the Mouse in our game.

Controlling a sprite with the keyboard

Let's set up the game logic:

  1. We need to make the player move with the WASD control and rotate to the mouse location. Click on the player and add the behavior.
  2. Once you see this dialog box, click on the plus sign to add a behavior.
  3. Scroll down and add the 8Direction behavior:
    Controlling a sprite with the keyboard
  4. Once you have added the 8Direction behavior, it should appear just like the preceding screenshot. Exit this window and go to the layout.

What we want is for the player to use either the arrow keys or the WASD controls to move around. It's good to have both, as players have different preferences. The following are the steps to do so:

  1. First, we have to change the eight directions to four. You can have a game with the full eight directions, but four is a little easier to manage programmatically.
  2. Select the box next to 8 directions and change it to 4 directions, as shown in the following screenshot:
    Controlling a sprite with the keyboard
  3. We also need to make sure that the angle will be determined by the mouse and not the 8Direction behavior. From the Set angle dropdown list, select No as shown in the following screenshot:
    Controlling a sprite with the keyboard
  4. Before we move on, let's test the game. Press F5 and run the game. Make sure that the controls work. Once you are satisfied, go to the event sheet and add your WASD controls.
  5. Once you are in the event sheet, double-click on the background to add an event and add a Keyboard event.
  6. Once you are in the Keyboard event window, select Key is down.
  7. Once you have selected Key is down, select <click to choose> in the Parameters for Keyboard: Key is down window:
    Controlling a sprite with the keyboard
  8. Once you have selected <click to choose>, select the key you want. In this case, it will be the D key:
    Controlling a sprite with the keyboard
  9. Once you have selected the D key, the event sheet should look like this:
    Controlling a sprite with the keyboard
  10. Click on Add action and click on Player in the layout. Once you have clicked on Player, select Simulate control from the available options:
    Controlling a sprite with the keyboard
  11. This will simulate the control of the 8Direction behavior. We want to simulate the right control, so select the Right control as follows:
    Controlling a sprite with the keyboard
  12. After you do that, the event sheet should look like this:
    Controlling a sprite with the keyboard
  13. Copy-and-paste the keyboard layout and change the controls accordingly. In the end, the event sheet should look like this:
    Controlling a sprite with the keyboard

Test and run the game. If the player moves around correctly, then we can move on. Before we do this, however, there is one more thing that needs to be addressed—restricting the player's position. The player can move off of the screen. Go to the layout and select the Bound to layout behavior.

Test the game again and see whether it works. It is really important to test your game; often in game development, the sooner you catch a problem, the better chance you have of fixing it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.40