Jump to content


Photo

Mouse Questions


  • Please log in to reply
3 replies to this topic

#1 tommyroyall

tommyroyall

    GMC Member

  • New Member
  • 29 posts
  • Version:GM8

Posted 09 June 2012 - 09:34 PM

Hey guys, I have been making a game, and I want it to place an object where-ever the mouse is when it clicks, but how would I do that? Right now all that I have done is create a cursor sprite as crosshairs and set that. Could you please help me with an example or a link? Thanks :D - Sledger721.
  • 0

#2 TsukaYuriko

TsukaYuriko

    GMC Member

  • GMC Member
  • 510 posts
  • Version:GM:Studio

Posted 09 June 2012 - 09:47 PM

To do something when the left mouse button has been pressed, you can use
if (mouse_check_button_pressed(mb_left))
If you need codes for any other mouse buttons, I guess you can figure them out easily. :) (Otherwise, just search the GM manual for "mouse")

Use the two variables mouse_x and mouse_y when setting the position of the instance to be created. This will create it at the mouse's position.
  • 0

#3 tommyroyall

tommyroyall

    GMC Member

  • New Member
  • 29 posts
  • Version:GM8

Posted 09 June 2012 - 11:05 PM

Ok now, but what if I have an object, and I want it to shoot in what-ever direction I click in? So that it has a full 360 degree spectrum of firing, and it fires towards wherever the mouse had clicked? And Thx TsukaYuriko :D.
  • 0

#4 TsukaYuriko

TsukaYuriko

    GMC Member

  • GMC Member
  • 510 posts
  • Version:GM:Studio

Posted 09 June 2012 - 11:19 PM

I assume you're going to make the object move by using GM's built-in speed and direction variables.

If you have an object which shoots things, e.g. a cannon, you can make its shots travel to the mouse's position using this:
var ID;
ID = instance_create(x, y, obj_shot); //or however your shot instances are created. Make sure you add the "ID =" part, though!

ID.direction = point_direction(x, y, mouse_x, mouse_y); //using a dot after ID will refer to ID's variables (the variables of the shot) instead of my the cannon's.
ID.speed = whatever speed you want the shot to travel at;

Edited by TsukaYuriko, 09 June 2012 - 11:20 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users