Moving the Columns
A. Setting up the Columns
Steps:
1. Click on the "graphics" sub stack, import the pictures of the columns (top and bottom) and note the ID #'s
2. Drag over 2 buttons to the main stack, name them "top1" and "bot1"
Now set each button to the icon ID of it's "pipe" (there is a top and a bottom pipe), uncheck all borders, etc
Click on the "Fit Content" buttons and move them to see the ends of each pipe
Hold down the "CTRL/Control" or the "Option" key, then click and and drag off copies of each pipe
Bring up the Property Inspector, then click on them one by one and change the names to "top2", top3", "bot2", "bot3"
B. Adding the Code to Move the Columns/Pipes
Now lets add the code to move the first set of pipes (Columns?). (We will move them to the left until they go off the screen (card), then bring them back on the right of the screen (card)
Add the following code and put it in it's own message handler: "movePipes"
set the right of btn "top1" to the right of btn "top1" - 4
if the right of btn "top1" < 0 then
set the left of btn "top1" to the right of card "main"
set the right of btn "bot1" to the right of btn "bot1" - 4
if the right of btn "bot1" < 0 then
set the left of btn "bot1" to the right of card "main"
And add a line in the moveBirds message to move the pipes when we move the bird.
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
if the right of btn "city02" < 0 then
set the left of btn "city02" to the right of btn "city02" +3
send "moveBird" to me in 20 millisecs
(We could put all the code together in the "moveBird" handler but that could get a bit messy because we still have to program for 2 more buttons (2 more pipes and that is a lot of code. So let us keep the pipes code separate
Now if you run it, you should see the first set of pipes moving across the card.
We could just copy and paste 3 copies of the code for "top1" and "bot1" and change the names to "top2", "top3", "bot2" and "bot3" but that is a lot of editing and it is easy to make mistakes.
The Smarter way is to use the "Repeat" command (from 1 to 3) and change the numbers as we go
e.g.
on movePipes
repeat with x = 1 to 3
set the right of btn ("top" & x) to the right of btn ("top" & x) + 4
if the right of btn ("top" & x) < 0 then
set the left of btn ("top" & x) to the right of card "main"
end if
set the right of btn ("bot" &x) to the right of btn ("bot" &x) +4
if the right of btn ("bot" &x) < 0 then
set the left of btn ("bot" &x) to the right of card "main"
end if
end repeat
end movePipes
Since the top and bottom pipes move together, we do not have to test both of them for going off the card
So we can make the code even shorter by moving them together...
e.g.
on movePipes
repeat with x = 1 to 3
set the right of btn ("top" &x) to the right of btn ("top" &x) + 4
set the right of btn ("bot" &x) to the right of btn ("bot" &x) +4
if the right of btn ("top" &x) < 0 then
set the left of btn ("top" &x) to the right of card "main"
set the left of btn ("bot" &x) to the right of card "main"
end if
end repeat
end movePipes
C. Oops - It goes too slow
If the columns move too slow, it could be due to a number of reasons:
- Your computer - what chip it has, what speed it is, if it has a graphics chip, if it has software acceleration, etc
- The OS that you are using (Windows, OS/X, Linux) and what version of it is on your system
- The version of LiveCode that you are using
All those factors affect the speed that the computer updates the screen. We can minimize the problem by reducing the number of times we send instructions to the screen. We can bundle all of them together and send them just once to the screen using "lock screen" and "unlock screen".
e.g.
on movePipes
lock screen
repeat with x = 1 to 3
set the right of btn ("top" &x) to the right of btn ("top" &x) + 4
set the right of btn ("bot" &x) to the right of btn ("bot" &x) +4
if the right of btn ("top" &x) < 0 then
set the left of btn ("top" &x) to the right of card "main"
set the left of btn ("bot" &x) to the right of card "main"
end if
end repeat
unlock screen
end movePipes
We should also do that anywhere else that we change things on the screen (e.g. moveBird, etc)
C. The entire code so far, that goes on the card:
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
lock screen
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
if the right of btn "city02" < 0 then
set the left of btn "city02" to the right of btn "city01" +3
if gameisRunning then
send "moveBird" to me in 20 millisecs
end if
end moveBird
on movePipes
lock screen
repeat with x = 1 to 3
set the right of btn ("top" &x) to the right of btn ("top" &x) - 4
set the right of btn ("bot" &x) to the right of btn ("bot" &x) -4
if the right of btn ("top" &x) < 0 then
set the left of btn ("top" &x) to the right of card "main"
set the left of btn ("bot" &x) to the right of card "main"
end if
end repeat
unlock screen
end movePipes
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