How do I check Y coord and play varying animations? SOLVED

Get help using Construct 2

Post » Tue Jun 17, 2014 8:32 am

This may be hard to explain and I'm sorry I can't share my current .capx, but hopefully someone will understand.

I have a very large sprite (an enemy) that's attached to a simple, very long vertical rectangle acting as it's hitbox. When the player attacks it (by spawning an square 'attack box' that overlaps anywhere on the enemy rectangle hitbox), the enemy plays a hit animation - all of that is working fine. What I'd like to know is how I can have the player's attack box check the y coordinate when it overlaps the enemy rectangle hitbox, and have the enemy play a different hit animation if it's over or under a certain value.

Basically I'm trying to fake a sprite having multiple collision boxes by having it play different animations based on where it is attacked on the long vertical hitbox. I've got all my animations set up based on global variables, so I do understand that much. What I don't know is how to set up the events and actions for the collision to check the Y coordinate of the enemy's box and send a value to a new variable that will trigger an animation based on how high the Y value is.

Any advice is appreciated! Thank you for your time.
Last edited by Joy on Sun Jun 22, 2014 8:08 pm, edited 1 time in total.
B
4
Posts: 37
Reputation: 276

Post » Tue Jun 17, 2014 9:09 am

You just need sub-events to compare the AttackBox.Y value under the collision detection event, then either play the animation directly:

Code: Select all
AttackBox->On collision with EnemyHitBox
    System->AttackBox.Y < 100    Enemy->Set animation to "Animation1"

    System->Else
    System->AttackBox.Y < 200    Enemy->Set animation to "Animation2"

    System->Else
    System->AttackBox.Y < 300     Enemy->Set animation to "Animation3"


Or set the animation global variable you mention, then call the animation:
Code: Select all
AttackBox->On collision with EnemyHitBox
    System->AttackBox.Y < 100    System->Set AnimationVariable to 1

    System->Else
    System->AttackBox.Y < 200    Set AnimationVariable to 2

    System->Else
    System->AttackBox.Y < 300   Set AnimationVariable to 3

    System->AnimationVariable > 0   Enemy->Set animation to "Animation" & AnimationVariable
                                                            System->Set AnimationVariable to 0
Last edited by OddConfection on Tue Jun 17, 2014 9:20 am, edited 1 time in total.
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,778

Post » Tue Jun 17, 2014 9:16 am

While beaten by OddConfection's answer I spent a few mins making an example so I'm going to post it anyway :)

attackbydistance.capx
You do not have the required permissions to view the files attached to this post.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Tue Jun 17, 2014 9:22 am

@Minor - An example is better than just text :)
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,778

Post » Tue Jun 17, 2014 9:23 am

Thank you both very much! It is really late here, but I will follow your advice and let you know if I get it fixed. Much appreciated!
B
4
Posts: 37
Reputation: 276

Post » Tue Jun 17, 2014 2:56 pm

It's working, thanks again! I do have one other question though; if the enemy hitbox is moving vertically at all (i.e. the enemy is moving), the check for the Y value of the player or the player's attack box may not be accurate. Could you tell me how, if it's possible, to actually check the Y value of the location on the enemy hitbox where the attack box overlapped it, and not the Y coordinate of the screen? The attack box can be 1 pixel in order to get a precise value.

Thanks! :)
B
4
Posts: 37
Reputation: 276

Post » Tue Jun 17, 2014 4:02 pm

My example already does that as the distance calculated is from the enemy_hitbox origin (top/middle). It doesn't use Y co-ordinate of the screen at all.

I've added sine movement to the enemy_hitbox in my previous example but changed nothing else. Keep the player sprite still while overlapping the enemy_hitbox and press space while the enemy_hitbox is moving and watch the distance change.

attackbydistancemoving.capx
You do not have the required permissions to view the files attached to this post.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Tue Jun 17, 2014 7:50 pm

Thank you again Minor, I see that it does work. Could you explain in a little more detail how this part of your code works though?


Set distance to distance(attack_detect.X, attack_detect.Y, attack_detect.X, enemy_hitbox.Y)


I assume the first two entries retrieve the X and Y coordinates of the player's attack box, but why do you use another X command for the attack box with the Y for the enemy hitbox? I'm not questioning that your method works, I just don't quite understand exactly what's happening with it. :)

Thanks for your time so far!

edit: Oh, I see that function does require x1, y1, x2, y2, sorry. If you have time though, could you still explain what measurements are taking place? I'm more of a graphics person than a math person. ;)
B
4
Posts: 37
Reputation: 276

Post » Tue Jun 17, 2014 8:02 pm

If you use distance(attack_detect.X, attack_detect.Y, enemy_hitbox.X, enemy_hitbox.Y) you would be checking the distance of a diagonal line which will be longer than a straight line. Basically I'm getting just the distance from attack_detect.y to enemy_hitbox.y

It may not make much of a difference for what you are using it for but can give results that people may not be expecting like HERE

Hope that sort of makes sense.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Tue Jun 17, 2014 8:05 pm

That does make sense, thanks very much. I was just starting to understand it when you replied, and you confirmed what I wasn't sure about. :)

Sorry to bother you and thanks again!

edit: funny, I came across that same thread that you linked to when I first encountered this problem, but I had no idea what that guy was having problems with. Seems I'm not the first one you've helped understand this. ;)
B
4
Posts: 37
Reputation: 276


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 21 guests