Skip to content

Events and Actions

In this chapter, we will delve into the fundamental concepts of events and actions in Game Maker Studio. Events are triggers that cause specific actions to occur in your game. By using events and actions, you can control the behavior of game objects and create interactive gameplay. We will focus on setting up movement for the Pac-Man character, handling collisions with power circles, and stopping movement upon hitting the maze walls.

Events and Actions Overview

Events in Game Maker Studio are essentially conditions that trigger specific behaviors or actions in your game. Actions are the tasks executed when an event occurs. Together, they form the basis of game logic, allowing you to control how your game reacts to player input and other in-game events.

Common Events

  1. Create Event: Triggered when an object is created. Used to initialize variables or set up the object’s properties.
  2. Destroy Event: Triggered when an object is destroyed. Used to clean up or trigger specific actions when an object is removed.
  3. Step Event: Triggered every frame. Used to update the object’s properties, such as position or state, continuously.
  4. Key Down Event: Triggered when a specific key is pressed and held down. Used to control actions such as movement.
  5. Key Up Event: Triggered when a specific key is released. Used to stop actions that were initiated by a Key Down event.
  6. Mouse Event: Triggered by mouse actions, such as clicking or moving the mouse. Used to interact with objects via the mouse.
  7. Collision Event: Triggered when two objects collide. Used to handle interactions between objects, such as bouncing off walls or collecting items.
  8. Alarm Event: Triggered after a set amount of time. Used for timed actions, such as spawning enemies or triggering events after a delay.
  9. Draw Event: Triggered when an object needs to be drawn on the screen. Used to control how objects are rendered.

Common Actions

  1. Set Speed: Adjusts the speed of an object. Used to control movement speed.
  2. Set Direction: Sets the direction in which an object moves. Used to change the direction of movement.
  3. Destroy Instance: Removes an object from the game. Used to delete objects when no longer needed.
  4. Create Instance: Creates a new instance of an object. Used to spawn new objects, such as enemies or projectiles.
  5. Play Sound: Plays a sound effect. Used to add audio feedback to actions.
  6. Set Alarm: Sets an alarm to trigger after a specified number of steps. Used to execute actions after a delay.

Combining Events and Actions

By combining these events and actions, you can create complex behaviors and interactions in your game. For example:

  • Moving an Object:
    • Use the Key Down event with the Set Speed and Set Direction actions to move an object in response to player input.
  • Stopping Movement:
    • Use the Key Up event with the Set Speed action to stop the object’s movement when the key is released.
  • Handling Collisions:
    • Use the Collision event with the Destroy Instance action to remove objects when they collide, such as collecting items or destroying enemies.

Understanding and utilizing these events and actions effectively will allow you to build engaging and interactive games. In the next sections, we will provide step-by-step instructions on setting up these events and actions in your game projects.

Key Down Event for Pac-Man

The Key Down event is triggered when a specific key is pressed and held down. This event is essential for controlling Pac-Man’s movement. When setting up events and actions in Game Maker Studio, we will use GML Visual, a visual scripting method that simplifies the process for beginners.

Setting Up Key Down Events

  1. Open the Object Editor for Pac-Man:

    • In the Resource Tree, find obj_pacman and double-click to open the Object Editor.
  2. Add Key Down Events:

    • Click on “Add Event” and select “Key Down” from the list.
    • Choose the directional keys: Up, Down, Left, and Right. You will need to repeat this process for each key.

Adding Actions to Key Down Events

Set Speed and Set Fixed Direction

For each Key Down event (Up, Down, Left, and Right), we will add actions to control Pac-Man’s speed and direction.

  1. Add Set Speed Action:

    • In the Key Down event for “Up”, click on “Add Action” and select “Set Speed” from the actions list.
    • Set the speed value to 1. This value determines how fast Pac-Man will move.
  2. Add Set Fixed Direction Action:

    • Still in the Key Down event for “Up”, click on “Add Action” again and select “Set Fixed Direction”.
    • Choose the direction corresponding to the Up key. For example, set the direction to 90 degrees (upwards).
    • Repeat these steps for the Down, Left, and Right Key Down events, setting the appropriate directions:
      • Up: Direction = 90 degrees
      • Down: Direction = 270 degrees
      • Left: Direction = 180 degrees
      • Right: Direction = 0 degrees

Key Up Event for Stopping Movement

The Key Up event is triggered when a specific key is released. This event is used to stop Pac-Man’s movement when the directional keys are no longer pressed.

  1. Add Key Up Events:

    • Similar to the Key Down events, click on “Add Event” and select “Key Up”.
    • Choose the directional keys: Up, Down, Left, and Right.
  2. Add Set Fixed Direction Action:

    • For each Key Up event, click on “Add Action” and select “Set Fixed Direction”.
    • Set the direction to “No Direction” to stop Pac-Man from moving. This is done by setting the speed to 0.

Handling Collisions

Collisions are interactions between objects in the game. Game Maker Studio provides robust tools to handle collisions and define what happens when objects interact with each other. We will set up collision events for Pac-Man to interact with power circles and maze walls.

How Collisions Work

In Game Maker Studio, collision events are used to detect when two objects intersect or come into contact. When a collision event occurs, you can specify actions that should be taken, such as stopping movement, destroying an object, or triggering another event. Collision detection is essential for creating interactive and responsive gameplay.

Collision with Power Circles

  1. Add Collision Event:

    • In the Object Editor for obj_pacman, click on “Add Event” and select “Collision”.
    • Choose obj_powercircle from the list of objects.
  2. Add Destroy Instance Action:

    • In the Collision event with obj_powercircle, click on “Add Action” and select “Destroy Instance”.
    • This action will remove the power circle from the game when Pac-Man collides with it.
  3. Update Score:

    • In the same Collision event with obj_powercircle, click on “Add Action” again.
    • Select the “Variables” category and choose “Assign Variable”.
    • Set the variable to score, the operation to add, and the value to 10. This will increase the score by 10 points each time Pac-Man collects a power circle.

