position of an object relative to another [SOLVED] (almost)

For questions about using Classic.

Post » Thu Mar 24, 2011 7:31 am

Correct me if I am wrong (please let me be wrong!)...

but after many hours in the program, and an extensive search of the forums, I have come to the conclusion that Construct is awful when it comes to switching layouts.

As far as I can tell there is no easy way to bring one sprite from one layout to the next without destroying it in one and creating it in the next. In the process you lose all that objects private variables. Now of course the easy way to remedy this would be to change all those private variables into global ones.

Problem solved right? Well what if I want to conserve the sprites animation, speed, angles etc. from one layout to the next? That would mean I would have to (right before switching layouts) retrieve all that information and store them in a bunch of global variables in order to effectively recreate my sprite the way it was right before the layout ended and we went onto the next.

If that is the most effective way to do it then that is alright by me-- my only question then would be, how do you get the position of an object relative to another?

As an example, imagine you had a square in one layout, and then an exact replica of it in the next. Both squares are at different coordinates within the layout. In the first layout there is a circle somewhere within the square... I want to get the position of that circle within the square so that I can copy the circle to that position into the next layout without any regard for where the square actually is within the layout itself... does that even make sense? Does to me.
B
5
S
1
G
2
Posts: 99
Reputation: 1,575

Post » Thu Mar 24, 2011 9:18 am

The way I would do it, is just make two more global variables
something like:
savedX
savedY

and when you're saving the globals and about to swap layouts:
set savedX to (Square.X - Circle.X) * -1
set savedY to (Square.Y - Circle.Y) * -1
^ This will give you the Circle's 'offset' from the Square's position

When you load up the new layout
set Circle.X to Square.X + global('savedX')
set Circle.Y to Square.Y + global('savedY')

I've only -kinda- tested this, it should work though :)
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

Post » Thu Mar 24, 2011 10:19 am

[quote="aldo":3letcm8s]As far as I can tell there is no easy way to bring one sprite from one layout to the next without destroying it in one and creating it in the next. In the process you lose all that objects private variables. Now of course the easy way to remedy this would be to change all those private variables into global ones[/quote:3letcm8s]

Yes, there are global variables for that purpose. You can also make an object itself global, in which case it will automatically exist in every layout up from the first you originally included it.

It comes down to carefully designing your game. You should be aware which variables need to be carried over in between layouts and use globals, global objects (array, hash) or even the S plugin.

[quote="aldo":3letcm8s]Problem solved right? Well what if I want to conserve the sprites animation, speed, angles etc. from one layout to the next? That would mean I would have to (right before switching layouts) retrieve all that information and store them in a bunch of global variables in order to effectively recreate my sprite the way it was right before the layout ended and we went onto the next.[/quote:3letcm8s]

You could keep track of all that with variables like you say. But if it comes down to many different variables/states that need to be remembered, you probably should try to use some kind of data structure (again array, hash, S), as it may end up being less of a hassle.

[quote="aldo":3letcm8s]If that is the most effective way to do it then that is alright by me-- my only question then would be, how do you get the position of an object relative to another?[/quote:3letcm8s]

I think the best method of saving a relative position may be simply to remember the angle and the distance between the objects in question. So you can set the relative position of Sprite2 to Sprite1 with using cos/sin.

Set Sprite2.X to Sprite1.X + cos(SavedAngle)*SavedDistance
Set Sprite2.Y to Sprite1.Y + sin(SavedAngle)*SavedDistance
B
21
S
6
G
10
Posts: 1,024
Reputation: 7,445

Post » Fri Mar 25, 2011 6:49 am

Thank you! Both of those methods worked for setting a position of an object relative to another. I'm still kind of scratching my head as to why, but I am sure it will make sense the longer I stare at it :?

I am attempting a metroid style transition. It's easy to do it with magicam but only within a single layout, not one to the next...

I like to figure stuff out by myself and not constantly whine for help, but can anyone give me some suggestions about how to do this... I feel like I might be half way there...

here is my cap... the my door/transition is on layout level1A

http://dl.dropbox.com/u/19590484/tower.cap
B
5
S
1
G
2
Posts: 99
Reputation: 1,575

Post » Mon Mar 28, 2011 10:42 pm

It took me several days but I almost got what I want: a metroid style transition from one layout to the other-- its kind of jerky but check it out! :D

http://dl.dropbox.com/u/19590484/tower2.cap

Oh, and run it from layout level 1A
B
5
S
1
G
2
Posts: 99
Reputation: 1,575


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests