Lingo Game: Controlling with Keys (Pacman-like) READING: Manual pages of the commands described here. File "Pacman.dir"shows
how to move a character using the arrow keys.
on keyDown
put _key.keyCode
x = sprite(1).locH -- remember the horizontal location of a sprite
put x -- just to track down its location
case (_key.keyCode) of
43, 123: -- < means go to the left
sprite("ball").locH = sprite("ball").locH - 5
47, 124: -- > means go to the right
sprite("ball").locH = sprite("ball").locH + 5
end case
end
shows how to control its location on the stage. The "ball" can move in all four directions and can recognize the screen boundaries:
on keyDown
case (_key.keyCode) of
123: -- left
sprite("ball").locH = sprite("ball").locH - 5
125: -- right
sprite("ball").locV = sprite("ball").locV + 5
126: -- up
sprite("ball").locV = sprite("ball").locV - 5
124: -- down
sprite("ball").locH = sprite("ball").locH + 5
end case
checkLocation
_movie.updateStage()
end
on checkLocation -- IT ASSUMES THAT THE SCREEN IS 320 BY 240
-- make sure the ball doesn't fall off the edge of the screen
if sprite("ball").locH < 10 then sprite("ball").locH = 10
if sprite("ball").locH > 310 then sprite("ball").locH = 310
if sprite("ball").locV < 10 then sprite("ball").locV = 10
if sprite("ball").locV > 230 then sprite("ball").locV= 230
end
sprite("ball").constraint = 1 -- stay within sprite 1
where sprite 1 is another sprite, a rectangle that you want to use as the constraining area (you have to create it first).
on keyDown
case (_key.keyCode) of
123: chompLeft
125: chompRight
126: chompUp
124: chompDown
end case
eatFood
_movie.updateStage()
end
sprite("chomper").intersects("foodball") = 1 -- check if they intersect
Now you can quickly check if the chomper touches any of the foodballs using a loop. Note, however, that this implementation makes the foodball disappear, but it is still there, and we can hear the chomping sound when our pacman is intersecting it. Can you fix this, so that it moves out of the way instead of disappearing?
|
|
|
Maintained By: Takis Metaxas
|
|