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

0 favourites
  • 10 posts
From the Asset Store
3 pixel charaters with over 8 animations each + animations with a gun
  • 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.

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

    AttackBox->On collision with EnemyHitBox
        System->AttackBox.Y < 100    Enemy->Set animation to "Animation1"
        System->AttackBox.Y < 200    Enemy->Set animation to "Animation2"
        System->AttackBox.Y < 300     Enemy->Set animation to "Animation3"[/code:1htpp62t]
    Or set the animation global variable you mention, then call the animation:
    [code:1htpp62t]AttackBox->On collision with EnemyHitBox
        System->AttackBox.Y < 100    System->Set AnimationVariable to 1
        System->AttackBox.Y < 200    Set AnimationVariable to 2
        System->AttackBox.Y < 300   Set AnimationVariable to 3
        System->AnimationVariable > 0   Enemy->Set animation to "Animation" & AnimationVariable
                                                                System->Set AnimationVariable to 0[/code:1htpp62t]
  • While beaten by OddConfection's answer I spent a few mins making an example so I'm going to post it anyway


  • Minor - An example is better than just text

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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!

  • 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.


  • 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.


  • 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.

  • 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


    Hope that sort of makes sense.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)