Skip to main content
Participating Frequently
February 22, 2014
Question

Help! How to Create Wall Barrier for Maze Game

  • February 22, 2014
  • 1 reply
  • 7878 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
Brainiac
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.

IzzanatorAuthor
Participating Frequently
February 22, 2014

Not that it matters greatly, but just for the sense of what's moving and what isn't I would be doing a car.hitTest(wall) rather than a wall.hitTest(car) since the car is the moving object.

If you do not want the wall to move then why do you tell it to move ( wallDet1_mc.x += 5; ) ?

As I already indicated, if you don't want the car to move past the wall then you need to make it move back to where it came from anytime it hits the wall...

if (leftPressed) {

   if(carP_mc.hitTestObject(wallDet1_mc)) {

        wallDet1_mc.x += 5;

        carP_mc.x += 5   // for rightPressed use -= 5, etc

        mazehit = true;

   }

}


I am sincerely sorry about that. I've been staring at this code so long that I didn't see it.  Thanks , I'll make the changes and get back to you ASAP.  thanks again.