• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Tutorial on how to program 2D platformers?

This topic is 5840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m looking for a tutorial on the logic behind a 2D platformer; more specifically, a tutorial on how to determine if and when a character/enemy gets hit with an attack, assuming that the characters & enemies aren''t attacking at all times. Can anyone help me out here?

##### Share on other sites
Objects take space, mostly you just put a square around the object and test if a target point (any point|part of area) is in this square. Then you take it as a hit or if you want pixel perfect, determine if a single pixel has overlapped.

It is hard to give a proper answer because your question is a bit vague on exactly what you want to test.

____________________________________________________________
Try RealityRift at www.planetrift.com

##### Share on other sites
Yeah, that''s kinda what I figured, but I''ll describe a situation.

Let''s say my main character carries a sword. Suddenly he comes across an enemy with a spear. Now, what I''m trying to figure out is how to get it so that if the main character swings his sword and hits the enemy, the enemy takes damage; but if the hero walks into the enemy, or walks into the enemy as a result of the attack, he takes damage instead.

I know it''s done using rectangles and checking all 4 corners, but if you have to check ALL attack & defense squares EVERY frame, the program will probably experience massive slowdown.

##### Share on other sites
Well you don''t test all the time you first see if there is a reason for testing (such as an attack) if so you then test if there is someone in range and then go on to the actual test. I say someone because of course the enemy can start the attack.
This way you only test when there is a reason to do so and now the performance should suffer very little (depending on how many enemies you have of course)

____________________________________________________________
Try RealityRift at www.planetrift.com

##### Share on other sites
give the hero class a member variable called something like isAttacking, where 0 = not attacking, 1 = attacking, when you start an attack, set this to 1, and when attack is over set it back to 0.

then do something similar to this

if( Hero.amIAttacking() ) // attacking
{
// check if enemy is within range in correct direction etc.
// if so, call member functions of the enemy
// when you check collision dont forget to use a variable
// for sword length. ie
// int AttackBoundsXA = Hero.GetX()+Hero.GetXWidth;
// int AttackBoundsXB = Hero.GetX()+Hero.GetXWidth +
// Hero.GetSwordLength();
}

else
{
// standard collision detection
// if they collide call appropriate functions
}

##### Share on other sites
Okay, I got that.

Now, how would you store the attack & defense fields of the characters? Obviously, there''s a very good chance the character''s attack field won''t be in the same spot the whole attack. Would you create a linked list of frames, and each frame has the attack & defense areas saved within?

Uh...anyone?

##### Share on other sites
quote:
Original post by Dark Phoenix
Okay, I got that.

Now, how would you store the attack & defense fields of the characters? Obviously, there''s a very good chance the character''s attack field won''t be in the same spot the whole attack. Would you create a linked list of frames, and each frame has the attack & defense areas saved within?

I would suggest you store all information relevant for an object within the class itself. You should also provide a challenge method that other objects can use to challenge eachother with.

for example: A challenges B by calling B.Challenge(reason)
and then the return value decides what happens.

Just an idea but something along those lines would work I think. Then you don''t need to implement a database system or something either since all objects will carry the information with them.

____________________________________________________________
Try RealityRift at www.planetrift.com

##### Share on other sites
Very simple. Have a single point or multiple points stored for each frame of the sword swing animation. like have one point at the tip, one in the middle etc. Then you just need to check for collision of the point into your characters collision box.

##### Share on other sites
Also if you have your enemies in classes and such, have the enemy check to see if he hit the player. As opposed to the player seeing if the enemy hit him. What I mean is that the enemy will know if it is in attack state and if it is all it needs is the collision box of the player. This is much easier than the player class having to check all enemies and their attack states etc.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015444
×