Game Programming‎ > ‎FlappyBird‎ > ‎

Flappy Bird 2

Moving the Bird

A. Setting up the City

   Steps:
     1. Click on the "graphics" sub stack, import the picture of the city and note it's ID #
     2. Drag over a button to the main stack, name it "city01" and set its icon to that of the city

                       

   We are going to move that button (city01) to the left and it will look like the bird is moving 
   and when it goes off the left of the screen (card), bring it back on the right side of the screen (card)
   
                        

   We need a second copy of the city to fill the gap on the page as the first one goes off the screen
   Add a button and call it "city02". Set it's icon to the (same) number of the city graphic as above

   Don't forget to go to the "Size and Position" of the property Inspector and click on "Fit to Content"
    and move the layer to the back so the buttons show in front of the city

                     

B. Making the city move

    Now add the following code to the card script (in the "moveBird" message handler):
        Every time we move the bird, we will also move the city
        It will move each button to the left by 3. When each city goes off the screen, it will be added to the other city on its right. The cities will look like they are moving together with no break between them

    on moveBird

           set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed


   set the right of btn "city01" to the right of btn "city01" -  3

   set the right of btn "city02" to the right of btn "city02" - 3

   if the right of btn "city01" < 0 then

      set the left of btn "city01" to the right of btn "city02" -3

   end if

      if the right of btn "city02" < 0 then

      set the left of btn "city02" to the right of btn "city02" -3

   end if


   if gameisRunning then

      send "moveBird" to me in 20 millisecs

   end if

end moveBird



    Now click on the card - Property Inspector and set the background sky to a nice blue color


C. The entire code that goes on the card is below:

         Card Script:    (this code goes on the card)

global birdDirection                                      // +1 is down, -1 is up
global birdSpeed                                        // speed the bird falls
global gameisRunning

on moveBird
        set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed

   set the right of btn "city01" to the right of btn "city01" - 3

   set the right of btn "city02" to the right of btn "city02" - 3

   if the right of btn "city01" < 0 then

      set the left of btn "city01" to the right of btn "city02" -3

   end if

      if the right of btn "city02" < 0 then

      set the left of btn "city02" to the right of btn "city02" -3

   end if

        if gameisRunning then
                send "moveBird" to me in 20 millisecs
        end if
end moveBird

on startGame
        put true into gameisRunning
        put +1 into birdDirection                       // bird will be falling
        put 3 into birdSpeed
        moveBird
end startGame


on stopGame
        put false into gameisRunning
        set the bottom of btn "bird" to 400
end stopGame


on keyDown    theKey
        if theKey is space then
                put -1 into birdDirection               // move the bird up
        end if
end keyDown

on keyUp
        put +1 into birdDirection                  // let the bird start falling again
end keyUp

Comments