17 Replies Latest reply on Feb 24, 2014 5:13 PM by kglad

    Need to Create a Wall Barrier and Detection for Maze Game

    Izzanator Level 1

      Hi, I have a basic maze that I created the artwork in Photoshop and brought it into Flash as a PNG file.  My trouble is that my car object is going through the wall.  Could someone help me on correcting this.  I've been trying for hours to fix this but was unsuccessful.  I'm hoping someone out there has some advice on how to get it to work.  Thanks in advance.

       

       

      Iz

        • 1. Re: Need to Create a Wall Barrier and Detection for Maze Game
          kglad Adobe Community Professional & MVP

          to start, it won't be easy to detect a will in a bitmap.  it's doable but more complex than using a vector wall that you convert to a movieclip and use hitTestObject between your car (presumably already a movieclip).

          • 2. Re: Need to Create a Wall Barrier and Detection for Maze Game
            Izzanator Level 1

            should I put it in Illustrator and change it to vector?  (I think Photoshop could do this to, perhaps)

            • 3. Re: Need to Create a Wall Barrier and Detection for Maze Game
              kglad Adobe Community Professional & MVP

              ps is strictly a bitmap program.  you can use illustrator but there's no need: flash can convert your bitmap to a vector.

               

              click insert > new symbol > movieclip > ok > add your bitmap to the movieclip's stage > (and while it's still selected) > click modify > bitmap > trace bitmap.  (you can experiment with the settings to see how quality and file size are related).

               

              you can now select elements of your vector bitmap (like walls if your bitmap was the race track) and convert the walls to movieclips.  or you may even be able convert just one wall to a movieclip and then replace the vector walls with that library symbol resized and rotated.  assign each wall movieclip an instance name and you are ready to code for hittests.

              1 person found this helpful
              • 4. Re: Need to Create a Wall Barrier and Detection for Maze Game
                Izzanator Level 1

                Thanks, I'll convert it right now and use the bottom wall as a test until I get the barrier to work.  I'll send you a reply once I make the maze a vector and convert the bottom maze as a movieclip and instance name and start the code.  Thanks again.

                 

                Iz

                • 6. Re: Need to Create a Wall Barrier and Detection for Maze Game
                  Izzanator Level 1

                  Hi - thanks for the advice to use the trace bitmap. The only way I could get this option to stop being grayed out was to use the Break Apart option. Once I did that I was able to run a trace on the image and now I have the maze in segments. I convert each one into symbols and assign them each an instance name. I have a total of 10 segments that i named WallDet1 thru 10, along with an instance name of wallDet1_mc thru wallDet10_mc.

                   

                  I've had some luck on the coding to on some of the walls but not others.  The car still goes thru the wall at other segments.  Here is the coding for it.  Forgive me on some of the wall segments, I am still troubleshooting it 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;

                  wallDet2_mc.enabled= false;

                  wallDet3_mc.enabled= false;

                  wallDet4_mc.enabled= false;

                  wallDet5_mc.enabled= false;

                  wallDet6_mc.enabled= false;

                  wallDet7_mc.enabled= false;

                  wallDet8_mc.enabled= false;

                  wallDet9_mc.enabled= false;

                  wallDet10_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))

                  if(carP_mc.hitTestObject(wallDet2_mc))

                  if(carP_mc.hitTestObject(wallDet3_mc))

                  if(carP_mc.hitTestObject(wallDet4_mc))

                  if(carP_mc.hitTestObject(wallDet5_mc))

                  if(carP_mc.hitTestObject(wallDet6_mc))

                  if(carP_mc.hitTestObject(wallDet7_mc))

                  if(carP_mc.hitTestObject(wallDet8_mc))

                  if(carP_mc.hitTestObject(wallDet9_mc))

                  if(carP_mc.hitTestObject(wallDet10_mc))  

                  {

                  carP_mc.y += 5;

                  mazehit = true;

                   

                  //break;

                  }

                  }

                  if (downPressed) {

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

                  if(carP_mc.hitTestObject(wallDet1_mc))

                  if(carP_mc.hitTestObject(wallDet2_mc))

                  if(carP_mc.hitTestObject(wallDet3_mc))

                  if(carP_mc.hitTestObject(wallDet4_mc))

                  if(carP_mc.hitTestObject(wallDet5_mc))

                  if(carP_mc.hitTestObject(wallDet6_mc))

                  if(carP_mc.hitTestObject(wallDet7_mc))

                  if(carP_mc.hitTestObject(wallDet8_mc))

                  if(carP_mc.hitTestObject(wallDet9_mc))

                  if(carP_mc.hitTestObject(wallDet10_mc)) 

                  {

                  carP_mc.y -= 5;

                  mazehit = true;

                  //break;

                  }

                  }

                  if (leftPressed) {

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

                  if(carP_mc.hitTestObject(wallDet1_mc))

                  if(carP_mc.hitTestObject(wallDet2_mc))

                  if(carP_mc.hitTestObject(wallDet3_mc))

                  if(carP_mc.hitTestObject(wallDet4_mc))

                  if(carP_mc.hitTestObject(wallDet5_mc))

                  if(carP_mc.hitTestObject(wallDet6_mc))

                  if(carP_mc.hitTestObject(wallDet7_mc))

                  if(carP_mc.hitTestObject(wallDet8_mc))

                  if(carP_mc.hitTestObject(wallDet9_mc))

                  if(carP_mc.hitTestObject(wallDet10_mc))  {

                  carP_mc.x += 5;

                  mazehit = true;

                   

                  //break;

                  }

                  }

                  if (rightPressed) {

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

                  if(carP_mc.hitTestObject(wallDet1_mc))

                  if(carP_mc.hitTestObject(wallDet2_mc))

                  if(carP_mc.hitTestObject(wallDet3_mc))

                  if(carP_mc.hitTestObject(wallDet4_mc))

                  if(carP_mc.hitTestObject(wallDet5_mc))

                  if(carP_mc.hitTestObject(wallDet6_mc))

                  if(carP_mc.hitTestObject(wallDet7_mc))

                  if(carP_mc.hitTestObject(wallDet8_mc))

                  if(carP_mc.hitTestObject(wallDet9_mc))

                  if(carP_mc.hitTestObject(wallDet10_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);

                  */

                  //}

                  • 7. Re: Need to Create a Wall Barrier and Detection for Maze Game
                    kglad Adobe Community Professional & MVP

                    everytime the car changes position you should execute a carP_mc.hitTestObject() with each of your wall segments.  ie, change position, then execute the hittests.  if the hittest is positive, return the car to its position prior to the hittest.

                     

                    and your if-statments are screwy.  use, for example

                     

                    if (rightPressed) {

                    mazehit=false

                    for(i = 0; i < speed; i++) {

                    if(carP_mc.hitTestObject(this["wallDet"+i+"_mc"])){

                    wallhitBool=true;

                    break;

                    }

                    if(mazehit){

                    carP_mc.x -= 5;

                    }

                    }

                    1 person found this helpful
                    • 8. Re: Need to Create a Wall Barrier and Detection for Maze Game
                      Izzanator Level 1

                      LOL... I agree.  My if statements are screwy...(:

                       

                      I just put multiple if statements because I was getting frustrated with it.

                       

                      I'll clean up my coding and put in you suggested codes and get back with you. I think it will work since its counting up the walldets. I am crossing my fingers and toes now...(:

                       

                      chat with u soon.

                      • 9. Re: Need to Create a Wall Barrier and Detection for Maze Game
                        Izzanator Level 1

                        Hi - I put in the codes for the upPressed (to test it) and these are the errors that came up. 

                         

                        Scene 1, Layer 'Actions', Frame 1, Line 1101120: Access of undefined property speed.
                        Scene 1, Layer 'Actions', Frame 1, Line 1101120: Access of undefined property i.
                        Scene 1, Layer 'Actions', Frame 1, Line 1101120: Access of undefined property i.
                        Scene 1, Layer 'Actions', Frame 1, Line 1141120: Access of undefined property wallhitBool.
                        Scene 1, Layer 'Actions', Frame 1, Line 1121120: Access of undefined property i.
                        Scene 1, Layer 'Actions', Frame 1, Line 1101120: Access of undefined property i.

                         

                        Here is the updated program:

                         

                        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;

                        wallDet2_mc.enabled= false;

                        wallDet3_mc.enabled= false;

                        wallDet4_mc.enabled= false;

                        wallDet5_mc.enabled= false;

                        wallDet6_mc.enabled= false;

                        wallDet7_mc.enabled= false;

                        wallDet8_mc.enabled= false;

                        wallDet9_mc.enabled= false;

                        wallDet10_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) {

                         

                        mazehit=false

                         

                        for(i = 0; i < speed; i++) {

                         

                        if(carP_mc.hitTestObject(this["wallDet"+i+"_mc"])){

                         

                        wallhitBool=true;

                         

                        break;

                         

                        }

                         

                        if(mazehit){

                         

                        carP_mc.x -= 5;

                         

                        }

                        }

                        }

                        if (downPressed) {

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

                        if(carP_mc.hitTestObject(wallDet1_mc))

                        if(carP_mc.hitTestObject(wallDet2_mc))

                        if(carP_mc.hitTestObject(wallDet3_mc))

                        if(carP_mc.hitTestObject(wallDet4_mc))

                        if(carP_mc.hitTestObject(wallDet5_mc))

                        if(carP_mc.hitTestObject(wallDet6_mc))

                        if(carP_mc.hitTestObject(wallDet7_mc))

                        if(carP_mc.hitTestObject(wallDet8_mc))

                        if(carP_mc.hitTestObject(wallDet9_mc))

                        if(carP_mc.hitTestObject(wallDet10_mc)) 

                        {

                        carP_mc.y -= 5;

                        mazehit = true;

                        //break;

                        }

                        }

                        if (leftPressed) {

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

                        if(carP_mc.hitTestObject(wallDet1_mc))

                        if(carP_mc.hitTestObject(wallDet2_mc))

                        if(carP_mc.hitTestObject(wallDet3_mc))

                        if(carP_mc.hitTestObject(wallDet4_mc))

                        if(carP_mc.hitTestObject(wallDet5_mc))

                        if(carP_mc.hitTestObject(wallDet6_mc))

                        if(carP_mc.hitTestObject(wallDet7_mc))

                        if(carP_mc.hitTestObject(wallDet8_mc))

                        if(carP_mc.hitTestObject(wallDet9_mc))

                        if(carP_mc.hitTestObject(wallDet10_mc))  {

                        carP_mc.x += 5;

                        mazehit = true;

                         

                        //break;

                        }

                        }

                        if (rightPressed) {

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

                        if(carP_mc.hitTestObject(wallDet1_mc))

                        if(carP_mc.hitTestObject(wallDet2_mc))

                        if(carP_mc.hitTestObject(wallDet3_mc))

                        if(carP_mc.hitTestObject(wallDet4_mc))

                        if(carP_mc.hitTestObject(wallDet5_mc))

                        if(carP_mc.hitTestObject(wallDet6_mc))

                        if(carP_mc.hitTestObject(wallDet7_mc))

                        if(carP_mc.hitTestObject(wallDet8_mc))

                        if(carP_mc.hitTestObject(wallDet9_mc))

                        if(carP_mc.hitTestObject(wallDet10_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);

                        */

                        //}

                        • 10. Re: Need to Create a Wall Barrier and Detection for Maze Game
                          Izzanator Level 1

                          Oops! Forgot to ask, how can I clear these errors up?  Thanks.

                          • 11. Re: Need to Create a Wall Barrier and Detection for Maze Game
                            kglad Adobe Community Professional & MVP

                            replace all your code with:

                             

                            var upPressed: Boolean = false;

                            var downPressed: Boolean = false;

                            var leftPressed: Boolean = false;

                            var rightPressed: Boolean = false;

                            var i: int;

                            var mazehit: 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);

                            this.addEventListener(Event.ENTER_FRAME, everyFrame);

                             

                            function everyFrame(event: Event): void {

                                if (upPressed) {

                                    carP_mc.y -= 5;

                                }

                                if (downPressed) {

                                    carP_mc.y += 5;

                                }

                                if (leftPressed) {

                                    carP_mc.x -= 5;

                                }

                                if (rightPressed) {

                                    carP_mc.x += 5;

                                }

                             

                             

                                for (i = 0; i < 10; i++) {

                                    mazehit = false

                                    if (carP_mc.hitTestObject(this["wallDet" + i + "_mc"])) {

                                        mazehit = true;

                                        break;

                                    }

                                    if (mazehit) {

                                        if(rightPressed){

                                            carP_mc.x -= 5;

                                        }

                                        if(leftPressed){

                                            carP_mc.x += 5;

                                        }

                                        if(upPressed){

                                            carP_mc.y += 5;

                                        }

                                        if(downPressed){

                                            carP_mc.y -= 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;

                                        }

                                }

                            }

                            • 13. Re: Need to Create a Wall Barrier and Detection for Maze Game
                              Izzanator Level 1

                              kglad could you please email me off the question link that Ned sent you. I did not know that I could not place the same question in two places. Thanks.

                              • 14. Re: Need to Create a Wall Barrier and Detection for Maze Game
                                Izzanator Level 1

                                I asked kglad to email me on the other link that you provided to him.

                                • 15. Re: Need to Create a Wall Barrier and Detection for Maze Game
                                  kglad Adobe Community Professional & MVP

                                  ned, thank you.

                                   

                                  izzanator, it's 'impolite' to ask two different people to spend their time working on the same code.  i know you hadn't thought of that so, no problem. 

                                   

                                  if you're going to use the code i suggested, continue on this thread and you should appologize to sinious and close that thread.  if you're going to use the code sinious suggested, stop posting here and continue on your other thread.

                                  1 person found this helpful
                                  • 16. Re: Need to Create a Wall Barrier and Detection for Maze Game
                                    Izzanator Level 1

                                    Sorry for the delay.  I just returned from school.  I completely understand and I apologize for that.  I didn't know about that since I am new to the whole thing.  But thanks a lot for the help.  Have a great day...(: