Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Collision with "bullet" and "enemy" issues
- Alkigreen
-
4 posts
Collision with "bullet" and "enemy" issues
Hello,
I'm having an issue with my project where i think parts of the code are skipping over sections of statements.
when I shoot at an enemy, some of the time the enemy will do the effects for dying, other times they will just be deleted and the bullet will continue on.
I'm confused how to fix this. I've experienced these issues in other projects as well.
I've attached a link to the shared project. Any help would be greatly appreciated.
https://scratch.mit.edu/projects/141284980/
I'm having an issue with my project where i think parts of the code are skipping over sections of statements.
when I shoot at an enemy, some of the time the enemy will do the effects for dying, other times they will just be deleted and the bullet will continue on.
I'm confused how to fix this. I've experienced these issues in other projects as well.
I've attached a link to the shared project. Any help would be greatly appreciated.
https://scratch.mit.edu/projects/141284980/
- duckboycool
-
1000+ posts
Collision with "bullet" and "enemy" issues
Below
Last edited by duckboycool (Jan. 23, 2017 03:50:02)
- duckboycool
-
1000+ posts
Collision with "bullet" and "enemy" issues
I found the problem, in the alien do;
...This higher speed works better (in my opinion) anyway.
repeat until <touching [edge v] ?>
change y by (-1.5)
if <touching [ammo1 v] ?> then
repeat (10)
...
end
end
end
Last edited by duckboycool (Jan. 23, 2017 03:48:43)
- Alkigreen
-
4 posts
Collision with "bullet" and "enemy" issues
What is supposed to happen when they die. It always either deletes or does nothing.
This also said 0 posts at first. Oh Scratch.
I have it have some color animations and get smaller, then delete.
- Alkigreen
-
4 posts
Collision with "bullet" and "enemy" issues
I found the problem, in the alien do;...This higher speed works better (in my opinion) anyway.
repeat until <touching [edge v] ?>
change y by (-1.5)
if <touching [ammo1 v] ?> then
repeat (10)
...
end
end
end
well that did it.
thank you very much for the help.
- TheLogFather
-
1000+ posts
Collision with "bullet" and "enemy" issues
The problem you had was because you were doing two touching tests, in two different sprites.
But you were assuming that nothing could change between the two tests, which is not necessarily true.
See this demo project for more explanation, and how it should be fixed:

https://scratch.mit.edu/projects/125830858/
But you were assuming that nothing could change between the two tests, which is not necessarily true.
See this demo project for more explanation, and how it should be fixed:

https://scratch.mit.edu/projects/125830858/
- learncsscratch
-
100+ posts
Collision with "bullet" and "enemy" issues
This is called a race condition…To avoid this, have one sprite be in charge of detecting the collision and then have it broadcast a message to the other sprite….
Here is an example of a race condition…You can fix it by having one sprite detect the collision and then have the other sprite wait to get a broadcast message from the first.
Here is an example of a race condition…You can fix it by having one sprite detect the collision and then have the other sprite wait to get a broadcast message from the first.
- Alkigreen
-
4 posts
Collision with "bullet" and "enemy" issues
This is called a race condition…To avoid this, have one sprite be in charge of detecting the collision and then have it broadcast a message to the other sprite….
Here is an example of a race condition…You can fix it by having one sprite detect the collision and then have the other sprite wait to get a broadcast message from the first.
that makes sense. thank you.
this may be a silly question,
but if i have one broadcast… say “hit”
does that mean that all clones that have the line
“when i receive ”hit"
hide.
will hide as soon as that message is broadcast?
how do you set it for each clone separately?
Last edited by Alkigreen (Jan. 24, 2017 01:33:57)
- asivi
-
1000+ posts
Collision with "bullet" and "enemy" issues
Use broadcast_an wait, so in clones when i receive_ if touching sprite delete this clone.
I forgot: assuming that the detection stuff is inside a loop with your movements.
I forgot: assuming that the detection stuff is inside a loop with your movements.
Last edited by asivi (Jan. 24, 2017 09:28:29)
- DadOfMrLog
-
1000+ posts
Collision with "bullet" and "enemy" issues
If the sprite with the broadcast-and-wait has other scripts/loops running concurrently with the broadcast loop, and these scripts can change things (e.g. position/costume/etc.), then the behaviour of that can also be ambiguous, and so subject to potential race conditions – that's why asivi added the extra bit about it all being in the same loop. Use broadcast_an wait, so in clones when i receive_ if touching sprite delete this clone.
I forgot: assuming that the detection stuff is inside a loop with your movements.
If there are other scripts like this running then the behaviour will depend on the order that the scripts get started up, which depends on the layer order of the scripts in the sprite, and that can change as you drag the scripts around the editor…
OTOH, if you ensure you only have a single main control loop in your project (which is what asivi was meaning above, and which I would recommend), then using the broadcast-and-wait method will be fine.
Last edited by DadOfMrLog (Jan. 24, 2017 11:20:34)
- DadOfMrLog
-
1000+ posts
Collision with "bullet" and "enemy" issues
Yes, but the most sensible thing is to have the clone do the detecting (and the broadcast), since it can target and control itself, and only itself, much more easily than the other sprite (which has no idea which clone it hit), and since there's (often) only one of the other thing the clone is broadcasting to (e.g. the player-controlled sprite). but if i have one broadcast… say “hit”
does that mean that all clones that have the line
“when i receive ”hit"
hide.
will hide as soon as that message is broadcast?
how do you set it for each clone separately?
If you have multiple clones detecting multiple clones, then the easiest way to deal with it is to just ‘give in’ on the whole only-detect-in-one-place principle, and have the clones do a “wait 0 secs” right after they detect a hit. This pauses the script for a frame, and so gives chance for the other clone to detect it has hit too. Note that you should ensure that detector script is the only one running in the clones (i.e. you do everything else within the same loop), otherwise you can run into the problem I mentioned above (something else can happen during that “wait”, which could change whether the other clone also detects the hit).
Hope that makes sense!
Last edited by DadOfMrLog (Jan. 24, 2017 11:29:44)
- _Codey_
-
2 posts
Collision with "bullet" and "enemy" issues
Lately, I have been trying to make a shooter game. The part I'm stuck on is where when the bullet hits the zombie, but the zombie doesn't disappear.
I want the zombie to die when the bullet hits it!!
Plz help me, im struggling a lot!
I want the zombie to die when the bullet hits it!!
Plz help me, im struggling a lot!
- VintageAura
-
100+ posts
Collision with "bullet" and "enemy" issues
Please post in a new topic, this is considered necroposting.
anyways, please link an example, otherwise I'm not sure how to fix it and this isn't for people to create raw strings.
anyways, please link an example, otherwise I'm not sure how to fix it and this isn't for people to create raw strings.
- bobbyjobbyhobbynobby
-
2 posts
Collision with "bullet" and "enemy" issues
Help, im making a tank game, it wont detect the collision.
- Oumuamua
-
1000+ posts
Collision with "bullet" and "enemy" issues
Help, im making a tank game, it wont detect the collision.
Hey! You reached the limit for usernames!

- Discussion Forums
- » Help with Scripts
-
» Collision with "bullet" and "enemy" issues