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

This forum is currently in read-only mode.
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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

    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.

    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.

    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.

    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?

    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

  • 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

  • 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!

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

    Oh, and run it from layout level 1A

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)