How do I swap sprites in a list while dragging up and down?

From the Asset Store
Full game Construct 2 capx code source to post on Google Play
  • Hello all,

    I have a several rows, each row is draggable (up and down / y axis only).

    What I need to happen is let's say I drag row 1 down the list I would like the other rows to move in place. So as I drag and collide over row 2, row 2 moves into row 1.

    Example

    Start:

    1 (Drag this row)

    2

    3

    4

    End:

    2

    3

    1 (drop row here)

    4

    If I drag down to row 5 then all the rows above it move up one space. I should be able to drag any row to any position as many times as I like without issue.

    Does anyone have any ideas on how best to do go about this?

    Many thanks in advance

    Tagged:

  • There you go:

    drive.google.com/file/d/1S0nZYekd6-x1jI7JZQykRraJMXbIQyPJ/view

    Replace the text with your sprite.

  • Maverick1912 I really appreciate you sending this through, this helps a lot from both showing how it can be done and a learning experience. I can't thank you enough for your help.

    While I had something working for the top two rows, it was buggy and ultimately would have created more problems down the line and my events list would have been at least 10x what yours example shows.

    Again, very much appreciated.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You're welcome.

    Can you show me the bug that you have?

  • So even though I was setting the position to increments of 16px, sometimes I could drag and drop and the positioning would be a few pixels out and the rows wouldn't 'snap' to position and would be sometimes overlapping by about 4 or 6 pixels. I couldn't work out why that was happening even though I had set the exact positions in global variables. I would need to tap the row again for it to snap into position.

    The other issue I had was when I started the drag I was saving the start position to a global variable and swapping the next row it overlapped with and moving the next row to that start position.

    However, I realised if I wanted to drag down more rows, all of those rows would then move to the start position, not really bugs either of them just not refined.

    The other thing that I've learned looking at your example is you use one sprite and I have a different container sprite for each row which makes more sense from an efficiency point of view.

    Again, I can't thank you enough.

  • In that case, I suggest you add all sprites into a family like this:

    drive.google.com/file/d/18RIwR7GuvGGBSFne1UfNZ-_T0PzBPLfP/view

    You can move the sprite with boolean firstOne = true anywhere, then others will follow and re-arrange.

    I also add a function to re-align all of them when finished the list of position Y.

  • Thank you, I really appreciate this, you really have gone above and beyond.

    Both examples work really well and I can and will learn from both. This is really great. Many thanks again Maverick1912.

    I'll be sure to show you what I'm making when I'm done, You'll certainly get a credit in-game.

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