© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_9

9. Mouse

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 
Mouse input is great for a player to interact with in your game. When done well, the player can seamlessly play your game. Mouse events can be detected using code or Mouse Events. You will use mouse interaction for such things as
  • Interacting with menu buttons.

  • Setting a target location for a player.

  • Making a gun turret point in the mouse's direction.

  • Changing weapons.

  • Showing info on mouse over.

  • Moving a gun sight around the room.

As with keypresses, there are three main button press types:
if mouse_check_button(mb_left)
{
      //do something
}
trigger while held down
if mouse_check_button_pressed(mb_left)
{
      //do something
}
trigger once when pressed
if mouse_check_button_released(mb_left)
{
      //do something
}

trigger once when released

The mouse position (in a Desktop export) is updated at the frame rate (default of 60 frames per second).

mouse_x holds the current x position.

mouse_y holds the current y position.

Both of the preceding mouse variables are global in nature, and can be accessed from any instance within your game.

There are various mouse button checks that can be made, using for example:
if mouse_check_button(button)
{
//do something
}
where button can be any of the following:
  • mb_left

  • mb_right

  • mb_middle

  • mb_none

  • mb_any

mb_middle means middle button

mb_none means no button

mb_any means any mouse button

Mouse interaction can also be detected and acted upon using Mouse Events, though you will have more flexibility using code.

The following is an example:
if mouse_check_button(mb_left)
{
      movement_speed=10; //How quickly it moves
      target_x=mouse_x; //Where to move to on x
      target_y=mouse_y; //Where to move to on y
      x+=(target_x-x)/ movement_speed; //target position-current
      y+=(target_y-y)/ movement_speed; //target position-current
}

This will make the instance move slowly to the mouse's position if the left button is being held down.

You can also detect scrolling of the middle mouse wheel:
if mouse_wheel_up()
{
      weapon+=1;
}

which will Increase the value of weapon by 1 when the mouse wheel is scrolled up.

For scrolling down:
if mouse_wheel_down()
{
      weapon-=1;
}

which decreases the value of weapon by 1 when the mouse wheel is scrolled down.

Cursors

You can also set the cursor to a sprite of your choice, where sprite_name is a sprite that exists.
cursor_sprite=sprite_name;

This will remain in place until to you set it to something else.

There are times you may want to hide the cursor from view, in that case you can use:
window_set_cursor(cr_none);

which sets the cursor to invisible – remember to set it back if it is needed.

There are also several built in cursor types that can be used to great effect, see Figure 9-1.

A table lists 15 cursor types with their constants. It begins with cr underscore none and ends with cr underscore size underscore all.

Figure 9-1

Showing built in cursor types

You can set one of these cursor types, for example with:
window_set_cursor(cr_arrow);
You can detect if the mouse cursor is over an instance, for example, the following in a Step Event:
if position_meeting(mouse_x,mouse_y,object_name)
{
      score++;
}

which increases the score if the cursor is over any instance of obj_name – replace with id to check if it is over itself.

Another example:
if position_meeting(mouse_x,mouse_y,id) &&
mouse_check_button_pressed(mb_left)
{
audio_play_sound(snd_bounce,1,false);
}

would check that the mouse is over itself and the left mouse button has just been clicked. A sound will then play.

Whether you use code or events, it is up to you and will change depending on what your game does.

Basic Projects

  1. A)

    Make an object that moves accordingly to mouse x position (not y position).

     
  2. B)

    Change the mouse's cursor when hovering over an object instance.

     
  3. C)

    Display the mouse's position as text on screen.

     

Advance Projects

  1. D)

    Set up various objects that each play a different sound when clicked.

     
  2. E)

    Make an object that can be clicked and dragged around the room with the mouse.

     

Useful Functions

You can recall the last button that was clicked:
if mouse_lastbutton=mb_middle
{
     y++;
}

Summary

You should be able to add basic mouse interaction in your own games, and allow player interaction to be acted upon.

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

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