-1
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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3856516
  • 31
  • 1
  • 7

2 Answers2

0

It appears snakeLength is a global variable. This means each time you eat the food, you are overwriting your reference to it. In your moveSnake(), you check the size of the snakeArray, but then never access the array itself. I recommend a for loop going through all of the indexes, updating their positions.

I also recommend putting snakeArray.append(snakeLength) at the bottom of the increaseSnakeLength() to have it get the newest snake part added to it.

Gliderman
  • 1,195
  • 9
  • 18
0

What you are doing is a perfect example for using a SKNode as a parent node. SKNodes serve as a abstract container for your sprites which allows you to move/scale/animate grouped sprites/nodes collectively.

Instead of adding your sprites to the scene itself, try something like this:

Create a SKNode class for your snake (that lets you control the snake and separate concerns a little better):

class Snake : SKNode {

    func grow(){
        let snakeLength = SKSpriteNode(texture: nil, color: UIColor.blueColor(), size: CGSizeMake(20, 20))
        // NOTE! position will now be relative to this node
        // take this into account for your positioning
        snakeLength.position = CGPointMake(snakeBody.position.x - inc,         snakeBody.position.y)
        self.addChild(snakeLength)
    }  

    func moveLeft() { // moves the snake parts to the left (do this for all your directions)
        // iterate over all your snake parts and move them accordingly
        for snakePart in self.children {

        }
    }      

}

In your main scene you can then create the snake:

let snake = Snake()

// when the snake eats food, grow the snake
snake.grow()

Also move the snake and it's parts by executing actions on the snake itself:

// In your main scene
snake.moveLeft()
the_critic
  • 12,720
  • 19
  • 67
  • 115