How to fill an array with animation frame # after creation?

0 favourites
  • 6 posts
From the Asset Store
Selection frame like in RTS games, works both on mobile and desktop devices.
  • I'm working on a game that is an 8x8 grid of sprites. Each sprite is the same object, just a different animation frame. My goal is to get each row or column of the grid to "slide" on a single axis with a touch gesture according to which sprite is touched and which direction the swipe is. Once a sprite passes through the edge of the grid. It should wrap around to the opposite side, appearing as though each side is connected. When the touch event ends, the sprite should snap to the nearest grid square.

    Right now I have the grid set up, the sprites spawn in the correct position with a random animation frame. The sprites slide when swiped and they snap to nearest grid square when the touch event ends. Any sprites left outside the grid (i.e. the "masking" sprites) are destroyed, so there aren't a numerous amount of invisible sprites lingering around.

    I'm having a problem with the wrapping portion. I just haven't been able to figure out how to accomplish this. I'm using a 2D array to collect the animation frames of each sprite in the row/column at the time the grid is created and then access the array when a sprite is swiped to set the masking sprites to the proper animation frame. It works great for the first swipe. The masking sprites show up in the right places, everything moves according to the initial axis of the swipe. Everything snaps to the closest grid square. But when you swipe another sprite in the same row/column, the original order of the sprites show up again, instead of the new order. Something like this:

    Original order - (masking sprites are regular font and main board sprites are bold)


    If the sprites are shifted over by one so the row that was shifted now looks like this -


    But, the masking sprites of the next touch event look like this -


    They don't match up. Like I said, I've been trying to use a 2D array to create the masking sprites, so I know I need to shift the index order of the array after every touch event, but I haven't been able to figure out how to do that. Push/pop doesn't do the job and I haven't been able to figure out how to use delete/insert to access the array properly to achieve this.

    The only thing I can think of is to use the 2D array to capture the frames at the time of creation, then use a 1D array to store the animation frames at the time of access, then use delete to remove the old values from the 2D array. Push/pop the values of the 1D array to match the amount of squares moved, then insert the contents of the 1D array into the corresponding row/column of the 2D array. All that just makes me feel like I'm making this way too difficult on myself though. lol

    If anyone knows how to help me use an array to do this, or if anyone has any suggestions to use another method I'm willing. lol

  • So I tried using a 2D array along with a 1D array. I've got an algorithm for the pop/push according to how many grid squares were moved. That works perfectly, but I'm still not able to put the contents back into the 2D array without the 2D array's contents going haywire.

    Am I missing something with the 2D array, or are they made so that we cannot alter a portion of them? From what I remember in college with my c++ and c# classes, arrays (1D and multi-dimensional) can be accessed and altered by index or value, so why aren't we able to do that in Contruct 2?

    I'm starting to think I need to make a 1D array for each row and column and figure out how to alter them all together. *so daunting!!!!* :S

  • You can do pretty much anything with arrays. But the push/pop thing can be a bit daunting when using more than a dimension. You can't push complete rows, you have to push a single value on the X/Y axis, then modify every cell in subsequent actions, because the value is pushed to all cell of the added row/column.

  • Magistross - So what you're saying is, I can't delete/insert an entire row/column? Deep down inside I knew that, but I've been in denial. Lol

    Correct me if I'm wrong, but the way I'm understanding what you're saying is I have to push/pop each item (which can only be done from the ends of the array and not at the ends of the rows and columns!!!) in order to adjust the rows and columns? I can't just insert a value at an index, which should move the remaining array down 1, then delete the old index which would move remaining back to their original place?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No no no, you CAN delete and insert entire row/column. Sorry if I sounded cryptic. It's only that adding and inserting must be done in multiple actions. Deleting a row/column is quite straightforward.

    Here's a simple example of a 2D array manipulation.

  • Magistross - Thank you for clarifying! I was a little worried. lol

    Thanks for the help. I'll see what I can do with this example.

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