如何在Swift中让一个对象跟随另一个对象?

时间:2018-03-14 03:51:32

标签: swift sprite-kit skaction cgpoint

我在Swift写一个游戏。我有一个可以在屏幕上移动的播放器。我希望SpeedLines(动画)与玩家一起移动(直接在它上面)。此外,当我编译并运行我的项目时,有时SpeedLines不会显示在屏幕上。如果你能提供帮助,我将非常感激。我的代码:

func buildSpeedLines() {
    let SpeedLinesAtlas = SKTextureAtlas(named: "SpeedLines")
    var walkFrames: [SKTexture] = []

    let numImages = SpeedLinesAtlas.textureNames.count
    for i in 1...numImages {
        let SpeedLinesName = "SpeedLines\(i)"
        walkFrames.append(SpeedLinesAtlas.textureNamed(SpeedLinesName))
    }
    SpeedLineFrames = walkFrames
    let firstFrameTexture = SpeedLineFrames[0]
    SpeedLines = SKSpriteNode(texture: firstFrameTexture)
    SpeedLines.setScale(1.2)
    addChild(SpeedLines)
}

func animateSpeedLines() {

    SpeedLines.run(SKAction.repeatForever(
        SKAction.animate(with: SpeedLineFrames,
                         timePerFrame: 0.4,
                         resize: false,
                         restore: true)),
                   withKey:"SpeedLinesBehindFallingObject")
}

func startGame(){

    currentGameState = gameState.inGame

    let deleteAction = SKAction.removeFromParent()
    let moveDroppableOntoScreenAction1 = SKAction.moveBy(x: 0, y: -self.size.height * 0.1, duration: 0.025)
    let moveDroppableOntoScreenAction2 = SKAction.moveBy(x: 0, y: -self.size.height * 0.09, duration: 0.05)
    let moveDroppableOntoScreenAction3 = SKAction.moveBy(x: 0, y: -self.size.height * 0.08, duration: 0.075)
    let moveDroppableOntoScreenAction4 = SKAction.moveBy(x: 0, y: -self.size.height * 0.07, duration: 0.1)
    let moveDroppableOntoScreenAction5 = SKAction.moveBy(x: 0, y: -self.size.height * 0.06, duration: 0.125)
    let moveDroppableOntoScreenAction6 = SKAction.moveBy(x: 0, y: -self.size.height * 0.05, duration: 0.15)
    let moveDroppableOntoScreenAction7 = SKAction.moveBy(x: 0, y: -self.size.height * 0.04, duration: 0.175)
    let moveDroppableOntoScreenAction8 = SKAction.moveBy(x: 0, y: -self.size.height * 0.03, duration: 0.2)
    let moveDroppableOntoScreenAction9 = SKAction.moveBy(x: 0, y: -self.size.height * 0.02, duration: 0.225)
    let moveDroppableOntoScreenAction10 = SKAction.moveBy(x: 0, y: -self.size.height * 0.01, duration: 0.25)
    let startGameSequence = SKAction.sequence([moveDroppableOntoScreenAction1, moveDroppableOntoScreenAction2, moveDroppableOntoScreenAction3, moveDroppableOntoScreenAction4, moveDroppableOntoScreenAction5, moveDroppableOntoScreenAction6, moveDroppableOntoScreenAction7, moveDroppableOntoScreenAction8, moveDroppableOntoScreenAction9, moveDroppableOntoScreenAction10])
    buildSpeedLines()
    animateSpeedLines()
    let location = CGPoint(x: player.position.x, y: player.position.y)
    player.run(startGameSequence)

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if currentGameState == gameState.preGame {
        startGame()
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

0 个答案:

没有答案