Need help alternating my actor's movements from left to right with a tap (or click)
I'm looking for my actor to move to the left with a tap, and then move to the right with the following tap, and so on...
However, my actor only moves to the left and doesn't switch to the right.
I programmed an integer ("touchcount" with value 1), and created a rule that when touchcount = 1 and the mouse button is clicked, the actor moves to the left. That works fine.
In order to make my actor move to the right with the next click, I created a rule that he has to move right when touchcount = 0 and the mouse button is clicked.
Within both rules I changed the attributes to "0" and "1" correspondingly, but it seems as though the value of touchcount" is never converted to 0 within the first rule.
I have included pictures to illustrate the rules.
What do I do?
Comments
I'm giving you a "Like" because you're new but you included a screenshot of your rules, which is so helpful.
One problem is that you don't have "run to completion" checked. That setting forces behavior(s) inside a Timer to run even if the condition of the rule changes. So you're basically changing game.touchcount from 1 to 0 instantly and from 0 to 1 instantly. The timer won't do anything without that checkbox because the rule is immediately made invalid. A better idea might be to move the change attribute behaviors inside the timer so that the move occurs and then the change attribute occurs.
If that doesn't fix it, you might need to change the structure of your rule so that it says
When Mouse is Down
When attribute game.touchcount=0
[timer, behavior, etc.]
Otherwise attribute game.touchcount=1
[timer, behavior, etc.]
Without the otherwise section, the mouse is down condition may continually trigger the rules you currently have.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
This works too.
File attached:
Thank you for your reply. I'm new to the forum and to gamesalad so I still need some time to figure everything out.
I implemented the changes you suggested, but it still didn't alternate the actor's direction. I've been puzzling with alternatives all day, yet the actor will only move into one direction when I click the mouse.
What else could I do?
@Socks thank you! I downloaded the file, turned off my codes and stuck your code into the actor....but...! he isn't moving. I imagine I did it wrong, am I supposed to copy your code into a specific part of my existing code?
Did you open my file and play it ?
@Socks I managed!!
I feel like this little step will open up a whole can of worms for me, but my actor is now moving left and right on alternate clicks. Are there any tutorial videos out there which you might know of that could explain what you just did?
I don't think there are any videos that use that exact method but there are plenty of others if you Google gamesalad switch or gamesalad toggle attribute. Honestly, though, this is something that doesn't require a video. If you have an attribute with a value of 0 and you subtract it from 1, the result is 1. If you have an attribute with a value of 1 and you subtract it from 1, the result is 0. This means that as long as your attribute starts at either 0 or 1, subtracting it from 1 over and over again will toggle between 0 and 1.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Like @tatiang says it's really straightforward.
1-0 = 1
1-1 = 0
That's about it !
. . . . .
The only confusion might arise from my use of the max.speed attribute - that's just me being lazy, I just used an attribute that is in the actor by default and not being used, you could instead just make a new attribute yourself - I just used one of the pre-existing ones, just somewhere to store a value, nothing to do with 'max speed'.