Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Merge sort and Binary Search using Lingo
Cursor trails
CD-ROM Drive Letter Finder-Using a File
ADOXtra Lite
Drag Install
MasterApp
turn sprites on/off handlers
Rolling Ball
Bouncing Bevel Light-Alphamania
Build Environment
 

 

 

Article Making a simple game

Added on 9/20/1999

 

Compatibilities:
D7 D8 Mac PC Shockwave

This item has not yet been rated

Author: MediaMacros (website)

I was looking aver your new Learning Arcade and was wondering if you could give me some help on a simple game I am working on. It is one of the old style "light up" games where you have a grid of lights and clicking one toggles it on, and off along with its's neighbors. The objective is to turn them all on. I know how to swap a graphic but I am a bit stuck on the rest.

Get the source
This is the type of game that Director can really do well.  Swapping the graphic is only part of it.  We need to establish a pattern for how the grid is laid out as well as a check for the "all done" when all are lit.  Let's start with the grid.  For this example we will set up a 5X4 grid of "lights" in random states.  Director will need to know a few things...

1.  How many lights are in each row?

2.  What is the first sprite used.

3.  What is the last sprite used.

From this we can let Director "intelligently" calculate the sprites neighbors for toggling.  Simply lay out the rows left to right and then top to bottom with the default "on" graphic.  Name the 2 graphics with a common prefix (in this case it is "light") followed by a "-" and a 0 or 1 for the on and off states.  Our graphics would then be named "light-0"   (off) and "light-1" (on.)  Now that the score is set up lets start coding.

Each sprite will need to track for itself whether it is on or off.   For this we will set a property called toggleState.  In our beginSprite handler we use the random command to set this property, then we swap out the appropriate graphic.  When the user clicks one of the sprites it toggles the state.  We could set up a conditional statement where we check if it is 1 and set it to 0 or else if it is 0 set it to 1, but there is an easier way.  Since we are using 1 and 0 (which are synonymous to true and false) we can use the "not" command to swap them...

set toggleState = not(toggleState)

Now all we need to do is tell the neighbors to swap as well.  For a sprite in the middle, the lights on the left and right are pretty simple.  It is the spriteNum + 1 and the spriteNum - 1, but what happens if you are on the end and how do you get the sprites below or above?  For this we use a few if statements and the mod command.  Mod is basically the math equivalent of the remainder from long division.   4 mod 4 is 0.  3 mod 4 = 3. 5 mod 4 = 1.  From this we can figure out if a sprite is at the left of a line, the right of a line, etc.  The reason we do this is that if sprite 5 is on the end of one line, we don;t want it to toggle sprite 6, as it is not really "touching" it.

Once we do this we can use the first light and last light to find out if there is a light touching above or below the current one.  Simply take the current item and add the total per line or subtract the number per line to get the sprite below or above.

So how do you know if you win?  Well, after each click we use the sendAllSprites command with a list to return all the current values.  We then use getOne() to check the returned list to see if there are any "0"'s in it.   If so, then we know that some of the lights are still "off" and the game continues.  If not, we go to the "done" marker.  Below is the code...

--Copyright 1999 Chuck Neal
--chuck@mediamacros.com
--If you find this code helpful, send me an e-mail and let me know. :-)

property toggleState, spriteNum, myPrefix
global gFirstToggle, gLastToggle, gNumberPerLine, totalTries

on beginSprite me
  --change to match the number of items you have per line
  -------------------------------------------------
  if gNumberPerLine = void then gNumberPerLine = 5
  -------------------------------------------------
  if totalTries <> 0 then totalTries = 0
  if gFirstToggle = void then gFirstToggle = 999
  if gLastToggle = void then gLastToggle = 0
  toggleState = random(0,1)
  the itemDelimiter = "-"
  myPrefix = sprite(spriteNum).member.name.item[1]
  checkToggleState()
  if gFirstToggle > spriteNum then gFirstToggle = spriteNum
  if gLastToggle < spriteNum then gLastToggle = spriteNum
end

on mouseUp me
  totalTries = totalTries + 1
  toggleState = not(toggleState)
  --not the last on the right
  if ((spriteNum + 1) mod gNumberPerLine) <> (gFirstToggle mod gNumberPerLine) then
    sprite(spriteNum + 1).toggleState = not(sprite(spriteNum + 1).toggleState)
  end if
  --not the last on the left
  if ((spriteNum - 1) mod gNumberPerLine) <> (gLastToggle mod gNumberPerLine) then
    sprite(spriteNum - 1).toggleState = not(sprite(spriteNum - 1).toggleState)
  end if
  if (spriteNum + gNumberPerLine) <= gLastToggle then
    sprite(spriteNum + gNumberPerLine).toggleState = not( sprite(spriteNum + gNumberPerLine).toggleState)
  end if
  if (spriteNum - gNumberPerLine) >= gFirstToggle then
    sprite(spriteNum - gNumberPerLine).toggleState = not(sprite(spriteNum - gNumberPerLine).toggleState)
  end if
  sendAllSprites(#checkToggleState)
  updateStage
  set myList = []
  sendAllSprites(#checkForDone, myList)
  if myList.getOne(0) < 1 then
    go to "done"
  end if
end

on checkToggleState me
  if sprite(spriteNum).member <> member(myPrefix & "-" & string(toggleState)) then
    sprite(spriteNum).member = member(myPrefix & "-" & string(toggleState))
  end if
end

on checkForDone me, theList
  add theList, the toggleState of me
end

on getBehaviorDescription me
  describe = "This behavior controls a simple light up game."
  describe = describe & return & "Create 2 graphics, one for the on state, one for the off state, and label them with the same prefix followed by a (-) and either a 0 (off) or a 1 (on)."
  describe = describe & return & "For example...Name your graphics light-0 and light-1"
  describe = describe & return & "Arrange them in the score from left to right starting with the top row. If the first sprite is 1 then you would place the next graphic on the same row and to the right and in sprite channel 2"
  describe = describe & return & "The lights are then set randomly on the beginSprite handler and the lights are turned on."
  return describe
end

Want to add to it?  What about making a more interesting interface, animate the buttons turning on and off, add a 3d layer to it.  Let your imagination run.

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail