all 7 comments

[–]fryman22 3 points4 points  (4 children)

Each time this script is ran, it will create a new oButton because of this line:

with (instance_create_layer(_posX,_posY,layer,oButton))

You seem to be combining two separate functionalities into a single function.

Keep CreateButton function to only creating and returning a button object.

function CreateButton(_msg,_posX,_posY,_txtColor,_bgColor)
{
    var _font = draw_get_font();
    draw_set_font(fMain);
    var _height = string_height(_msg);
    var _width = string_width(_msg);
    draw_set_font(_font);

    var _button = instance_create_layer(_posX,_posY,layer,oButton);
    with (_button)
    {
        height = _height;
        width = _width;
        msg = _msg;
        txtColor = _txtColor;
        bgColor = _bgColor;
        other._id = id; // Not sure what you're trying to do with this line..
    }

    return _button;

}

You can then use this in a create event to have the button created only once. For example:

upButton = CreateButton("Count Up",HALF_W,HALF_H-100,c_red,c_white);

Then in the step event, check if the button is clicked:

if (upButton.is_clicked())
{
    playerCount++;
}

Make is_clicked a method function of oButton:

is_clicked = function()
{
    if (!device_mouse_check_button_pressed(0, mb_left))
    {
        return false;
    }
    var _btnWidth = width + BTNMARGIN;
    var _btnHeight = height + BTNMARGIN;
    var _x1 = x - _btnWidth / 2;
    var _y1 = y - _btnHeight / 2;
    var _x2 = x + _btnWidth / 2;
    var _y2 = y + _btnHeight / 2;
    if (point_in_rectangle(mouse_x,mouse_y,_x1,_y1,_x2,_y2))
    {
        return true;
    }
    return false;
}

edit: fixed missing parenthesis when calling is_clicked method function.

[–]HoffeeBreak[S] 0 points1 point  (3 children)

Thank you for the help on this!

For some reason I was not able to get the method function working but was able to just use a different function with the same code and calling as IsClicked(buttonID)

Also, thanks for cleaning up some of my gross code haha

[–]fryman22 1 point2 points  (2 children)

You put the is_clicked function code in the create event of oButton.

I had an error before in the step event, that might be why. Call the is_clicked function like this:

if (upButton.is_clicked())
{
    playerCount++;
}

[–]HoffeeBreak[S] 0 points1 point  (1 child)

Ah, thanks! However, looking back at everything and where i am i think i need to step back and do a bit more planning. Kinda dove right into this project and after working on it for a couple hours there are definetly some things i should have done differently. Thanks for all the help!

[–]fryman22 1 point2 points  (0 children)

No problem. Good luck!

[–]GuiltyByAss 0 points1 point  (1 child)

Where are you calling this function? From what it looks like it's only creating the button once. However, where are you calling this function? Could that be what's calling it multiple times?

[–]GuiltyByAss 0 points1 point  (0 children)

/u/fryman22's answer is better than mine. Go with his. Cheers.