GML Rotate an Object to Value and Return to Normal Position Using Button

Discussion in 'Programming' started by GOD-sSs-END, Mar 16, 2019.

  1. GOD-sSs-END

    GOD-sSs-END Member

    Joined:
    Mar 16, 2019
    Posts:
    13
    View attachment 23725 View attachment 23725 Greetings everyone,

    So very new to GameMaker and GML, but so far I love it. Here's my question:

    I want to hide and unhide the main users' panel for the game I'm making by clicking a button. The button has four states (minus up and down, plus up and down -- like SimCity). When I click "minus," I want the panel to rotate 180 degrees. This puts all but the corner of the panel off the screen. When I click "plus," I want it to rotate back (-180 degrees) to its regular position. I'd like to parent the rest of the buttons on the panel to go with it, but that's another thread. The first click works. The second click doesn't. On the third click, it all just gets nuts. Here's my code:

    obj_panel (in-game users' panel):

    Create Event

    panelRotation = "";

    Step Event

    if (panelRotation = "hide") {
    image_angle = image_angle + 10;
    }
    if (image_angle = 180) {
    panelRotation = "st1";
    }
    if (panelRotation = "st1") {
    image_angle = 180;
    }
    // unhide panel
    if (panelRotation = "unhide") {
    image_angle = image_angle - 10;
    }
    if (image_angle = 0) {
    panelRotation = "st2";
    }
    if (panelRotation = "st2") {
    image_angle = 0;
    }

    obj_hidepanel (4-state hide/unhide button):

    Left Pressed Event

    if (sprite_index=spr_hidepanelminusup) {
    sprite_index=spr_hidepanelminusdown;
    }
    if (sprite_index=spr_hidepanelplusup) {
    sprite_index=spr_hidepanelplusdown;
    }

    Left Released Event

    if (sprite_index=spr_hidepanelminusdown) {
    sprite_index=spr_hidepanelplusup;
    obj_panel.panelRotation = "hide";
    }
    if (sprite_index=spr_hidepanelplusdown) {
    sprite_index=spr_hidepanelminusup;
    obj_panel.panelRotation = "unhide";
    }

    Again, thanks in advance. Looking forward to the education.
     
    Last edited: Mar 17, 2019
  2. VagrantWhaleGames

    VagrantWhaleGames Member

    Joined:
    Jul 10, 2018
    Posts:
    58
    your description is kinda confusing...but you mean something like this? If what I got from this was correct...

    Code:
    ///OBJ BUTTON
    //
    //in room / create even set btype to 0 or 1
    //0 = rotate
    //1 = hide / show
    //left pressed event
    switch(btype)
    {
        case 0:
            objPanel.image_angle+=45;
        break;
        case 1:
            with(objPanel)
            {
                if(hide)
                {
                    hide=-1;
                }
                else
                {
                    hide=1;
                }
            }
        break;
    
    }
    Code:
    ///OBJ PANEL
    //create
    hide=-1;
    
    //draw
    if(!hide)
    {
        draw_self();
    }
     
  3. GOD-sSs-END

    GOD-sSs-END Member

    Joined:
    Mar 16, 2019
    Posts:
    13
    No, this isn't it. After some tinkering, I got your code to work, but it only redrew the panel at 45-degree intervals with each button press, creating a flower of panels after a few clicks.

    In my code, the panel is not redrawn but rotates around the button and stops at 180 degrees, which effectively hides the panel. I just can't get it to return from the hidden position to its original unhidden position of 0 degrees (-180) upon a second click.

    I do think that a combo of your code and my code may do the trick, though. I'm going to try it now. I really like the "switch" code and your use of "case". If I can get it to work, I'll post it.

    Thank you for your help and patience. This is literally my 4th day coding and making games.

    BRB!

    Here it is:

    /// Hide/Unhide Main Panel

    // Hide Panel
    if (btnState = 1) {
    image_angle = image_angle + 10;
    if (image_angle = 180)
    btnState = 2;
    } else {
    // Unhide Panel
    if (btnState = 3) {
    image_angle = image_angle - 10;
    if (image_angle = 0) {
    btnState = 4;
    }
    }
    }

    I guess I don't understand cases. I tried using them but to no avail. But this works beautifully. I have other buttons on the panel. To get them to rotate with the panel, I'll have to make them children of the panel. I heard that's possible, so I'll check. Below is the whole code I'm using to hide and unhide my panel, just in case anyone needs it. I'll start with a brief description.

    Description:

    The users' panel can be hidden by pressing a "-" button. When pressed, the panel rotates counterclockwise out of view, and a "+" button replaces the "-" button. Clicking on the "+" button unhides the panel, which rotates clockwise back into view. Both the "-" and "+" button have two states -- and up-state and a down-state. Here's the code:

    _________________________________________________

    obj_panel

    Create Event

    /// Variable set
    btnState = 0;

    Step Event

    /// Hide/Unhide Main Panel
    // Hide Panel
    if (btnState = 1) {
    image_angle = image_angle + 10;
    if (image_angle = 180)
    btnState = 2;
    } else {
    // Unhide Panel
    if (btnState = 3) {
    image_angle = image_angle - 10;
    if (image_angle = 0) {
    btnState = 4;
    }
    }
    }

    obj_hidePanel

    Left Pressed Event

    if (sprite_index=spr_hidepanelminusup) {
    sprite_index=spr_hidepanelminusdown;
    obj_panel.btnState = 1;
    }
    if (sprite_index=spr_hidepanelplusup) {
    sprite_index=spr_hidepanelplusdown;
    obj_panel.btnState = 3;
    }

    Left Released Event

    if (sprite_index=spr_hidepanelminusdown) {
    sprite_index=spr_hidepanelplusup;
    }
    if (sprite_index=spr_hidepanelplusdown) {
    sprite_index=spr_hidepanelminusup;
    }

    _________________________________________________

    I liked how you set the variable. I was using a string to do that. To get the rotation to stop, I nested my if/then statements and then conditioned them further with an else. My use of the variable you may find interesting. Variable set "1" and "3" result in actions--rotate one way or the other 180 degrees, while "2" and "4" are left undefined to stop the rotation of the panel by default.

    Challenge

    If anyone knows how to make this code simpler, I'd love it. Also, if someone can make the functionality cooler, that'd also be great.

    Try the code for yourself. You'll need one sprite and object for the panel, and four sprites for the button object (minus up, minus down, plus up, plus down).

    I'd also like to know how to make a child and parent use the same rotation point. I'm thinking set the height and width of every panel button sprite to that of the panel sprite itself, and then put the anchor in the same spot.
     
    Last edited by a moderator: Mar 18, 2019 at 7:04 AM
  4. VagrantWhaleGames

    VagrantWhaleGames Member

    Joined:
    Jul 10, 2018
    Posts:
    58
    place two objects, objPanel & objButton...objButton needs a sprite with 2 frames. 0 = show 1 = hide

    objPanel
    Code:
    ///create
    state="idle"
    
    //step
    if(state = "show")
    {
        if(image_angle < 180)
        {
            image_angle++
        }
        else
        {
            state = "idle"
        }
    }
    else if(state = "hide")
    {
        if(image_angle > 0)
        {
            image_angle--
        }
        else
        {
            state = "idle"
        }   
    }
    else if(state = "idle")
    {
       
    }
    objButton

    Code:
    ///create
    image_index=1;
    image_speed=0;
    
    //left pressed
    if(objPanel.state="idle")
    {
        if(image_index=0)
        {
            image_index=1;
            objPanel.state="hide"
        }
        else
        {
           image_index=0;
           objPanel.state="show"
        }
    }
     
  5. GOD-sSs-END

    GOD-sSs-END Member

    Joined:
    Mar 16, 2019
    Posts:
    13
    Dammit!

    Side note: How the hell do you cancel a reply? I didn't want to reply yet, but there's no cancel.

    Ugh! Sorry. . .

    That's clever. When the panel rotates, the buttons don't have to function. But here's the issue. There is a day/time clock, along with an animated graphic for the music, on the panel. Those would have to rotate too. How do I rotate the clock and the music graphic with the panel?

    Basically, I need three independent objects to rotate on the same axis with a single button click.

    I check out "physics_joint_revolute_create," but the "instance_create" function is not working. It's been deprecated by "instance_create_layer," but I don't understand how that works. And . . . Gamemaker Studio 2 doesn't seem to like it, which is out-of-this-world weird. So I can't use it, apparently.

    Any ideas?
     
    Last edited by a moderator: Mar 18, 2019 at 7:04 AM
  6. VagrantWhaleGames

    VagrantWhaleGames Member

    Joined:
    Jul 10, 2018
    Posts:
    58
    the physics system only works when its enabled, its a pre-build system included by yoyogames. This changes a lot of the functions to use "physics_" but if you're working normally you will ignore them.

    where you control the image angle you can control as many instances / objects as you want. when you set the image angle of the panel, just also change the image angle of the other objects.

    objPanel.image_angle++
    objClock.image_angle++
     
  7. GOD-sSs-END

    GOD-sSs-END Member

    Joined:
    Mar 16, 2019
    Posts:
    13
    Okay, I'll try that.

    I was overthinking it. Or . . . underthinking it - lol. . . .

    I'm sorry. My code above doesn't work. It only worked because I forgot a "{" at the end of "if (image_angle = 180)".

    I put in the bracket, and it doesn't work anymore.

    Dammit!
     
    Last edited: Mar 18, 2019 at 5:56 PM

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