Search code examples
logicsense

How do I put this into code?


I'm struggling on how I can put this into code.

int a=500;

By default x = a-100 I am trying to find a way where when one button is clicked, it sets the x-coordinate to x=a-100 , and then when the same button is pressed again, it sets x=0.

How would i logically put this into code?

Thanks a lot!


Solution

  • Somewhere, have a variable keep track of whether or not the button has been previously pressed. Since you haven't indicated what language you're using, I'll have to make this very generic, and assume the typical interface that's used to handle button press events.

    var button = (some GUI button object);
    
    var a = 500;
    var x = 2;
    
    var buttonHasBeenPressed = false;
    
    button.onPress = function() {
        if (buttonHasBeenPressed) {
            x = 0;
    
        } else {
            x = a - 100;
        }
    
        buttonHasBeenPressed = true;
    };
    

    Where you store the flag (buttonHasBeenPressed) depends on your skill level, the size of your program and many other factors. This is just a rough example using pseudo-javascript.