1

I am writing a game in Swift. I have a player which can move around the screen. I want the SpeedLines (which are an animation) move with the player (directly above it). Also, when I compile and run my project, sometimes the SpeedLines don't show up on the screen. I would be very grateful if you could help.My code:

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?) {

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aidan
  • 89
  • 1
  • 2
  • 11

0 Answers0