Projectile making problems

Well, I wanted to make an enemy (rifleDirk) shoot projectiles to “mySprite” direction every second when mySprite is in the rifleDirk’s range, but I don’t know how. And when you meet rifleDirk in the beginning, the other “snakes” won’t follow “mySprite”. There is a test tilemap level that I put every object the game will contain, so that I can test them, and that is the reason that mySprite floats in the air. ( I didn’t do the pixel art myself and a part of my code is taken by Castle crawler )
https://makecode.com/_fAp4FXaTo3Co

I made this test code! I think it does what you want:

There’s basically two components to it:

  1. Creating a custom projectile that shoots in the direction of the hero. The easier way to it is to set the vx to be the distance of the hero from the enemy in the x direction, and vy to be the distance in the y direction. The problem is that the closer the hero is to the enemy, the slower the projectiles will be. To fix this, I added the “normalizing ratio” variable, so that no matter where the hero is, the projectile will have the same speed

  2. Checking to see if the hero is in range. This is done by the distance formula

Lemme know if you have questions! The math is kind a pain, but I can explain better if you’d like to know! otherwise feel free to use this code :smiley:

5 Likes

That’s awesome @livcheerful!

Another way you can solve part 1—that I think I learned from @richard on an earlier advanced stream :grin:—is to use the atan2 trig function to get the angle to the target. Once you have the angle, cos and sin can give you the x and y components (respectively) of the vector to the target… which you can scale however you like to control the speed.

I made a couple of edits to livcheerful’s solution to show that alternative approach: https://makecode.com/_TL8JM9i0pT6C (the big change is in the createProjectileThatFollows function).

1 Like

ooo love! the trig approach :smiley:

it seems like you gotta do some math no matter what method you use :sweat_smile:

2 Likes

Thanks so much, I really appreciate it!
Here is my fixed code if anyone wants to try:

2 Likes

Wowwwww. The visuals (character design, animation, background, etc) aren’t anything like what I expected to see in a MakeCode arcade game. Incredible!

Well, I used tool for converting png to javascript, and the sprites in that game were taken from the internet, from the original Hyper Light Drifter game.

1 Like

Oh wow. I’ve never seen that before. Thanks for pointing it out!

Please help me making some of the art I want to edit this game and increase it

So excellent! I love this idea. Thanks for sharing!