【问题标题】:Having trouble moving my skspritenodes EDIT:无法移动我的 skspritenodes 编辑:
【发布时间】: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


【解决方案1】:

看来snakeLength 是一个全局变量。这意味着每次你吃食物时,你都会覆盖你对它的引用。在您的moveSnake() 中,您检查snakeArray 的大小,但永远不要访问数组本身。我建议使用for 循环遍历所有索引,更新它们的位置。

我还建议将snakeArray.append(snakeLength) 放在increaseSnakeLength() 的底部,以便将最新的蛇部分添加到其中。

【讨论】:

    【解决方案2】:

    您正在做的是使用SKNode 作为父节点的完美示例。 SKNodes 充当您的精灵的抽象容器,它允许您共同移动/缩放/动画分组的精灵/节点。

    不要将你的精灵添加到场景本身,尝试这样的事情:

    为您的蛇创建一个SKNode 类(这让您可以更好地控制蛇并分离关注点):

    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 {
    
            }
        }      
    
    }
    

    然后您可以在主场景中创建蛇:

    let snake = Snake()
    
    // when the snake eats food, grow the snake
    snake.grow()
    

    还可以通过对蛇本身执行操作来移动蛇及其部件:

    // In your main scene
    snake.moveLeft()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2018-01-01
      • 2011-11-03
      相关资源
      最近更新 更多