Scrolling World

From Coder Merlin
Jump to navigation Jump to search

Prerequisites[edit]

Background[edit]

In order to further the endless runner game, we must first have the player run. In order to do this, rather than moving the player, we will instead move all of the other objects on the stage; making it appear as though he is moving forward. We can do this by using 2 images and when one goes off screen, we can place it on the right side of the screen.

Experiment[edit]

Getting Started[edit]

Continue from the previous project; we’ll be editing all of our files there. Enter into the GameScene directory of the project.

tariq-mahamid@codermerlin: cd ~/Experiences/ChangingScenes/Sources/ScenesShell/GameScene

Scrolling Background[edit]

Enter the background file of your project in order to start creating the scrolling background.

In order to create the background, we must first initialize the correct variables:

    let backgroundSpeed : Int                                                                                                                                                                                                        
    let parallaxBackgrounds : [Image]                                                                                                                                                                                                
    var parallaxCurrentPoints : [Point]                                                                                                                                                                                              
                                                                                                                                                                                                                                     
    init(backgroundSpeed: Int) {                                                                                                                                                                                                     
        guard let parallaxBackgroundURL = URL(string: "https://i.ibb.co/C09XGhG/1920x1080-1.png") else {                                                                                                                             
            fatalError("Failed to create URL for parallaxBackground")                                                                                                                                                                
        }                                                                                                                                                                                                                            
        parallaxBackgrounds = [Image(sourceURL: parallaxBackgroundURL), Image(sourceURL: parallaxBackgroundURL)]                                                                                                                     
        parallaxCurrentPoints = [Point(), Point()]                                                                                                                                                                                   
        self.backgroundSpeed = backgroundSpeed                                                                                                                                                                                       
                                                                                                                                                                                                                                                     
        super.init(name:"Background")                                                                                                                                                                                                
    }

Exercises[edit]

ExercisesIcon.png
Exercises
  • Use the calculate function to move the background to the left and reset it when it goes off screen.
  • Create an obstacle script that spawns 2 types of obstacles, one that is high that the player will have to duck under and one that is low and the player will have to jump over. These obstacles should reset to the right when off screen.


CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA