This is more of a logic question than a Phaser 3 question. I am trying to make the game 'snake'. I can move 1 body up, down, left, and right easily, however, I do have issues when there is more than 1 body of 'snake'. In snake, when the front of a snake switches directions, the others follow suit in such a way so that they stay behind the snake. I cannot seem to figure out how to do this- changing the direction respectively for all of the 'other' bodies behind the 'snake'. In short, this is basically one sprite following another sprite. I would appreciate some help on this. Here is my code so far:
create() {
this.snake = this.physics.add.group()
this.front = this.snake.create(25,25,"tile").setScale(0.12)
}
This is where I create my snake, as well as the 'front' of the snake.
update() {
this.cursors = this.input.keyboard.createCursorKeys()
if (this.cursors.right.isDown) {
this.x_vel = this.vel
this.y_vel = 0
this.DIRECTION = "right"
} if (this.cursors.left.isDown) {
this.x_vel = -this.vel
this.y_vel = 0
this.DIRECTION = "left"
} if (this.cursors.up.isDown) {
this.x_vel = 0
this.y_vel = -this.vel
this.DIRECTION = "up"
} if (this.cursors.down.isDown) {
this.x_vel = 0
this.y_vel = this.vel
this.DIRECTION = "down"
}
this.front.x += this.x_vel
this.front.y += this.y_vel
}
This is how I will move my snake. I am only moving the 'front' of the snake because I want all of the other snake 'bodies' to follow the 'front' of the snake.
I have tried looking at other snake projects, especially those on phaser discussions and on medium, but they don't go over the details that much and I have trouble following their code.