Here is my game, https://arcade.makecode.com/S86781-93594-46145-26528 for some reason only one enemy shoots a projectile that aims at the player and the others mimic the angle of the one on the top right could someone help?
I was trying to make a tower defense game a while ago and I was having a similar problem where I could only get one projectile to go to a tower, and the rest didn’t care. The only solution I could think of is to make a new block and sprite name for every single projectile. That is not very do-able though so I gave up on that game. I hope you can find a better solution.
Thank you!
This problem is probably happening because variables only hold the most recent sprite created. What you want is to set up a For loop like this:
The For loop goes through every sprite of kind “enemy” and runs code for each one. The sprite that is being effected is held in the “value” variable, which you can drag out of the For loop and put wherever you want, for example, I dragged the “value” variable into the “MySprite” spot of the projectile block so that each sprite of kind “enemy” will shoot that projectile when the loop is run!
Looks like your problem is here:
The problem you are having is with those “after” blocks. The program loops through every enemy, creating a hitbox sprite for each one, and then after 100 milliseconds it makes each sprite shoot a bullet with the velocity of the hitbox sprite. The problem is that the variable “hitbox” only holds the most recent hitbox sprite created, so the hitbox that every bullet uses to set velocity is just the most recent one created, which is why they all have the same velocity!
What you need to do instead is use math to determine the velocity to give the projectile, or use an extension like arcade-sprite-util to do it for you, which has a “set (sprite) velocity to (number) at angle (number)” block and a “get angle between (sprite) and (sprite)” block, which can be combined to send a projectile from one sprite towards another!
You could also look at this very long explanation that I wrote here, which has the math you would need if you didn’t use the sprite util extension: Enemies fire projectiles at me Raycasting - #2 by WoofWoof
To get the sprite util extension, search “riknoll/arcade-sprite-util” in the extension search bar.
Riknoll is the GitHub account of the moderator who made the extension, Richard!