Just moving a player through a maze or around a screen can be a game but you may want to make it more exciting or dangerous by adding moving targets.
These are objects like enemies, flying birds, etc that move across the screen and ones that you should avoid. You can have them move across the screen like birds flying, down the screen like falling raindrops or diagonally across the screen like comets or asteroids.
First we we decide what direction they will move and write the code, then we have to decide where to put the code so that they move over and over on their own.
Finally, we should provide a way to stop it. Otherwise, it will keep on moving even when you go on to another level or another card. All games should have a check so that they do not run on forever
Adding your custom message ("moveObj") and code to move it
You make them go much like you did your player - by adding or subtracting a number from its position on the screen. We will put this code on the card stack - call it "moveObj" and it will be a "Command" that responds to our message "moveObj"
Code that goes on the Card script:
i.e.
--------------------------------------------------------
e.g.
command moveBird
(your code will go here)
end moveBird
In these examples - I used 10 but you can increase or decrease the amount depending on how fast or slow you want them to go.
Moving Horizontally (right to left)
Moving an object from right to left is simple, you just keep subtracting from it's x position until it reaches the left of the screen (zero) then start it again from the right of the screen.
Code example #1 - Moving a bird across the screen
Here's the code for a bird flying across the screen:
command moveBird
set the left of the button "bird1" to the left of the button "bird1" - 10
end moveBird
We have to check when it gets to the far left of the screen and goes out of sight. We then start it again on the right side.
This is the code to check when it goes off the card and start it back on the right side of the card
command moveBird
set the left of the button "bird1" to the left of the button "bird1" - 10
if the right of the button "bird1" < 0 then
set the left of the button "bird1" to the right of the card "level1"
end if end moveBird
note: here we use the "set" command to change the location of the bird
or
Here's the code for an enemy moving right to left across the screen:
on moveEnemy
set the left of button "enemy" to the left of button "enemy" - 5
if the left of the button "enemy" < 0 then
set the left of the button "enemy" to the right of the card "level 1"
end if
end moveEnemy
What Direction will They Move?
You make them go much like you did your player - by adding or subtracting a number from its position on the screen. We will put this code on the card stack - call it "moveObj" and it will be a "Command" that responds to our message "moveObj"
1. Moving Horizontally (right to left)
Moving an object from right to left is simple, you just keep subtracting from it's x position until it reaches the left of the screen (zero) then start it again from the right of the screen.
Code examples #1 - Using the "Set" command
Here's the code for a bird flying across the screen:
command moveObj
set the left of the button "bird1" to the left of the button "bird1" - 10
if the right of the button "bird1" < 0 then
set the left of the button "bird1" to the right of the card "level1"
end if end moveObj
note: here we use the "set" command to change the location of the bird
Here's the code for an enemy moving right to left across the screen:
set the left of button "enemy" to the left of button "enemy" - 5
if the left of the button "enemy" < 0 then
set the left of the button "enemy" to the right of the card "level 1"
Code example #2 - Using the "Move" command (this is sometimes slower than the set command above)
Here's another way of doing the code for a bird flying across the screen:
command moveObj
move the button "bird1" relative -10,0
if the right of the button "bird1" < 0 then
set the left of the button "bird1" to the right of the card "level1"
note:
in the "move" statement we subtract 10 from the x-value and 0 from the
y-value of it's location. We want it to move horizontally across the
screen, not up or down.
2. Moving Vertically (top to bottom)
Moving an object from top to bottom is also simple, you just keep adding to it's y position until it reaches the bottom of the screen (zero) then start it again from the top of the screen.
Here's the code for a bird flying across the screen:
move the button "bird1" relative 0,+10
if the top of the button "bird1" > the bottom of the card "level1" then
set the bottom of the button "bird1" to the top of the card "level1"
note: in the "move" statement we add 10 to the y-value of the location and leave the x-value the same (add 0). We want it to move vertically down the screen, not left or right
Here's another way of doing the code for a bird flying across the screen:
set the top of the button "bird1" to the top of the button "bird1" + 10
if the top of the button "bird1" > the bottom of the card "level1" then
set the bottom of the button "bird1" to the top of the card "level1"
note: here we use the "set" command to change the location of the bird
2. Moving Diagonally Across the Screen
Moving an object from top to bottom and left to right is more involved as you add to both the x and the y positions until it goes off the screen either at the bottom or the right then start it again from the top of the screen.
Here's the code for a bird flying across the screen:
move the button "bird1" relative +10,+10
if the top of the button "bird1" > the bottom of the card "level1" then
set the bottom of the button "bird1" to the top of the card "level1"
if the right of the button "bird1" > the right of the card "level1" then
set the right of the button "bird1" to the left of the card "level1"
note: in the "move" statement we add 10 to the y-value of the location and add +10 to the the x-value. We want it to move vertically and horizontally on the screen.
Making Them Move On Their Own
Now the hard part, making them move without a game loop.
Remember we are writing handlers (code) that run when they receive a message. Ours responds to the message "moveObj". So why don't we have it send itself a message as the last thing that it does? So it will run, then send itself a message, then run again, then send itself a message, .....over and over
So let's add the line: send "moveObj" to me in 1 seconds to our new command - moveObj
(your code to move it is here)
send "moveObj" to me in 1 secs
end moveObj
Now the only questions are how do we start it moving and how do we stop it?
Making Them Move On Their Own
Now the hard part, making them move without a game loop.
Starting it Up The First Time
To get it going the first time, we add the code (the message) to the script of the "Start" button
on mouseUp
moveObj
end mouseUp
or to the card script in the "OpenCard" message
on openCard
moveObj
end openCard
Now we get to the most important part of it - How do you stop it?
Stopping It - 2 Ways
This is very important. We need a way to turn it off, to stop it. Otherwise, the messages go on forever which is not good when you go to another level of your game. Gradually, those messages will slow your game down and maybe even hang it.
1. Stop and Start Buttons
In fact, every game should have a Start and Stop button so that you can prevent things from happening even when the game is over.
To do this, we need a variable to tell us that the game is stopped or running. Then everywhere we do something, we check if the game is still running or not.
We start with a variable which we will call it "gameRunning" (You can name your variable anything that you want.)
We put this statement at the beginning of the card script so every handler on the card can use it:
global gameRunning
and set it false when we start up and first open the card: (this also goes into the card script
on openCard
put false into gameRunning
end openCard
We set it true when the game starts
on startGame
put true into gameRunning
end startGame
We set it false when the game stops
put false into gameRunning
Then we change the code that we already have in place to check if the game is running before we do anything:
e.g.
set the top of the button "bird1" to the top of the button "bird1" + 10
if the top of the button "bird1" > the bottom of the card "level1" then
set the bottom of the button "bird1" to the top of the card "level1"
send "moveObj" to me in 1 secs
Now it will check if the game is still running before doing anything.
- If the game is still running - it will move the object and do it again in 2 seconds
- If the game is not running - it will do nothing
Note: This is always a good idea to have something like this in every game
2. Canceling the messages
This is easy to do. Once you stop the messages, the cycle ends
A. Cancel the last message.
Add a "Stop" button with the script:
on mouseUp
cancel item 1 of last line of the pendingMessages
end mouseUp
B. Cancel all the messages.
If you have more than 1 moving "enemy", you have to stop all messages
Add a "Stop" button with the script:
on mouseUp
put the pendingmessages into tPendingMsgs
repeat FOR each line x in tPendingMsgs
cancel item 1 of x
end REPEAT
end mouseUp
on mouseUp
repeat with x=1 to the number of lines of the pendingMessages
cancel item 1 of line x the pendingMessages
end repeat
end mouseUp
C. Cancel only that particular message.
Add a global to save the message ID and delete that message when needed
Instead of just the line in the script:
(your code to move it is here)
send "moveObj" to me in 2 seconds
Substitute the following lines in the script:
(your code to move it is here)
send "moveObj" to me in 2 secs
put the result into lastMessage
Then in the "Stop" button put the following script
on mouseUp
global lastMessage
cancel lastMessage
end mouseUp