• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Instance Creation via Mouse Press

J

Jordan Allred

Guest
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?
 

samspade

Member
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.
 
J

Jordan Allred

Guest
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();
 

samspade

Member
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();
Which object(s) is that code in?
 

samspade

Member
The code is place on the gun itself.
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).
 

FrostyCat

Redemption Seeker
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.
 

Ido-f

Member
I'd like to note that your code in the global left mouse down event would run for every frame that the left mouse button is being held down.
That means you'll create a new gun every frame, which could slow down the game and break the gameplay.

I suggest you try to make it work with the global left mouse pressed and global left mouse released events instead.
I've read your post again, nvm...
 
Top