Search code examples
swiftxcodegamekit

An SKAction.moveBy is not working and i can't tell why


I've been following Jared Davidson's tutorial How to make Flappy Bird and I don't know why one of the functions that is using the SKAction.moveBy isn't working. I think part of the problem is that it might be in landscape mode. The user is supposed to touch the screen which causes the ghost to jump and the obstacles to start moving. Someone please help. Here is my code for the one function:

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if (gameStarted == false)
    {
        gameStarted = true
        let spawn = SKAction.run(
        {
            () in

            self.createObstacles()
        })

        let delay = SKAction.wait(forDuration: 2.0)
        let spawnDelay = SKAction.sequence([spawn, delay])
        let spawnDelayForever = SKAction.repeatForever(spawnDelay)
        self.run(spawnDelayForever)

        let distance = CGFloat(self.frame.width + 20)
        let moveObstacles = SKAction.moveBy(x: distance, y: 0, duration: TimeInterval( 0.01*distance))
        let removeObstacles = SKAction.removeFromParent()
        moveAndRemove = SKAction.sequence([moveObstacles, removeObstacles])

        ghost.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        ghost.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 90))


    }
    else
    {
        ghost.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        ghost.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 90))
    }

}

Solution

  • I just recently ran into this same problem, and was able to solve it by moving the

        let wallDistance = CGFloat(self.frame.size.width + wallPair.frame.width)
        let moveWall = SKAction.moveBy(x:  -wallDistance, y: 0, duration: 
                TimeInterval(0.008 * wallDistance))
        let removeWall = SKAction.removeFromParent()
        moveAndRemove = SKAction.sequence([moveWall, removeWall]) 
    

    down under where you create top and bottom wall inside of createWalls. I put it under the setScales.

    Then after that you add

        wallPair.run(moveAndRemove)
    

    under where you add topWall and bottomWall as children.