# Quick way to test the difference between 2 values

Get help using Construct 2

### » Thu Feb 05, 2015 7:12 pm

Is there an easy way to just test the difference between 2 values? I'm making my own directional animation events which should say something like:

If (Sprite.X < Destination.X)
and (difference (Sprite.Y,Destination.Y) < difference (Sprite.X,Destination.X))
> Set animation "WalkRight"

If (Sprite.Y < Destination.Y)
and (difference (Sprite.Y,Destination.Y) > difference (Sprite.X,Destination.X))
> Set animation "WalkDown"

... and so on for all the other directions. I know I can do it manually by subtracting one value from another, but all the Cartesian positive/negative stuff's twisting my head a bit Is there an easier way to do what I'm trying to do?
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Thu Feb 05, 2015 7:18 pm

You can use abs() to get rid of the sign,
https://www.scirra.com/manual/126/system-expressions
B
72
S
22
G
290
Posts: 3,885
Reputation: 159,511

### » Thu Feb 05, 2015 7:39 pm

Nice - that works, but I still seem to need 8 events rather than 4. Eg for "WalkRight", I need to test twice - one for when Sprite.Y > Destination.Y, and one for when Sprite.Y < Destination.Y, as it changes the order of the subtractions. Might be my crappy logic though! Is there a way to do it in 4 nice neat events?
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Thu Feb 05, 2015 9:48 pm

You can have 2 events with 2 subevents each. Events will calculate the difference (first will do it for X, second for Y) and subevents will look for relative position.
B
12
S
6
G
2
Posts: 99
Reputation: 1,727

### » Thu Feb 05, 2015 11:40 pm

That's a lot neater, but still not sure I've got it. Here's the problem I'm having:

The Up and Left directions work fine all the time, Down and Right only work sometimes. Pretty sure know why it isn't working - half the time the wrong absolute value is being subtracted, it should always be the smaller one subtracted from the greater one. I just can't figure out a (simple) way to do it.

If I could just replace the values in those abs events with with "difference (EnemyZombie.X,EnemyZombieDetector.X)" I'm pretty sure it'd work fine.
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Fri Feb 06, 2015 12:20 am

No, you abs(EnemyZombie.X-EnemyZombieDetector.X) ...
B
72
S
22
G
290
Posts: 3,885
Reputation: 159,511

### » Fri Feb 06, 2015 12:40 am

BOOM. I'm an idiot thanks!
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

### » Fri Feb 06, 2015 4:13 am

You could use conditionals and just set the animation based on those.
set animation to:
x>n ?thisanimation :thatanimation

Not sure how you would chain multiple conditionals together.
A nice suggestion for C3 would be easier ways to set stuff like that up.
I realize the canned answer is just use conditions and subevents, but even those are somewhat broken when you want to use else, and or together.
@Ashley
B
179
S
50
G
207
Posts: 8,719
Reputation: 128,306