Skip to main content
Participating Frequently
February 22, 2014
Question

Help! How to Create Wall Barrier for Maze Game

  • February 22, 2014
  • 1 reply
  • 7894 views

Hi - I'm working on a simple maze program that has ball which is controlled by the up, down. left, right arrow keys.  I made a maze image in Photoshop and brought it into the Flash CS6 and CC (AS3). I made one wall barrier called wallDet1_mc to test before creating other wall barriers. It doesn't work. The ball goes through the wall everytime I test it.  Any help on this would be greatly appreciated.  If anyone has a basic maze with arrow keys control where their object do not cross over the wall please let me know how to do this. Thanks.

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
February 22, 2014

Show the code you tried that does not work, and explain what element is what in that code.

IzzanatorAuthor
Participating Frequently
February 22, 2014

Thanks Ned. At this point, the program is still in its infancy because I cannot get past this point .  Here is what I have so far:

import flash.events.KeyboardEvent;

stop();

/* Move with Keyboard Arrows

Allows the specified symbol instance to be moved with the keyboard arrows.

Instructions:

1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.

Note the number 5 appears four times in the code below.

*/

wallDet1_mc.enabled=false;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

carP_mc.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_3);

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);

//stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function fl_MoveInDirectionOfKey_3(event:Event)

{

    if (upPressed)

    {

        carP_mc.y -= 5;

    }

    if (downPressed)

    {

        carP_mc.y += 5;

    }

    if (leftPressed)

    {

        carP_mc.x -= 5;

    }

    if (rightPressed)

    {

        carP_mc.x += 5;

    }

}

function fl_SetKeyPressed_3(event:KeyboardEvent):void

{

    switch (event.keyCode)

    {

        case Keyboard.UP:

        {

            upPressed = true;

            break;

        }

        case Keyboard.DOWN:

        {

            downPressed = true;

            break;

        }

        case Keyboard.LEFT:

        {

            leftPressed = true;

            break;

        }

        case Keyboard.RIGHT:

        {

            rightPressed = true;

            break;

        }

    }

}

function fl_UnsetKeyPressed_3(event:KeyboardEvent):void

{

    switch (event.keyCode)

    {

        case Keyboard.UP:

        {

            upPressed = false;

            break;

        }

        case Keyboard.DOWN:

        {

            downPressed = false;

            break;

        }

        case Keyboard.LEFT:

        {

            leftPressed = false;

            break;

        }

        case Keyboard.RIGHT:

        {

            rightPressed = false;

            break;

        }

    }

    }

function everyFrame(event:Event):void {

var mazehit:Boolean = false;

if (upPressed) {

//for(var i:int = 0; i < speed; i++) {

if(wallDet1_mc.hitTestPoint(carP_mc.x, carP_mc.y, true)) {

wallDet1_mc.x += 5;

mazehit = true;

//break;

}

}

if (downPressed) {

//for(var i:int = 0; i < speed; i++) {

if(wallDet1_mc.hitTestPoint(carP_mc.x, carP_mc.y, true)) {

wallDet1_mc.x += 5;

mazehit = true;

//break;

}

}

if (leftPressed) {

//for(var i:int = 0; i < speed; i++) {

if(wallDet1_mc.hitTestPoint(carP_mc.x, carP_mc.y, true)) {

wallDet1_mc.x += 5;

mazehit = true;

//break;

}

}

if (rightPressed) {

//for(var i:int = 0; i < speed; i++) {

if(wallDet1_mc.hitTestPoint(carP_mc.x, carP_mc.y, true)) {

wallDet1_mc.x += 5;

mazehit = true;

//break;

}

}

}

My movie symbols are Maze, WallDet1, Car_P

instance names: maze_mc, wallDet1_mc, carP_mc

Hopeful this helps you Ned.  Thanks again for your help.

Ned Murphy
Legend
February 22, 2014

I think you probably want to make use of hitTestObject rather than hitTestPoint.  Otherwise you are testing for a collision with just one specific point of the car mc.

You do need to have the everyFrame function getting executed in order for anything in it to work.

I don't see where you have anything that would stop the car from going thru the wall.  I see where you appear to nudge the wall to the right anytime it gets hit, but that will not stop the car from moving.  I think you want to make the car take a step back instead of moving the wall, and it should happen in the opposite direction as it came from.

If you intend to have the same speed all around you should use a variable to set it rather than having the number appear everywhere.  That way when you want to change it to be faster or slower you just change that one variable rather than every place you have that number.  That way you could change it while playing the game if desired as well.