How do I order sprite creation via array?

0 favourites
  • 3 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • Hi, I know the title might be confusing, but I've been working on a snake game, starting with the example found in the forums. I am trying to create a different sprite for the 'head' 'body' and 'tail' for the snake, which is created by cycling through the array. Ive found the easiest way to accomplish this is to say the first instance of snake, change animation frame, and last instance, change animation frame. While it is (somewhat) working, on direction change the snake doesnt keep its order, so the 'head' ends up at the end of the snake etc and it ends up out of order. How can I keep the first instance always at the start of the snake, and the last instance at the end?

    I also have no idea how to rotate in the direction the snake is heading as I've tried multiple rotate toward angle and rotate toward position, neither giving me the desired outcome.

    tinyurl.com/y5a783fy

  • nez05

    ok, since none of the instances actually move, and the routine recreates all segments row by row starting from the top left, you can't assume that the first instance is the head and the last instance is the tail.

    what it does is store the number of segments where the head just moved to, and then decreases every place in the array by one where there is a segment.

    so, the place in the array with the highest value (which is = Segments) is the head, and the place with a value of 1 is the tail.

    to get the direction for any segment, we would either have to look in the array all around that spot for a value that is one higher, in order to determine where the snake came from to get there. I thought an easier way was to create a second array and store the direction in a corresponding cell for that segment. So, the main array stores the segment number of the snake at that spot, and the direction array at that same spot holds the direction the snake was moving.

    The only irregular case is when the snake eats the food - all the segment values are bumped up by one, so when the snake is next created, there is no tail (segment with a value of 1). So, in that case we need to look for a value of 2 and we need to know the snake just ate food. Otherwise there would be no tail until the next time the snake gets recreated.

    https://www.rieperts.com/games/forum/snakearray.capx

    oh, there was a bug - food could get created at the top of the screen where the snake could not get it. I fixed that by add 2 instead of 1 to the random number. I got a snake up to 171 segments! :)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • AllanR

    Wow, thank you! This also helps me understand more about arrays and the setup

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