蛇在垂直块中移动

时间:2017-07-15 19:20:49

标签: swift animation sprite-kit

我正在尝试使用SpriteKit制作蛇游戏(就像旧诺基亚手机中的游戏一样)。问题是,当蛇的方向改变时,蛇在一个静止的垂直块中移动,并且没有得到L形(如图中所示)。为了进入上下文,我有一个名为SKDSpriteNode的类,它是相同的SKSpriteNode,具有额外的direction属性; Snake类具有directionlength属性。有snakeBody属性,类型为[SKDSpriteNode],包含蛇体的节点。

在游戏开始时,为.up分配Snake的方向,然后由用户的滑动分配。 顺便说一句,changeDirection方法在蛇的方向改变时被调用(didSet)。这是代码和图片:

extension ClassicLevelScene {

func startGame() { snake.direction = .up; moveSnake() }

func checkPlacement(for node: SKDSpriteNode) -> SKAction {
    return SKAction.run({
        if !(node.isInsideFrame(of: self)) { self.gameOver() }
    })
}

func getMovement(for node: SKDSpriteNode) -> (once: SKAction, repetitive: SKAction) {
    let movement = SKAction.move(by: node.direction.getVector(withIntensity: movementSpeed), duration: 0.5)
    let moveAction = SKAction.sequence([movement, checkPlacement(for: node)])
    let repetitiveMoveAction = SKAction.repeatForever(moveAction)
    return (moveAction, repetitiveMoveAction)
}

func moveSnake() {
    for node in snakeBody { 
        node.removeAllActions()
        node.run(getMovement(for: node).1) 
    }
}

func moveOnce() {
    for node in snakeBody { 
        node.removeAllActions()
        node.run(getMovement(for: node).0) 
    }
}

func changeDirection() {
    for i in 0..<snake.length {
        if i == 0 {
            snakeBody[i].direction = snake.direction
            snakeBody[i].run(SKAction.move(by: snakeBody[i].direction.getVector(withIntensity: movementSpeed), duration: 0.5))
        } else {
            snakeBody[i].direction = snakeBody[i-1].direction
            snakeBody[i].run(SKAction.move(to: snakeBody[i-1].oldPosition, duration: 0.5))
        }
    }
}

screenshot

1 个答案:

答案 0 :(得分:2)

我需要发布作为答案来向你展示代码,但你应该只是移动头部,让尾巴拖到后面。现在有更好的方法可以做到这一点,但为了简单起见你已编写的代码,这里应该是什么样的

function graficoEstiloAdaptado(exame){
    var ctx = document.getElementById('graficoEsquerdo').getContext('2d');

    var total = 280;
    var incentivador = 0;
    var idealizador = 0;
    var detalhista = 0;
    var sociavel = 0;

    for(var i=0;i<exame.respostas.length;i++){
        for(var j=0;j<exame.respostas[i].alternativas.length;j++){
            switch(exame.respostas[i].alternativas[j].categoria){
                case 'Incentivador':
                    incentivador += 4-j;
                    break;
                case 'Idealizador':
                    idealizador += 4-j;
                    break;
                case 'Detalhista':
                    detalhista += 4-j;
                    break;
                case 'Sociável':
                    sociavel += 4-j;
                    break;
            }
        }
    }

    var porcentagens = {
        incentivador: (incentivador/total).toFixed(1),
        idealizador: (idealizador/total).toFixed(1),
        detalhista: (detalhista/total).toFixed(1),
        sociavel: (sociavel/total).toFixed(1)
    };

    var chartEstiloAdaptado = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: [porcentagens.incentivador + "%", porcentagens.idealizador + "%", porcentagens.detalhista + "%", porcentagens.sociavel + "%"],
            datasets: [{
                label: "Gráfico I",
                data: [
                   porcentagens.incentivador,
                   porcentagens.idealizador,
                   porcentagens.detalhista,
                   porcentagens.sociavel
                ]
            }]
        },
        options: {
            animation: {
                onComplete: function() {
                    window.JSREPORT_READY_TO_START = true;
                }
            }
        }
    });
}