Doing something after laser is not in contact?
5 posts • Page 1 of 1
Doing something after laser is not in contact?
So for instance I have a red ball and a laser. I want the ball to turn green if the laser hits it.
I made this script:
But I can't figure out how to turn it red again after the laser is removed from the ball. Any help?
I made this script:
- Code: Select all
onHitByLaser: (e)=>{colorHSVA = [120.0, 1.0, 1.0, 1.0]}
But I can't figure out how to turn it red again after the laser is removed from the ball. Any help?
- egkteam
- Posts: 91
- Joined: Mon May 21, 2012 3:49 pm
- Location: East Java, Indonesia
Re: Doing something after laser is not in contact?
One way to do it is with a timer. When the laser hits the red circle, it changes the color to green, and it also loads a local variable (_timer) with sim.time. As long as the laser keeps hitting the circle, the _timer will continually get set to sim.time. When the laser stops hitting the circle, another script located in postStep checks to see if sim.time is greater than the value in _timer plus 0.1 seconds. If it is, then the color reverts back to red. Here is the code:
First, create a local variable named _timer:
Next, add this code to postStep:
And finally, add this code to onHitByLaser:
There will be a very slight delay (0.1 seconds) from the time the laser stops hitting the circle, to the time that the color actually changes. In most cases that slight delay will not cause any problems. If it does, then you will have to find some other method, without a timer, to do this.
First, create a local variable named _timer:
- Code: Select all
_timer := sim.time
Next, add this code to postStep:
- Code: Select all
sim.time > _timer + 0.1 ? {
colorHSVA = [360.0, 1.0, 1.0, 1.0]
} : {}
And finally, add this code to onHitByLaser:
- Code: Select all
colorHSVA = [120.0, 1.0, 1.0, 1.0];
_timer = sim.time
There will be a very slight delay (0.1 seconds) from the time the laser stops hitting the circle, to the time that the color actually changes. In most cases that slight delay will not cause any problems. If it does, then you will have to find some other method, without a timer, to do this.
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: Doing something after laser is not in contact?
Okay, thanks!
What is the use of the postStep script box?
Also, what is the purpose of underscores on variable names?
Also, what is the purpose of underscores on variable names?
- egkteam
- Posts: 91
- Joined: Mon May 21, 2012 3:49 pm
- Location: East Java, Indonesia
Re: Doing something after laser is not in contact?
postStep executes script at the screen refresh rate, which is dependent on sim.frequency. Also, postStep will only execute the code when Algodoo is running, as opposed to update which will execute code all the time, regardless if Algodoo is running or not!
Here is a tutorial scene that I made which explains the difference between postStep and update. I hope you will have a better understanding of postStep and update after viewing it:
Script Timing Demo
Here is a tutorial scene that I made which explains the difference between postStep and update. I hope you will have a better understanding of postStep and update after viewing it:
Script Timing Demo
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
- egkteam
- Posts: 91
- Joined: Mon May 21, 2012 3:49 pm
- Location: East Java, Indonesia
5 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 6 guests



