How do I create a Snake like trail without Arrays

Get help using Construct 2

Post » Tue Apr 19, 2016 1:20 am

I'm working on a game that has a function similar to that of the classic game snake.

The players tail grows as their level increases and follows them around (player movement is in 4 directions).

I'm not sure how to get the location of the players old coordinates - as in where the player "was" 0.2 seconds ago and set the object to follow in that same pattern of movement. The tail does not need to be made up of parts but instead can be one object that gets stretched as the players level increases.

Does anyone have any ideas? I'd like to not use arrays if possible - I'd like to understand it without.
B
78
S
25
G
9
Posts: 85
Reputation: 9,255

Post » Tue Apr 19, 2016 2:57 am

I haven't made a snake game but my thinking is that you might want to try looking into setting an image point outside of your snake head, and additional appendages

You can have a global variable equaling he UID of each appendage as it is created and use that to give a reference point for spawning new appendages (Pick Appendige by UID=global variable).
Each appendage sprite could have its own variable for UID that could be set the the UID of the *spawning* appendage and used to set it's position to the aforementioned image point.

This is all just me thinking off the top of my head so let me know if this is helpful haha.

Otherwise, I think that if you've found a tutorial that uses arrays, I'd really invest time into getting a handle on it. Arrays are tricky but very useful.
B
65
S
39
G
24
Posts: 352
Reputation: 18,106

Post » Tue Apr 19, 2016 3:20 am

ultrafop I appreciate the help - but the tail wouldn't leave a trail that way.. the tail would change instantly depending on the rotation of the player.
B
78
S
25
G
9
Posts: 85
Reputation: 9,255

Post » Tue Apr 19, 2016 5:15 am

Hmmm I suppose you could have a for each condition that lerps the current x,y to the previous UID's X Y in the chain? but either way, I'm rereading your original post it it looks like you want a solid body. I'd experiment with having one appendage that spawns a duplicate sprite that has a fade effect on t. As the number of appendages increases you can increase the number of instances being spawned/adjust the fade time and see if that doesn't get you what you're looking for.
B
65
S
39
G
24
Posts: 352
Reputation: 18,106

Post » Tue Apr 19, 2016 5:17 am

You could do the following. Store the x,y position of the sprites into private variables. Then move the head or first sprite in whatever direction you like. And finally just set the other sprite's position to the saved position of the previous one.

every tick
--- sprite: set oldx to self.x
--- sprite: set oldy to self.y

pick sprite instance 0
--- sprite: move forward 32 pixels

pick sprite by comparison: sprite.iid>0
--- sprite: set position to (sprite(self.iid-1).oldx, sprite(self.iid-1).oldy)
B
92
S
32
G
110
Posts: 5,295
Reputation: 71,501

Post » Tue Apr 19, 2016 5:48 am

For the record, there should be a thread consisting purely of @R0j0hound 's posts.
B
65
S
39
G
24
Posts: 352
Reputation: 18,106

Post » Tue Apr 19, 2016 9:19 am

Since the op mentioned wanting multiple snakes the above would only work if each snake were a different object type. Here's a go at using a for loop to do it. In a way it looks a lot cleaner to me.
https://dl.dropboxusercontent.com/u/542 ... nakes.capx
B
92
S
32
G
110
Posts: 5,295
Reputation: 71,501


Return to How do I....?

Who is online

Users browsing this forum: Armench, brunopalermo, Kin, worm1 and 15 guests