# change variable based on coordinates

Get help using Construct 2

### » Mon Sep 10, 2012 11:09 pm

Hello!

In my game I have this bar, and a sphere. Whenever the sphere is over the bar, the player must press spacebar. But, I want to have the player to check if this bar is red or green. If it's red, he cannot press spacebar.

My issue os That i need the bar to change colors when the sphere is about 100 pixels close to the bar. Problem is, the sphere can approach from left or right, and i can't calculate X that way.

B
6
S
1
Posts: 56
Reputation: 862

### » Mon Sep 10, 2012 11:26 pm

You missed the CAPX ^^

Do you want a simpler way? Make a check by overlapping with offset or, make a check with a invisible mask around and pinned to your bar.

B
95
S
21
G
16
Posts: 1,337
Reputation: 20,955

### » Mon Sep 10, 2012 11:48 pm

In terms of performance, how wold the mask imply at this game?
B
6
S
1
Posts: 56
Reputation: 862

### » Tue Sep 11, 2012 12:02 am

what about checking distance between bar and sphere?

distance(bar.X, bar.Y, sphere.X, sphere.Y) < 100 -> change color
B
141
S
58
G
37
Posts: 2,549
Reputation: 31,736

### » Tue Sep 11, 2012 12:23 am

[QUOTE=shinkan] what about checking distance between bar and sphere?

distance(bar.X, bar.Y, sphere.X, sphere.Y) < 100 -> change color
[/QUOTE]

@shinkan It will check the distance only of the origin point, right?

@Zerei the impact is near to zero, you can't feel the difference. I'm using it and it's rocking on my game.

You should use square objects instead of complex polygons, because more points on the polygon issue on performance. It's better to check 10 objects with 4 points on their polygons instead of 2 with 20 points.

This is my debugging interface, you can check all the collision masks, I implemented them on a single layout and did this layout check if it's on preview, if yes, it become visible, if not, invisible.

B
95
S
21
G
16
Posts: 1,337
Reputation: 20,955

### » Tue Sep 11, 2012 12:47 am

distance(bar.X, bar.Y, sphere.X, sphere.Y) - yes, from the origin point

but you modified it as you want

distance(bar.X, bar.Y-(bar.Height/2), sphere.X, sphere.Y) - this will check between sphere origin point and bar top center.
B
141
S
58
G
37
Posts: 2,549
Reputation: 31,736

### » Tue Sep 11, 2012 7:45 pm

Thanks Guys! It really helped!
B
6
S
1
Posts: 56
Reputation: 862