Light Switch

Discussion in 'Programming' started by Tari, Jul 14, 2019 at 5:13 AM.

Tags:
  1. Tari

    Tari Member

    Joined:
    Jun 1, 2019
    Posts:
    7
    This is a bit of a beast doubt. I'm doing a light system,t on image_index =1 it's on and 0 it's off, how can I make the lights turn on and off with one button?
     
  2. Lonewolff

    Lonewolff Member

    Joined:
    Jan 8, 2018
    Posts:
    1,088
    Code:
    image_index = light_state;       // 0 is off, 1 is on
    
     
  3. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,272
    Further to what Lonewolff says for setting the image_index equal to light_state, if you wanted to use a single button to flip the light on and off you can use something like this to toggle the state of the light_state variable between true/false (1 and 0) when you press the Space Bar.

    Code:
    if(keyboard_check_pressed(vk_space))
    {
        light_state = !light_state;
    }
     
    HayManMarc likes this.
  4. Tari

    Tari Member

    Joined:
    Jun 1, 2019
    Posts:
    7
    But this I put in the event of collision with the switch or in the step event?
     
  5. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,272
    It depends how you want to toggle the light on and off in your game.

    The code I posted would be put in a Step Event and would toggle the value of light_state between true/false (on/off) every time you pressed the Space Bar on the keyboard no matter the position of your character, but you can do it however you like.

    If you wanted your player to have to be within a certain distance of the light switch before they could press it then you could do something like a point_distance() check between the player and the switch first, and only if it was under a set distance then allow the input to toggle the light to be run.

    You haven't given much info other than asked how to toggle it, so it's hard to give you help specific to the way you have things setup.
     
  6. Tari

    Tari Member

    Joined:
    Jun 1, 2019
    Posts:
    7

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