Collision with Maze Walls

  1. Add Collision Event:

    • In the Object Editor for obj_pacman, click on “Add Event” and select “Collision”.
    • Choose obj_maze from the list of objects.
  2. Add Set Fixed Direction Action:

    • In the Collision event with obj_maze, click on “Add Action” and select “Set Fixed Direction”.
    • Set the speed to 0 to stop Pac-Man’s movement upon colliding with the maze walls.

Keeping Track of the Score

To create a more engaging gameplay experience, we need to keep track of the player’s score. We will use the Draw GUI event and the Draw Value action in GML Visual to display the score on the screen. Additionally, we’ll use the Assign Variable action to manage and update the score.

What is a Variable?

A variable is a storage location identified by a name that holds a value, which can be changed during the execution of a program. In game development, variables are used to store information such as the player’s score, health, and other game states. Variables can be numbers, strings, or other data types.

What is Draw GUI?

The Draw GUI event is used to draw elements on the screen that remain fixed in position, regardless of the camera’s movements or the player’s position in the game. GUI stands for Graphical User Interface, and the Draw GUI event is typically used to display HUD (Heads-Up Display) elements such as scores, health bars, and other information.

What is Draw Value?

The Draw Value action is used within the Draw GUI event to display a variable’s value on the screen. This action is perfect for showing numerical values like the player’s score. The x and y values determine the position on the screen where the value will be displayed. The x value represents the horizontal position, and the y value represents the vertical position.

What is Assign Variable?

The Assign Variable action allows you to create, update, or modify the value of a variable. This is useful for keeping track of various game states, such as the player’s score, health, or other attributes.

Setting Up the Score Variable

  1. Initialize the Score Variable:
    • Open the Object Editor for obj_pacman.
    • Add

a Create Event by clicking “Add Event” and selecting “Create”.

  • Click “Add Action”, select the “Variables” category, and choose “Assign Variable”.
  • Name the variable score and set its initial value to 0. This ensures that the score starts at 0 when the game begins.

Updating the Score on Collision with Power Circles

  1. Update Score in Collision Event:
    • In the Collision event with obj_powercircle, click “Add Action”.
    • Select the “Variables” category and choose “Assign Variable”.
    • Set the variable to score, the operation to add, and the value to 10. This will increase the score by 10 points each time Pac-Man collects a power circle.
    • This is done by setting the variable score to score + 10.

Displaying the Score

  1. Add Draw GUI Event:

    • In the Object Editor for obj_pacman, click “Add Event” and select “Draw GUI”.
    • The Draw GUI event ensures that the score will be drawn on the screen in a fixed position, independent of the game world’s camera.
  2. Add Draw Value Action:

    • In the Draw GUI event, click “Add Action”.
    • Select the “Draw” category and choose “Draw Value”.
    • Set the value to score and specify the position on the screen where you want the score to be displayed. For example, set x = 10 and y = 10 to display the score near the top-left corner of the screen.
    • This action will display the current value of the score variable at the specified coordinates on the screen.

Detailed Steps with Visuals

Initializing the Score Variable

  1. Add Create Event:

    • In the Object Editor for obj_pacman, click on “Add Event” and select “Create”.
  2. Assign Variable Action:

    • In the Create event, click on “Add Action”.
    • Select the “Variables” category and choose “Assign Variable”.
    • In the action parameters, set:
      • Variable Name: score
      • Operation: Set
      • Value: 0
    • This initializes the score to 0 when the game starts.

Updating the Score on Collision with Power Circles

  1. Add Collision Event:

    • In the Object Editor for obj_pacman, click on “Add Event” and select “Collision”.
    • Choose obj_powercircle.
  2. Assign Variable Action:

    • In the Collision event, click on “Add Action”.
    • Select the “Variables” category and choose “Assign Variable”.
    • In the action parameters, set:
      • Variable Name: score
      • Operation: Add
      • Value: 10
    • This increases the score by 10 points whenever Pac-Man collides with a power circle.

Displaying the Score

  1. Add Draw GUI Event:

    • In the Object Editor for obj_pacman, click on “Add Event” and select “Draw GUI”.
  2. Draw Value Action:

    • In the Draw GUI event, click on “Add Action”.
    • Select the “Draw” category and choose “Draw Value”.
    • In the action parameters, set:
      • Value: score
      • x: 10
      • y: 10
    • This action will display the current value of the score variable at coordinates (10, 10) on the screen.

Testing Your Game

Now that you have set up the events, actions, and score tracking, it’s time to test your game:

  1. Compile and Run:

    • Click the green “Play” button in the toolbar to compile your game.
    • The game will open in a new window, allowing you to test the controls, interactions, and score display.
  2. Evaluate and Adjust:

    • Play through the game to ensure that Pac-Man moves correctly, stops upon key release, and interacts with power circles and maze walls as expected.
    • Verify that the score updates and displays correctly when Pac-Man collects power circles.
    • Make any necessary adjustments in the Object Editor and re-test until you are satisfied with the results.

By following these detailed steps, you will have created a functional and interactive Pac-Man character that responds to player input, interacts with the game environment, and keeps track of the player’s score. This foundational knowledge of events and actions will enable you to create more complex and engaging gameplay experiences in the future.

In the next chapter, we will explore advanced events and actions, such as handling ghost movements and adding game logic for scoring and lives. Continue experimenting with events and actions to see how they influence your game. Happy game developing!