Can anyone help me this is due tmmr😭

idk how to get the enemies on level 2 to shoot at the player when the player is at a close enough distance. can anyone help
link: https://arcade.makecode.com/S40267-80912-83170-51239
note(it’s not fully done yet i’m js stuck on that bit)
thx

5 Likes

Hello! Welcome to the Makecode Forum! I believe someone has made an extension that lets you do this better, but an easy solution would be to check every Enemy sprite and make it shoot if it is close to the player. The simplest way I can think to do this would be to do something like

Forever:
Wait (however often you want the enemies to shoot)
For (value) in list (array of all sprites of kind (enemy)):
If (((value) x ) - ((playerSprite) x ) > (-50)) and (((value) x ) - ((playerSprite) x ) < (50)):

      (Make (value) shoot at playerSprite)

Sorry that my sudo code is hard to read, I don’t want to make this in blocks and screenshot it cause that’s too much work. In this case, the “-50” and the “50” are how many pixels away the Enemy sprite can “see” the player in the X direction. Making the enemy sprites shoot at the player is also difficult. Usually what I do is I make a completely clear sprite follow the player using the “(sprite) follow (othersprite))” block and then I create the actual projectile and set its VX and VY to the VX and VY of the clear sprite that you can’t see. This way, the enemy shots will move towards the player, but won’t follow them so they can still dodge the bullets and stuff. You might want to create the clear Sprite at the beginning of the game and simply set its position to the enemy position before the enemy shoots. This way you aren’t making a new clear Sprite every time the enemy shoots. I hope this helps!! If you need me to send a picture of the blocks I can do that, it will just take some time.

3 Likes

I would recommend using an extension (player utility)

4 Likes

alr tysm but can you pls send the cod bc idk what it means
sorry for any inconvenience

1 Like

ok thanks for the help

1 Like

Here: This link here has the code I wrote. It works… well enough. If you mess around with it you might find some problems, but for a school project it should work just fine!