Convert coordinate from one layer to another?

Get help using Construct 2

Post » Thu Jul 19, 2012 11:45 am

Hi!

I'm currently working on a survival shooting game roughly similar to gunbros or minigore, with enemy constanly generated and moving toward the player, inside a closed area larger than the window... (vertical and horizontal scrolling)

What I'd like to do is an arrow for each enemy off-screen, like an indicator of the incoming danger... Something like that (you see the blue arrows?):



My problem is that my enemies are on a layer with paralax while the arrows are on the GUI layer without parallax. They're not in the same "system of coordinates" and I don't know how to create a link between them.

I'd like to know if there is a way to know the coordinates of an element from a layer but on another layer... a kind of conversion or something, giving the coordinate of an object in a specific layer). That would be awesome since I'd like to use this system not only for the enemies but also for bonuses and other things... A crucial element of the game, really.

Thanks in advance for your help!

Rag' Ragtime2012-07-19 12:57:57
B
17
S
4
G
4
Posts: 137
Reputation: 3,767

Post » Thu Jul 19, 2012 1:01 pm

It could be the answer to my question (could it work with any type of objects?) but I don't know how to use the function in question

http://www.scirra.com/manual/28/layer/page-1

Layer functions

layer.canvasToLayerX(x)
layer.canvasToLayerY(y)
Convert from canvas coordinates to layer coordinates. Useful for converting e.g. mouse co-ordinates to layer co-ordinates. See Mouse for an example.


Thanks Ragtime2012-07-19 13:03:13
B
17
S
4
G
4
Posts: 137
Reputation: 3,767

Post » Thu Jul 19, 2012 1:24 pm

Maybe something like this?:
http://dl.dropbox.com/u/52716812/Projects/Tutorials/MoveArrowTowardsObject.capx

Basically you move the arrow to the position of the object/monster in question, but clamp it to stay within a certain coordinates, in my case I use the window sizes.

This should work since the coordinates are the same, regardless which layer you are on and if they are with or without paralax.
B
38
S
13
G
9
Posts: 151
Reputation: 8,437

Post » Thu Jul 19, 2012 8:03 pm

Thanks for your answer :)

I tried your method, it's a clean way to display the arrow properly inside a fixed windows (I'll use it) but the initial problem seems to remain the same: it works only if the player stays inside the basic window but the scrolling makes a big mess of all of that :) :(

(or maybe did I just missed something?)

That's why I wanted to know if there was a method/function/something to convert the coordinates from a layer to another... It seems to be possible for the mouse but what about the objects?

Je suppose it may be easier to find a solution, who knows?

https://dl.dropbox.com/u/39121175/MegaWar_0.07.capx

Thanks

Rag'Ragtime2012-07-19 20:05:57
B
17
S
4
G
4
Posts: 137
Reputation: 3,767

Post » Thu Jul 19, 2012 9:46 pm

[quote=The manual]
ViewportBottom(layer)
ViewportLeft(layer)
ViewportRight(layer)
ViewportTop(layer)

Return the co-ordinates of the viewport in the layout of a given layer, by its name or number. Not all layers have the same viewport if they are parallaxed, scaled or rotated separately.
[/quote]

You can convert the coordinates using those expressions.
enemy x on layer 1 = enemy.X + ViewportLeft(1) - ViewportLeft(0)
enemy y on layer 1 = enemy.Y + ViewportTop(1) - ViewportTop(0)

There's an example in this post that converts coords on one layer to another layer with different parallax.


But... why not have a separate normal layer for the arrows in between the game and GUI layers?
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Jul 20, 2012 2:15 pm

That's exactly what I was looking for :)

Thanks both of you!!!
B
17
S
4
G
4
Posts: 137
Reputation: 3,767

Post » Sun Sep 23, 2012 1:45 am

This is exactly what I was looking for, thank you!

Note:
for layers that have parallax that is positive, be sure to subtract the difference between the layers, not add as above.

Parallax is less than 100:
enemy x on layer 1 = enemy.X + ViewportLeft(1) - ViewportLeft(0)
enemy y on layer 1 = enemy.Y + ViewportTop(1) - ViewportTop(0)

Parallax is greater than 100:
enemy x on layer 1 = enemy.X - ViewportLeft(1) - ViewportLeft(0)
enemy y on layer 1 = enemy.Y - ViewportTop(1) - ViewportTop(0)
B
16
S
1
G
3
Posts: 43
Reputation: 2,597


Return to How do I....?

Who is online

Users browsing this forum: Zebbi and 25 guests