Hi.
I couldn't understand the cooper-bounce-overlap-timer part (lesson 8 )
Can you explain it?
Sure, no problem
First off, the reason:
We don't want the turtles to overlap each other. If they do start overlapping, they won't be able to pull apart, and you will have two (or more) turtles occupying the same space. This just looks awkward.
Second, the theory:
So... we need a way to get the turtles to bounce away from each other when they touch. Since we're manually moving the turtles by setting their speed and direction, it makes sense to set their speed and direction to something else when they collide. The easiest way to do this would be to make them reverse direction temporarily. This is where the timer comes in. We want them to only "bounce" away for a short period of time, and then return to normal movement. (If we simply tell the turtles to only move away when they're overlapping, then they will continuously try to push into the turtle in front of them. The timer gives them time to get clear.)
Third, the practical application:
In event 13 of the coopBox sheet, we have the overlap condition that starts the bouncing sequence. "coopBox overlaps coopBox." easy enough.
There is an additional condition that, out of those two turtles, the one that is farthest from the player is picked. This is the one the action gets performed on. The 'bounce' variable is set to 10.
Event 14 subtracts 1 from the 'bounce' variable every 10 milliseconds. 10 'bounce' multiplied by 10 milliseconds = 100 milliseconds, or one tenth of a second. This is the timer that defines the duration of the bounce, and it gives the turtle enough time to get clear of the one it's overlapping. When the 'bounce' reaches zero, we will return to normal movement.
BUT - notice that event 13 is always setting the 'bounce' back to 10 as long as the turtle is overlapping... this means that the timer doesn't effectively start counting until the turtle is no longer overlapping.
To get the turtle to stop overlapping we need to change it's direction. This is done in event 12. Event 11 sets the speed for the turtle when it's moving normally:
+ coopBox.Value 'bounce' Equal to 0
-> coopBox: Set horizontal speed to coopBox.Value('xSpeed')
[/code:1vvt5pda]
But in event 12, we check to see if 'bounce' is greater than zero, and if so we reverse the speed:
[code:1vvt5pda]
+ coopBox.Value 'bounce' Greater than 0
-> coopBox: Set horizontal speed to 0-(coopBox.Value('xSpeed'))
[/code:1vvt5pda]
The "zero minus value" equation is a way to toggle a positive number to a negative number, and vice versa. 0 minus 5 = -5, and 0 minus -5 = 5.
To summarize:
1. A turtle overlaps another turtle. The 'bounce' timer is set.
2. The turtle timer begins to count down only when it is no longer overlapping another turtle.
3. As long as the 'bounce' timer is above 0, the turtle reverses his normal direction.
And that's about it. Hope this helps