• 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 Find the Button then display text that door is now unlocked (GML)

J

JulianW

Guest
So I'm doing an assignment for Uni and we have been told to make a simple mechanic in GMS2.
My mechanic idea is a simple stupid idea of find the button.
I've made the map/background, the player, lighting and a button.
I've also made a sprite with text in saying door is now unlocked.
What I want to be able to do is click the button then the button disappears and the text appears.

This is what I have so far for the button script.
Left Pressed:

buttonClicked = false;

d = point_distance(mouse_x, mouse_y, self.x + (self.sprite_width/2), self.y + (self.sprite_height)/2)
if (d < self.sprite_width/2) {
instance_destroy()
buttonClicked = true;

}

if (buttonClicked==true){
draw_sprite(spr_DoorIsUnlocked, 0, 490, 380);
}

The button disappears but the sprite with text on doesn't appear? any advice on that?

Here's a short video of what's going on.

I also want to add some sort of if you click the mouse somewhere then a ring will appear and the player will move to that area. I've got the click and move bit done just not able to get a ring to appear.
 

PlayerOne

Member
If I'm reading this correctly, the reason button text will not appear is because you destroyed the button object where the sprite is suppose to be drawn. In this instance you need to draw the spr_doorisunlocked in a seperate object and call a global to equal true when the button is pressed.
 
J

JulianW

Guest
If I'm reading this correctly, the reason button text will not appear is because you destroyed the button object where the sprite is suppose to be drawn. In this instance you need to draw the spr_doorisunlocked in a seperate object and call a global to equal true when the button is pressed.
Okay, So I made a separate object and placed the spr_doorisunlocked in it. I then created draw event and put this:
global.buttonClicked = false;

//buttonClicked = false;

if (global.buttonClicked = true){
draw_self();
}


but nothing is happening. I tested it to see if it draws and it does. it just doesn't respond to the button being clicked?

obj_button:
Left Pressed:
globalvar buttonClicked;

buttonClicked = false;

d = point_distance(mouse_x, mouse_y, self.x + (self.sprite_width/2), self.y + (self.sprite_height)/2)
if (d < self.sprite_width/2) {
instance_destroy()
global.buttonClicked = true;

}

Am I doing something wrong there?
 

PlayerOne

Member
Okay, So I made a separate object and placed the spr_doorisunlocked in it. I then created draw event and put this:
global.buttonClicked = false;

//buttonClicked = false;

if (global.buttonClicked = true){
draw_self();
}


but nothing is happening. I tested it to see if it draws and it does. it just doesn't respond to the button being clicked?

obj_button:
Left Pressed:
globalvar buttonClicked;

buttonClicked = false;

d = point_distance(mouse_x, mouse_y, self.x + (self.sprite_width/2), self.y + (self.sprite_height)/2)
if (d < self.sprite_width/2) {
instance_destroy()
global.buttonClicked = true;

}

Am I doing something wrong there?

Close. Few things to note:
1) Do not use globalvar. global.buttonclicked is enough when declared in a seperate object in the room.
2) Unless it is a special sprite for that text, drawing it using the draw_text function is enough.

Here let me fix this and get back to me if it doesn't work.

Code:
//Obj_Text

//CREATE
global.buttonclicked=false;


//DRAW GUI

if (global.buttonclicked == true)
{
draw_text(100,100,"Door is unlocked")
}
Code:
//obj_button:

Left Pressed:
d = point_distance(mouse_x, mouse_y, self.x + (self.sprite_width/2), self.y + (self.sprite_height)/2)
if (d < self.sprite_width/2)
{
global.buttonclicked = true;
instance_destroy()
}
 
J

JulianW

Guest
Close. Few things to note:
1) Do not use globalvar. global.buttonclicked is enough when declared in a seperate object in the room.
2) Unless it is a special sprite for that text, drawing it using the draw_text function is enough.

Here let me fix this and get back to me if it doesn't work.

Code:
//Obj_Text

//CREATE
global.buttonclicked=false;


//DRAW GUI

if (global.buttonclicked == true)
{
draw_text(100,100,"Door is unlocked")
}
Code:
//obj_button:

Left Pressed:
d = point_distance(mouse_x, mouse_y, self.x + (self.sprite_width/2), self.y + (self.sprite_height)/2)
if (d < self.sprite_width/2)
{
global.buttonclicked = true;
instance_destroy()
}
YAY! That works, Thanks. I tried just doing it in Draw instead of gui to display a sprite and it worked. Thanks for your help. Now to figure out how to add sound lol.
 
Top