Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make two objects disappear when they touch each other?
- CoolCoder1005
-
Scratcher
10 posts
How to make two objects disappear when they touch each other?
So recently I've been working on a small game (you can find it here) where you shoot at volleyballs coming your way. Something I've had a bit of trouble with, however, is making the laser beam and the volleyball disappear on contact. The first thing I tried was making separate If conditions to check whether they've reach either end of the screen or touched the other object, and then checking if it had hit the other object. It was something like this for each sprite:
Object1:
Essentially, I just split the original code into two threads for the first object, and the second one will only be eliminated whenever the first one sends the broadcast, ensuring that both are destroyed. This would work if there were only one clone of the second object at all times, which is not true for my case. Whenever the broadcast is sent, all instances are destroyed, even if they never came into contact with one of the first objects. I could technically switch the code around, but that would just pass the problem to the first one, which is not what I want. Is there a way to send a signal to only one of the object2s? Better yet, is there another way I can achieve this without having to eliminate one at a time?
when I start as a cloneHowever, this wouldn't work. I suspect this was because one would be deleted before the other sensed it, thus not registering for the opposite one. I then tried this (my current one):
repeat until <<reached one end> or <touching [other object] ?>>
move (10) steps
end
if <touching [other object] ?> then
do something
end
delete this clone
Object1:
when I start as a cloneObject2 (First clone code in here):
repeat until <at edge>
move (10) steps
end
delete this clone
when I start as a clone
wait until <touching [object2] ?>
do something
broadcast [obj2 elim]
delete this clone
when I receive [obj2 elim]
delete this clone
Essentially, I just split the original code into two threads for the first object, and the second one will only be eliminated whenever the first one sends the broadcast, ensuring that both are destroyed. This would work if there were only one clone of the second object at all times, which is not true for my case. Whenever the broadcast is sent, all instances are destroyed, even if they never came into contact with one of the first objects. I could technically switch the code around, but that would just pass the problem to the first one, which is not what I want. Is there a way to send a signal to only one of the object2s? Better yet, is there another way I can achieve this without having to eliminate one at a time?
Last edited by CoolCoder1005 (March 5, 2024 02:19:57)
- deck26
-
Scratcher
1000+ posts
How to make two objects disappear when they touch each other?
Each object should detect the touch, wait 0 seconds (to give the other time to also detect the touch) and then delete itself.
- CoolCoder1005
-
Scratcher
10 posts
How to make two objects disappear when they touch each other?
Thanks! That fixed the problem entirely! This is what my new code looks like now:
when I start as a clone
repeat until <touching [other end]>
move (10) steps
end
do something
delete this clone
when I start as a clone
wait until <touching [other object]>
do something
wait (0) secs
delete this clone
- Discussion Forums
- » Help with Scripts
-
» How to make two objects disappear when they touch each other?