Quick Tip: Mimic the Boo’s Movement From Super Mario Bros.

Quick Tip: Mimic the Boo’s Movement From Super Mario Bros.

Tutorial Details
  • Difficulty: Beginner
  • Platform: Flash (Flash Player 9+)
  • Language: AS3
  • Software Used: Flash Professional CS3+
  • Estimated Completion Time: 15 minutes

In this Quick Tip you’ll learn a technique of conditional enemy movement, based on a classic enemy from an awesome game. Face the ghost and he freezes; look away and he comes for you.


Final Result Preview

Let’s take a look at the final result we will be working towards:

Use the left and right arrow keys to move. Walking movement has been simplified for the sake of this Quick Tip.


Step 1: Brief Overview

A player and an enemy will be placed on screen, the player will be controlled using the left and right arrow keys and the enemy will react to the player’s position and orientation.


Step 2: Set Up Your Flash File

Launch Flash and create a new Flash Document, set the stage size to 320x200px and the frame rate to 24fps.


Step 3: Interface

This is the interface we’ll be using, simple shapes and colors to recreate this behavior.

Convert the characters to Movie Clips and name them ghost and player.


Step 4: ActionScript

Create a new ActionScript Class (Cmd+N), save the file as Main.as and write the following lines, please read the comments in the code to fully understang the class behavior.

package
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.Event;

	public final class Main extends Sprite
	{

		public final function Main():void
		{
			addListeners();
		}

		private final function addListeners():void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, movePlayer);
			stage.addEventListener(Event.ENTER_FRAME, follow);
		}

		private final function movePlayer(e:KeyboardEvent):void
		{
			if(e.keyCode == 37) //move left if left arrow key is pressed
			{
				player.x -= 4;
				player.rotationY = 180; //rotate to match direction
			}
			else if(e.keyCode == 39) //move right if right arrow key is pressed
			{
				player.x += 4;
				player.rotationY = 0; //rotate to match direction
			}
		}

		private final function follow(e:Event):void
		{
			/* Right side */

			if(player.rotationY == 0 && player.x > ghost.x)
			{
				ghost.x += 0.4;
				ghost.y += 0.4;
				ghost.alpha = 1;
				ghost.rotationY = 0;
			}
			else if(player.rotationY == 180 && player.x > ghost.x) //if the player looks at the ghost
			{
				ghost.alpha = 0.5;
			}

			/* Left side */

			if(player.rotationY == 180 && player.x < ghost.x)
			{
				ghost.x -= 0.4;
				ghost.y += 0.4;
				ghost.alpha = 1;
				ghost.rotationY = 180;
			}
			else if(player.rotationY == 0 && player.x < ghost.x) //if the player looks at the ghost
			{
				ghost.alpha = 0.5;
			}

			/* Stop Y if ghost is near floor */

			if(ghost.y >= 165)
			{
				ghost.y = 165;
			}
		}
	}
}

Step 5: Document Class

Add the class name to the Class field in the Publish section of the Properties panel to associate the FLA with the Main document class.


Conclusion

You’ve learned a simple and useful technique of enemy movement, try it in your own games!

I hope you liked this tutorial, thank you for reading!

Add Comment

Discussion 2 Comments

  1. dedé says:

    Nice article,
    the same effect but less lines :

    package
    {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.Event;

    public final class Main extends Sprite
    {

    private var _ghostvelocity : Number = 0.4;

    public final function Main():void
    {
    addListeners();
    }

    private final function addListeners():void
    {
    stage.addEventListener(KeyboardEvent.KEY_DOWN, movePlayer);
    stage.addEventListener(Event.ENTER_FRAME, follow);
    }

    private final function movePlayer(e:KeyboardEvent):void
    {
    if(e.keyCode == 37) //move left if left arrow key is pressed
    {
    player.x -= 4;
    player.rotationY = 180; //rotate to match direction
    }
    else if(e.keyCode == 39) //move right if right arrow key is pressed
    {
    player.x += 4;
    player.rotationY = 0; //rotate to match direction
    }
    }

    private final function follow(e:Event):void
    {
    if(player.x > ghost.x)
    {
    ghost.x += 0.4;
    ghost.rotationY = 0;
    }else{
    ghost.x -= 0.4;
    ghost.rotationY = 180;
    }

    ghost.y >= 165 ? ghost.y = 165 : ghost.y += 0.4;

    if(ghost.hitTestPoint(player.x,player.y,true))
    {
    stage.removeEventListener(Event.ENTER_FRAME, follow);
    ghost.alpha = .5;
    }
    }
    }
    }

  2. Jordan says:

    Cool tutorial! I was bored, so I enabled the up and down arrows and then created a new global variable called “heightFactor”, which allows the ghost to follow in all directions:

    if(player.y > ghost.y)
    {
    heightFactor = 1;
    }
    else
    {
    heightFactor = -1;
    }

    and then later when setting the ghost’s y:

    ghost.y += heightFactor * 0.4;

    Just a tip for anyone else wondering if it was possible. :)

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.