# Maze Behaviors

February 22, 1999
by Pat McClellan

Dear Multimedia Handyman,

I'm trying to make a game where a sprite follows the user's mouse clicks through a maze. I'm new at this, so I really don't know what I'm doing or the best way to make it move. Can you help please?

Steven Agalizi

Dear Steven,

The easy part of this is getting a sprite to follow the mouseClicks around. To do that, we simply need to compare the current location of the sprite to that of the mouseClick, then move the sprite incrementally toward that click until it reaches it. Let's create a behavior called FollowClick. We'll need to provide for properties to hold the location of the mouse click, plus a Flag which will act as a switch to turn the motion on and off. When the mouse is clicked, the behavior will store the click location and turn the switch on. Then, in each subsequent frame, the sprite will move 10 pixels horizontally and vertically toward the click location. Note that since we're moving equal amounts vertically and horizontally, the sprite will only be able to move at 45 degree angles -- or direct horizontal or vertical lines.

```
-- FollowClick Behavior
-- copyright © 1999, ZZP Online, LLC
property pClickH, pClickV, pFlag, pMySprite, pLastLoc
on beginSprite me
set pMySprite = the spriteNum of me
set pLastLoc = the loc of sprite pMySprite
set pFlag = #stop
end
on mouseClick
set pClickH = the mouseH
set pClickV = the mouseV
set pFlag = #go
end
on exitFrame me
if pFlag = #go then
set myLocH = the locH of sprite pMySprite
set mylocV = the locV of sprite pMySprite
set pLastLoc = the loc of sprite pMySprite
if pClickH > myLocH then
set the locH of sprite pMySprite to min(myLocH + 10, ¬
pClickH)
else if pClickH < myLocH then
set the locH of sprite pMySprite to max(myLocH - 10, ¬
pClickH)
end if
if pClickV > myLocV then
set the locV of sprite pMySprite to min(myLocV + 10, ¬
pClickV)
else if pClickV < myLocV then
set the locV of sprite pMySprite to max(myLocV - 10, ¬
pClickV)
end if
if myLocH = pclickH AND myLocV = pClickV then
set pFlag = #stop
put pFlag
end if
end if
end
on wallCollision me
set pFlag = #stop
set the loc of sprite pMySprite to pLastLoc
beep
end

```

The trickiest challenge here is determining when the sprite bumps into the wall of your maze. Without creating a complex list of coordinates for all the walls, I simply chose to make each wall segment a separate sprite. Then I applied the following behavior to each.

```
-- Collision behavior
-- copyright © 1999, ZZP Online, LLC
property pMySprite, pMover
on getPropertyDescriptionList me
set pdlist to [:]
addprop pdlist, #pMover, [#comment:"Which sprite moves?", ¬
#format:#integer, #default:1]
return pdlist
end getPropertyDescriptionList
on beginsprite me
set pMySprite = the spriteNum of me
end
on exitFrame me
set testRect = intersect(the rect of sprite pMover, the rect ¬
of sprite pMySprite)
if testRect > rect (0,0,0,0) then
sendSprite(pMover, #wallCollision)
end if
end

```

This behavior allows the author to specify which sprite is moving around the maze -- I call this sprite pMover. On each exitFrame, the behavior checks to see if the rect of pMover is intersecting with the rect of its own sprite (the wall segment). If so, it sends out a message to sprites pMover to notify it that there's been a collision. That's when that last handler of the first behavior (wallCollision) gets activated.

These will serve as a basic start for you. You can download the dir file and customize them as you need. (Mac or PC) Good luck with your program.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2018, Director Online. Article content copyright by respective authors.