Quick way to test the difference between 2 values

Get help using Construct 2

Post » 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 :P Is there an easier way to do what I'm trying to do?
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Post » 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
ImageImageImage
B
71
S
19
G
165
Posts: 3,425
Reputation: 95,566

Post » 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

Post » 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

Post » 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:

Image

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

Post » Fri Feb 06, 2015 12:20 am

No, you abs(EnemyZombie.X-EnemyZombieDetector.X) ...
ImageImageImage
B
71
S
19
G
165
Posts: 3,425
Reputation: 95,566

Post » Fri Feb 06, 2015 12:40 am

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

Post » 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
Image Image
B
164
S
49
G
124
Posts: 7,789
Reputation: 84,532


Return to How do I....?

Who is online

Users browsing this forum: ninepixels, oddreal and 9 guests