press actor once moves it one way press it again moves the other way

myu699myu699 Member Posts: 96

Hey guys! I am working on a new game but have run into a problem. What I want to do is have a button that when pressed moves an actor to the side, but when that same button is pressed again it goes the opposite way. Thanks!

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Create an integer attribute called game.direction and set its value to 0. Have a rule that says If Touch is Pressed, change attribute game.direction to 1-game.direction. This will toggle the value back and forth between 0 and 1 each time the actor is clicked. Then have a rule for If attribute game.direction=0... and for If attribute game.direction=1.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • myu699myu699 Member Posts: 96

    Ok thanks will make sure to try it out! :smiley:

  • myu699myu699 Member Posts: 96
    edited March 2015

    @tatiang just to be clear. Does it say 1 minus game.direction?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Yes, one minus game.direction. This is an efficient way to switch between two values, namely 0 and 1.

    If the attribute is 0, then 1-0 = 1.
    If the attribute is 1, then 1-1 = 0.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • AJaymzAJaymz Member, PRO Posts: 164

    @myy699 I would use a self.attribute on that actor instead of a game.attribute if that's the only actor that will be changing direction.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Good point, @AJaymz.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • myu699myu699 Member Posts: 96

    thanks

  • dapiondapion Member, PRO Posts: 353
    edited March 2015

    Another way is to make an Index Attribute.
    In the Rule...
    When touch is pressed,
    Change attribute to (your.index+1)%2
    So you have by every press the change between 0-1

Sign In or Register to comment.