【发布时间】:2016-01-01 15:43:51
【问题描述】:
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)
}
[贪吃蛇游戏][1][1]:http://i.stack.imgur.com/nhxSO.png
当蛇头与我的食物接触时,我的函数 increaseSnakeLength() 会在场景中添加一个新的蛇长度块。然后在我的 moveSnake() 函数中,我移动蛇块,但是当我移动蛇长度块时,它只移动最新的块,而使旧块不动。您可以在上图中看到这一点。我不知道如何同时移动所有的snakeLength 块。
【问题讨论】:
-
蛇是只能上下左右移动还是可以任意角度移动?
-
蛇只根据滑动方向左/右/上/下移动
标签: swift sprite-kit skspritenode