Objects Popping Up EverywhereIf you have a static game where you move the player object using arrow keys or the mouse button, you can add random movements of other objects to add to the action. For instance if your game is moving a person through a maze, you can add excitement by have a diamond, a bomb or a black hole pop-up randomly in different places as you work your way through the maze.
It is more exciting if you are chasing something, being chased or have to look out for bombs popping up in front of you.
First you need code to pop it up in different locations. If your screen is 400 x 600 pixels, you want to generate random locations in that range. This would be done with the following code:
put random(400) into x put random(600) into y set loc of graphic “diamond” to x,y put random(600) into y set loc of graphic “diamond” to x,y
Open up the Property Inspector for your card, click on “Size and Position” and see what the size is of your card (screen). Then use those numbers in the code above instead of mine (400 and 600) There are many places to put this code.One way, is to tie it into the movement of your player object. After you move, you place the object then check for the intersect of your player and the object. on arrowkey k if k = “Down” then …. end if if k = “Down” then …. end if put random(400) into x put random(600) into y set loc of graphic “diamond” to x,y put random(600) into y set loc of graphic “diamond” to x,y if intersect (….) then ... end if
end arrowkey ... end if end arrowkey
The only problem is that the object moves each time with the player and you have no chance to chase it or avoid it. A better way is to have the object move every time interval itselfYou could create a self-running function which waits 2 seconds then sends itself a message. It then moves the object, waits again 2 seconds then sends itself a message. This goes on forever once you start it.
Add the following code to your Card Script, below all your other code. on moveObj put random(400) into x put random(600) into y set loc of graphic “diamond” to x,y
send “moveObj” to me in 2 seconds end moveObj put random(400) into x put random(600) into y set loc of graphic “diamond” to x,y send “moveObj” to me in 2 seconds end moveObj
To start it, either add a button which sends it the first message “moveObj” or add the message when you open the card on openCard moveObj end openCard moveObj end openCard A. Stopping it !! (Very Important) - if you already have a start button and other moving objects:
You want it to stop after the game is over or whenever you go to another level (card). If you don't stop it, it will run on forever, or worse - cause your game to stop with an error.
( When you go to another card, it may not have a "moveObj" message handler. So the message "moveObj" will be sent to the new card in 2 seconds and it will not know what to do.) So we need to tell it when to stop sending itself the message. We use a variable for this and set it to either true or false. If you already have moving objects and created a variable to tell if the game is still on, then you can use that variable. Let's say it was "playGame
Add on the card script at the top: Add on the card script in the openCard handler: put true into playGame moveObj end openCard Add on the card script in your moveObj handler: set loc of graphic “diamond” to x,y send “moveObj” to me in 2 seconds Add on the card script whenever you stop the game or go to another card: B. Stopping it !! - from scratchYou want it to stop after the game is over or whenever you go to another level (card). If you don't stop it, it will run on forever, or worse - cause your game to stop with an error. ( When you go to another card, it may not have a "moveObj" message handler. So the message "moveObj" will be sent to the new card in 2 seconds and it will not know what to do.)
So we need to tell it when to stop sending itself the message. We use a variable for this and set it to either true or false.
Add on the card script at the top:
global stillGoing
Add on the card script in the openCard handler:
on openCard put true into stillGoing moveObj end openCard Add on the card script in your moveObj handler:
command moveObj if stillGoing then put random(400) into x put random(600) into y set loc of graphic “diamond” to x,y send “moveObj” to me in 2 seconds end if command moveObj
Add on the card script whenever you stop the game or go to another card: put false into stillGoing
NEXT - Disappearing Objects
|
|