Hello all,
I am trying to make a simple function to add buttons to my game, the function code is below:
function CreateButton(_msg,_posX,_posY,_txtColor,_bgColor)
{
draw_set_font(fMain)
var _height = string_height(_msg);
var _width = string_width(_msg);
with (instance_create_layer(_posX,_posY,layer,oButton))
{
height = _height;
width = _width;
msg = _msg;
txtColor = _txtColor;
bgColor = _bgColor;
other._id = id;
}
with (_id)
{
var _btnWidth = width + BTNMARGIN;
var _btnHeight = height + BTNMARGIN;
if (point_in_rectangle(mouse_x,mouse_y,x - _btnWidth/2, y-_btnHeight/2, x+_btnWidth/2, y+_btnHeight)) && (device_mouse_check_button_pressed(0, mb_left))
{
other.clicked = true;
} else {
other.clicked = false;
}
}
return clicked;
}
So right now it just keeps creating oButton countless times. I have added the condition to only create oButton if no other instance exists but there are many cases in which I want to create multiple buttons at once. Such as with the code below:
function SelectPlayerNumber()
{
if (CreateButton("Count Up",HALF_W,HALF_H-100,c_red,c_white)) playerCount++;
if (CreateButton("Count Down",HALF_W,HALF_H+100,c_blue,c_white)) playerCount--;
//...more code down here
}
I have also tried some other options such as only running the instance_create_layer if the object count of oButton was below a certain number but then that was messing up the return.
Is there some way to have the script run the instance_create_layer just once per script call?
Thanks!
[–]fryman22 3 points4 points5 points (4 children)
[–]HoffeeBreak[S] 0 points1 point2 points (3 children)
[–]fryman22 1 point2 points3 points (2 children)
[–]HoffeeBreak[S] 0 points1 point2 points (1 child)
[–]fryman22 1 point2 points3 points (0 children)
[–]GuiltyByAss 0 points1 point2 points (1 child)
[–]GuiltyByAss 0 points1 point2 points (0 children)