1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Instance Creation via Mouse Press

Discussion in 'Programming' started by Jordan Allred, May 16, 2019 at 11:28 PM.

  1. Jordan Allred

    Jordan Allred Member

    Joined:
    Thursday
    Posts:
    4
    I seem to be having an issue when it comes to creating instances only when my mouse is pressed. I want instance to be created when I hold down my left mouse button, and to be destroyed when I let go. Any solutions?
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,788
    What does your code look like so far?

    With such a general question there's really no way to give a good answer. Without knowing anything more I would put a global left release event in the object that is being created and put instance_destroy() in it.
     
  3. Jordan Allred

    Jordan Allred Member

    Joined:
    Thursday
    Posts:
    4
    Currently I am trying to make a gun appear when holding down the mouse, and disappear with the mouse is released. Here is what I got so far.
    //Left Pressed
    x = navy_player.x;
    y = navy_player.y;
    instance_create_layer(x,y,"Gun",pistol)

    //Left Released
    instance_destroy();
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,788
    Which object(s) is that code in?
     
  5. Jordan Allred

    Jordan Allred Member

    Joined:
    Thursday
    Posts:
    4
    The code is place on the gun itself.
     
  6. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,788
    I think if you thought about that logically for a bit you would understand why that wouldn't work.Specifically how can code run when it doesn't exist? If you change left release to a global left release (which maybe it already is) and you put the code that creates the gun in another object that does it exist, what you wrote would probably work (assuming no other code is interfering).
     
    EvanSki likes this.
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,960
    Events can't run until there is an instance of the object in the room. You're trying to get an instance_create_layer() line in the gun to run before any instance of a gun gets to exist. Move that piece of code to something else that will already exist in the room, for example the player object.

    Also, the mouse events that read "x Pressed", "x Released" and "x Down" only run when the mouse is over an instance of the object. To respond to clicks that occur outside the collision box, you need to use the ones that start with "Global".

    Topics like this demonstrate exactly why I despise current tutorial authors for churning out students that know next to nothing about events. It then ends up being the job of Q&A responders like me to clean after their negligence. Event ignorance needs to become an unacceptable learning outcome again.
     
    EvanSki likes this.
  8. Jordan Allred

    Jordan Allred Member

    Joined:
    Thursday
    Posts:
    4
    Thank you for the help. I got it to work now.
     
  9. Ido-f

    Ido-f Member

    Joined:
    Feb 19, 2018
    Posts:
    97
    I've read your post again, nvm...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice