Keep an object attached to another

0 favourites
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Been working on a similar issue this evening, and after lots of research (as you may also have already figured out) the issue comes from setting position of a sprite (or worse, pinning) to another sprite WHICH ALSO has its position set to an object. For instance, an invisible, rectangular controller sprite handles collision and movement. To this, we set the position of the visible player sprite (containing the animations). For accuracy, we set image points to the visible player sprite (as the invisible collision sprite is often smaller, and will not have a visual guide for where to set the image points). Then, we set the second sprite (sword, gun, shield, shadow, etc.) to the visible sprite's image point, and therein lies the problem. We have "pinned" (through events, not behavior) s sprite to a sprite to a sprite, and C2 HATES that.

    To test this, create a platform with a sine behavior. Set its movement to either horizontal or vertical. My platform is small, 64x16, so I'll set the period to 3 and magnitude to 32. Size yours as you wish, and use appropriate settings. Now create a rectangle sprite with platform behavior, and place it on the platform. This will be our "Controller", a rectangular collision detector, as is best practice for platforming. Next, we create our hero sprite, a visible, but non-solid representation of the player controlled hero. For this test, it can be a larger rectangle, or add your favorite animated asset. We'll name this sprite "Player". Every tick, set the position of "Hero" to "Controller". (I know this is all platformed 101, bear with me). Now, lets set a new image point on "Hero". Right-click and quick assign it to "right" (unless you're feeling ambitious and used an animated asset, in which case feel free to assign the point to wherever the hand appears). This image point will be where we hold our "object" sprite. I'll name my point "Hand" and my new sprite object "coffee". You can name yours whatever is appropriate. Now I'll create the sprite, "Coffee" and set it's position every tick to "Hero" image point 1 (remember, it's 0 based, and 0 is the origin, so your new point is 1. You can also set the "Coffee" sprite to "Hero" at image point "Hand" if you prefer names over numbers, but don't forget the quotes.)

    Alright, lets test it out. Assuming I wasn't sleepy enough to forget a step, you ought to see the "Controller" and "Hero" sprites moving together perfectly, but notice that "Coffee" is always a bit behind, sort of like a LERP effect. This is the lag caused from setting the position of one sprite to a second sprite which is positioned to a third.

    Now, try setting the position of "Coffee" to a similarly placed "Hand" image point on our original "Controller" sprite. Magically, they all move in sync now.

    This is a bit of a pain, since you lose the visual reference of the "Hero Sprite", and since the origin point and size of your controller and hero are rarely the same, a lot of tweaking comes into play when fine tuning the position of your held object for every animation frame. I'm sure you could find the position of each point on the "Hero", then offset that by the difference in origin points between the "Hero" and "Controller", but again, lots of extra work.

    If anyone has any ideas for a non-plugin fix, or method of determining image points without your visual reference, I would love to hear! Or if I've just plum lost everybody, I'll try to clarify. I do hope this has been helpful, and I hope some one comes up with better solutions!

    TLDR: Use one sprite to set position of all other sprites, do not set position to a sprite which has its position set to another sprite. A headache, but works.

    Apologies for reviving an old topic, but it seems to be unsolved. Also for any errors, it's very late.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Wow. You just solved a problem that's been stumping me for 2 years. Thank you so very, VERY much.

    It was a bit of a hassle to copy all of the relevant animations with their moving "hand" image points from the player to the movement object, but with that done it works perfectly.

    Seriously, thanks a lot.

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