func increaseSnakeLength(){
snakeArray.append(snakeLength)
inc += snakeBody.size.height
if snakeMovingLeft == true{
snakeLength = SKSpriteNode(texture: nil, color: UIColor.blueColor(), size: CGSizeMake(20, 20))
snakeLength.position = CGPointMake(snakeBody.position.x + inc, snakeBody.position.y )
addChild(snakeLength)
}
if snakeMovingRight == true{
snakeLength = SKSpriteNode(texture: nil, color: UIColor.blueColor(), size: CGSizeMake(20, 20))
snakeLength.position = CGPointMake(snakeBody.position.x - inc, snakeBody.position.y)
addChild(snakeLength)
}
if snakeMovingUp == true{
snakeLength = SKSpriteNode(texture: nil, color: UIColor.blueColor(), size: CGSizeMake(20, 20))
snakeLength.position = CGPointMake(snakeBody.position.x, snakeBody.position.y - inc)
addChild(snakeLength)
}
if snakeMovingDown == true{
snakeLength = SKSpriteNode(texture: nil, color: UIColor.blueColor(), size: CGSizeMake(20, 20))
snakeLength.position = CGPointMake(snakeBody.position.x, snakeBody.position.y + inc)
addChild(snakeLength)
}
}
func moveSnake(){
if snakeArray.count > 0{
snakeLength.position = CGPointMake(snakeBody.position.x, snakeBody.position.y)
}
snakeBody.position = CGPointMake(snakeHead.position.x, snakeHead.position.y)
snakeHead.position = CGPointMake(snakeHead.position.x + snakeX, snakeHead.position.y + snakeY)
}
[Snake Game][1][1]: https://i.stack.imgur.com/nhxSO.png
My function increaseSnakeLength() adds a new snakeLength block to the scene when the snakeHead makes contact with my food. Then in my moveSnake() function i move the snake blocks but when i move the snakeLength block it only moves the newest block and leaves the old block not moving. You can see this in the image above. I am not sure how to move all the snakeLength blocks at the same time.