Oh yeah, that's another thing, doing things too soon. Remember, the computer can't read in 3D like we can. A human can assess his environment even if it's not being observed. A computer is similar, except it only does what it's told to do and with the things it's told to do them. AND IT DOES IT in a specific way, from top to bottom, one at a time, and the current piece of code takes authority over the one before it.
Take any action in your game, and write it out before you code it if you have to. For example, a problem I run into is like this:
Is touching something //here you're touching something
---Is Variable = 0 // if var is 0
------Set Variable to 1 // set to 1
---Is Variable = 1 //here is the authority problem, the "set to 1" above makes this statement true now, so it runs this instead of stopping at setting to 1
------Set Variable to 2 //same continues until the end value is 0.
---Is variable = 2
------Set Variable to 0
What happens here is the variable will be equal to 0 at the end because you are touching, and it's quickly turning it to 1, then turning it to 2, which makes it 0 again.
The same code, just fenagled with:
Is Variable = 2
---Set Variable to 0
Is touching something
---Is variable = 1
------Set Variable to 2
---Is Variable = 0
------Set Variable to 1
This way above it won't accidentally read previous values and update those, but is locked itself in a "dead end" situation with the block of code. This is an example of an "on off" switch or button etc.
This works for all things, not just switches but animations as well. My original reply is my favorite method because it creates a solid "rubber band" style of game play, where "jump" or "run" or "walk" is like "popping the rubber band" but it all goes back in place once you're done.