Game Development Reference
In-Depth Information
Creating Enemy Grid Movement
1. Take a brief look at the resources—there are four sprites there for a scorpion moving in
four directions, as well as a sprite called spr_reserved . This sprite will be used in an
object that keeps a grid position reserved once an enemy has decided to move there.
2. Create an object obj_impassable . This is going to be the parent object for all objects
that our scorpion enemies are not allowed to pass through. This is much easier than
creating conditions for every single object.
3. Create an object obj_reserved and set its Sprite to spr_reserved . We're going to let our
scorpions place this where they plan to walk, so that no other enemy can go there. Set
the Parent to obj_impassable and uncheck the Visible option.
4. In the same object, add a Create event and include a Set Alarm timer for just 5 Steps in
Alarm 0 .
5. Add an Alarm , Alarm 0 event and include a Destroy Instance action. This makes
obj_reserved kill itself after a few steps, which is more than enough to allow the
instance that placed it there to start moving in that direction and occupy the spot.
6. Double-click obj_wall and set the Parent to obj_impassable .
7. Now create an object obj_scorpion and assign it any of the scorpion sprites. Set the
Parent to obj_impassable .
8. Let's give each scorpion a random start position in the grid. Add a Create event and
include an Execute Code action. Insert the following lines:
1: {
2: do move_random(32,32)
3: until ( place_empty(x,y) && distance_to_object(obj_explorer) > 128 );
4: }
The move_random function does the simple task for us of putting the instance in a
position that aligns to our grid of 32x32. However, we need to make sure it is an empty
place and reasonably far away from the explorer, which is why we do repeat the
placement until the instance placed there meets no other instance and is more than
128 pixels away (5 grid moves). You may guess you shouldn't fill up the game with too
many instances, as it may take Game Maker long before it finds a free spot.
 
Search WWH ::




Custom Search