• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help! How to Create Wall Barrier for Maze Game

Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

7.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

I replaced the hitTestPoint with hitTestObject and the car_P object still go through the wall.  I wanted to keep the maze static and only have the car_P move by the arrow keys.  That part works but the test barrier (called wallDet1_mc) that I created is not working.  I made the barrier red so I can see it while I test it. Do you have a barrier coding that would work on this type of scenerio?  thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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;

   }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

Still doesn't work, and I was working on it and I cannot get the object (carP_mc) to not go through the wall. There has to be a code or math equation to stop it from doing this.  If the wall detection picks up the car at the wall then it should keep it away by the same amount of pixels.  What do you think?  Below is a revamped program and thanks again for your help:

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(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.y -= 5;

mazehit = true;

//break;

}

}

if (downPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.y += 5;

mazehit = true;

//break;

}

}

if (leftPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.x -= 5;

mazehit = true;

//break;

}

}

if (rightPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.x += 5;

mazehit = true;

//break;

}

}

}

//End of program. Not using the codes below

/**onClipEvent(enterFrame){

    if(this.hitArea(carP_mc._x,carP_mc._y, true)){                   

      

        carP_mc._x=carP_mc._x;

        carP_mc._y=carP_mc._y;

    }

}**/

//Keyboard Listener on stage

//stage.addEventListener(KeyboardEvent.KEY_DOWN, theKeysDown);

//stage.addEventListener(KeyboardEvent.KEY_UP, theKeysUp);

//Makes MazeArt follow the movement of the path_mc

/*addEventListener(Event.ENTER_FRAME, onNewFrame01);

function onNewFrame01(e:Event):void{

    maze_mc.x=wallDet1_mc.x;

    maze_mc.y=wallDet1_mc.y

}

}

function Car_P(e:KeyboardEvent):void

{

    //maze_mc.addEventListener(KeyboardEvent, wallDet1_mc);

}

    //wallDet1_mc.addEventListener(KeyboardEvent.KEY_DOWN, maze_mc);

*/

//}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

You are doing the opposite of what I just showed you to do.  If you just stepped += 5 in the x axis (rightPressed)and you hit a wall, why would you step another += 5?... etc....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

Sorry about that. I sent you the readjusted coding because after I changed every keyPressed the car_P did not move at all.  Below are the adjustments that you suggested:

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(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.y += 5;

mazehit = true;

//break;

}

}

if (downPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.y -= 5;

mazehit = true;

//break;

}

}

if (leftPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.x += 5;

mazehit = true;

//break;

}

}

if (rightPressed) {

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

if(carP_mc.hitTestObject(wallDet1_mc)) {

carP_mc.x -= 5;

mazehit = true;

//break;

}

}

}

/**onClipEvent(enterFrame){

    if(this.hitArea(carP_mc._x,carP_mc._y, true)){                   

      

        carP_mc._x=carP_mc._x;

        carP_mc._y=carP_mc._y;

    }

}**/

//Keyboard Listener on stage

//stage.addEventListener(KeyboardEvent.KEY_DOWN, theKeysDown);

//stage.addEventListener(KeyboardEvent.KEY_UP, theKeysUp);

//Makes MazeArt follow the movement of the path_mc

/*addEventListener(Event.ENTER_FRAME, onNewFrame01);

function onNewFrame01(e:Event):void{

    maze_mc.x=wallDet1_mc.x;

    maze_mc.y=wallDet1_mc.y

}

}

function Car_P(e:KeyboardEvent):void

{

    //maze_mc.addEventListener(KeyboardEvent, wallDet1_mc);

}

    //wallDet1_mc.addEventListener(KeyboardEvent.KEY_DOWN, maze_mc);

*/

//}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

forgot to mention that the car_P is not moving at all with the latest changes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

You are going to have to take the time to try to work thru your issues.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

yes, this problem is pretty tough. I have some ideas how to attack it next.  Thanks for trying to figure it out with me. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 31, 2017 Jul 31, 2017

Copy link to clipboard

Copied

Did you ever have any luck with is?  I have a student who is trying to achieve the same type of thing making a simple car racing game.

Cheers

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 14, 2017 Aug 14, 2017

Copy link to clipboard

Copied

LATEST

It is doubtful that the OP is going to reply - hasn't been around here since March 2014.  If you have an issue you would like help with, start your own posting explaining the problem and provide the code and maybe someone will drop in to try to help.  Posting with a lot of replies tend to fall out of attention.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

I think the problem is at the variable var mazehit: Boolean = false;

I have nothing assoicated with it. Perhaps if I change it to wallDet1hit. What do you think?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

is there anyway I could send you the .fla and Assets folder?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines