Moving snake segments behind snake head

My code is simply like this
for segments:

for (let i = this.snakeArray.length - 1; i > 0; i--) {
            this.snakeArray[i].position = this.snakeArray[i - 1].position;
}

for moving the snake head:

if (this.snakeDirection === Direction.UP) {

           this.snakePos.y += Math.floor(this.MOVE_SPEED * deltaTime);

        } else if (this.snakeDirection == Direction.LEFT) {

            this.snakePos.x -= Math.floor(this.MOVE_SPEED * deltaTime);

        } else if (this.snakeDirection == Direction.RIGHT) {

            this.snakePos.x += Math.floor(this.MOVE_SPEED * deltaTime);

        } else if (this.snakeDirection == Direction.DOWN) {

            this.snakePos.y -= Math.floor(this.MOVE_SPEED * deltaTime);
        }

The problem is the segments when spawned collided with the head and caused game over state. How can I fix this? Thanks.

.position is readonly, I think. which version of CC are you using?
You may need to use .setPostion() to set the position of a node.

1 Like

I use version 3.7.1, I have tried but it is still buggy, I think because the game is running at 60fps, therefore every frame it updates, the position of the body’s parts follows strictly to the previous head’s position.
Running at 20fps is fine, but I want my game to run at 60fps btw.