Scrolling World

From Coder Merlin
Revision as of 21:42, 6 July 2021 by Tariq-mahamid (talk | contribs) (Created page with "== Prerequisites == * Changing Scenes == Background == In order to further the endless runner game, we must first have the player run. In order to do this, rather than mov...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

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]

ExercisesExercisesIcon.png
  • 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.