How do I return -1 or 1 by comparing positions

Get help using Construct 2

Post » Wed Sep 30, 2015 1:39 pm

Hi everyone,

This sounds simple enough but I can't find a way to get a -1/1 by comparing positions.

For example, there's a player object, and when it collides with something I want to compare their X positions so if the object.x is > than the player.x, I get a 1 and if it's not I get a -1.

Any idea how to achieve that? It's probably something simple but I'm stumped!

Thanks!
B
124
S
43
G
41
Posts: 849
Reputation: 28,235

Post » Wed Sep 30, 2015 1:45 pm

round( X / abs(X)) will give you -1/+1.
ImageImageImage
B
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » Wed Sep 30, 2015 1:46 pm

blackhornet wrote:round( X / abs(X)) will give you -1/+1.


Nice, exactly what I needed! Thanks!
B
124
S
43
G
41
Posts: 849
Reputation: 28,235

Post » Wed Sep 30, 2015 2:19 pm

@blackhornet
Err after some testing I still can't get what I want. Sorry, I'm really bad at math!

This function allows me to get a -1/1 position based on the 0,0 in the canvas. I've been tweaking it to get a -1/1 position based on two objects, but I don't see how...

Edit: Nevermind, got it. I swear I tried it before but it didn't work, tried it again now and it did...

I used round((player.X-door.x)/abs(player.x-door.X))
B
124
S
43
G
41
Posts: 849
Reputation: 28,235

Post » Wed Sep 30, 2015 2:25 pm

X=object.x - player.x
round( X / abs(X))
ImageImageImage
B
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » Wed Sep 30, 2015 2:25 pm

Conditional expression(under operators).

Code: Select all
object.X > player.X ? 1 : -1
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,116

Post » Wed Sep 30, 2015 2:30 pm

linkman2004 wrote:Conditional expression(under operators).

Code: Select all
object.X > player.X ? 1 : -1


Oh wow that's handy. Thank you!
B
124
S
43
G
41
Posts: 849
Reputation: 28,235


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo and 28 guests