Coding a sword type weapon?

Hello,

I am looking to code a sword or other weapon that my sprite can swing. I want the sword to be able to destroy enemies it hits but am struggling to find a good way to do so.

So far I have made the sword a projectile but am looking for other ideas. I know that making a firing projectile would be easier but I have done that in another game.

Thanks for your help

2 Likes

Here’s a trick I’ve used to do this before:

Try moving around and pressing the A button to swing.

The basic idea is that the sword can be a regular sprite that’s always invisible except when it’s being used. When you press the A button, change the invisible image to a sword image and then change it back after a pause.

1 Like

Thank you so much. This is exactly the kind of thing I was looking for.

There is one Problem after some time My enemies die but not the one that I hit with my sword but random ones look https://arcade.makecode.com/S61928-50721-52858-12667

1 Like

Welcome to the forums

2 Likes

Looks like you are destroying “myEnemy” when the sword hits an enemy sprite. The thing is, “myEnemy” is whichever sprite was most recently created with that variable, which is usually not going to be the sprite you want destroyed. What you need to do is drag the red “(sprite)” block down into the spot where the “myEnemy” variable is to replace it:


That small “sprite” variable holds whichever sprite just got hit with the “otherSprite”, so it will destroy the correct sprite!!

1 Like