I'm making a platformer, and I'd like the player to be able to attack

TomaNeko

Member
Okay, so I've never programmed before, and I've been trying to make a very simple and short game so I can learn, and see if I should keep learning. It's a platformer, and so far I've been able to allow the player to move.
I've been following Shaun Splading's Complete Platformer tutorial, so I have the exact code as he has throughout the first two parts. In his tutorial, he has a character who shoots a gun, but I'm wanting to make a dinosaur be able to bite. I've tried looking at other tutorials, but they all show using swords, guns, and other weapons that are separate to the player's object.
I don't know if something like this is possible, but I'd like to make it so that when I press a button, the dinosaur will stop moving, and they will play a small 8 frame animation, and go back to their idle sprite. In the process of this, I'd like to be able to deplete my enemies health.
I hope this makes sense.
I'd prefer if you gave me an example of code I can insert into my game, and not an explanation of what to do.
Thank you
 

FoxyOfJungle

Kazan Games
You can do this using point_distance(), distance_to_object() or distance_to_point().
You will check if the distance is less than a certain number, you then create a variable "atack" and set it to true, if it is true, change the sprite to atack, if it is false, change to idle. Then you create a variable "time" and add +1 to it, when it reaches 30, for example, damage to the player, and after that the variable resets.
You can also make the player take damage upon reaching the last frame of the dino's animation.
 
Last edited:

samspade

Member
Very possible. I would look into state machines (I can't remember if Shaun Spalding covers them). After setting up a state machine to control the biting animation, I would simply create an invisible hit box, you can almost think of it as an invisible stationary bullet, that is a separate instance and destroys itself after a few frames, dealing damage to any enemy it touches.
 

Focksbot

Member
I'd prefer if you gave me an example of code I can insert into my game, and not an explanation of what to do.
Can't really give you code without knowing what code is already there.

But I agree with Sam above - the simplest way of doing it is to have you dinosaur create an invisible bullet (but make it visible at first so that you can see where it is while testing). Have it create the bullet at the point you want it to start biting - which might be when you press a certain key, or a few frames into the biting animation. Program the bullet so that it stays in place for the duration of the bite animation and then destroys itself. And then you simply have enemies take damage from the bullet.

When you're making something like this, you have to think of things like: do you want the bite to be able to take out multiple enemies at once? If not, then have the bullet destroy itself when it meets the first enemy. And can the bite be interrupted if the player takes damage? If so, that would need to destroy the bullet as well.
 
Top