Visible only when in use and origin of object centers with view of the player?

I have a player that uses ←↑↓→ arrows and a cursor that uses ASDW.

The cursor is persistent but does not come into play very often, so I would like to hide it by making it invisible when none of the ASDW keys are actively being pressed. I don't want to destroy/create it each time because I plan to have it hold information the player can use throughout the level even when they aren't using the cursor.

How would I make it visible only when active and have it "return" to a relative point inside the player's view when invisible?

Here's my basic movement so far for the cursor.

create:

GML:
movespeed = 2;
step:

GML:
//inputs
c_left =  keyboard_check(ord("A"))
c_right = keyboard_check(ord("D"))
c_down = keyboard_check(ord("S"))
c_up = keyboard_check(ord("W"))

///movement
hspeeds = (c_right - c_left) * movespeed;
vspeeds = (c_down - c_up) * movespeed;
//move cursor
x += hspeeds;
y += vspeeds;
draw:

GML:
draw_sprite(sp_cusor,0,x,y,)
The player's view is view[0].
 

Simon Gust

Member
You can define a variable that reacts to any of the c_ variables being 1.
There are some ways to do this
GML:
var any_input = c_left + c_right + c_down + c_up;
if (any_input > 0)
{
  ///movement
  hspeeds = (c_right - c_left) * movespeed;
  vspeeds = (c_down - c_up) * movespeed;

  //move cursor
  x += hspeeds;
  y += vspeeds;

  visible = true;
}
else
{
  x = camera_get_view_x(view_camera[0]) + camera_get_view_width(view_camera[0]) / 2;
  y = camera_get_view_y(view_camera[0]) + camera_get_view_height(view_camera[0]) / 2;

  visible = false;
}
or if you use GM:S 1.4
GML:
var any_input = c_left + c_right + c_down + c_up;
if (any_input > 0)
{
  ///movement
  hspeeds = (c_right - c_left) * movespeed;
  vspeeds = (c_down - c_up) * movespeed;

  //move cursor
  x += hspeeds;
  y += vspeeds;

  visible = true;
}
else
{
  x = view_xview[0] + view_wview[0] / 2;
  y = view_yview[0] + view_hview[0] / 2;

  visible = false;
}
visible being the built-in variable every object has. It toggles the execution of the draw event.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Use an alarm... So, in the STEP event, you'd add this:
GML:
if x != xprevious || y != yprevious
{
visible = true;
alarm[0] = room_speed * 3;
}
Now, in the alarm[0] event add this:
GML:
visible = false;
So, while the cursor moves, it will be visible and the alarm will be constantly reset. If it doesn't move, then the alarm will count down, and 3 seconds after the last movement it will go invisible. :)
 
Top