Skip navigation
Tixzy
Currently Being Moderated

How to make maze walls?

Jun 17, 2012 6:20 AM

Tags: #problem #cs5 #cs5.5 #3.0 #code_snippets #actionscript #ac3 #game #actionscript3 #wall #maze #game.

Hello,

First off I would like to clearify that i'm terrible at this and have no idea what im doing

Anyhow, i would like to make a maze game with accelerometer. I have managed to make a ball within a certain area with this code:

 

import flash.events.Event;

 

 

var accelX:Number;

var accelY:Number;

 

 

var fl_Accelerometer:Accelerometer = new Accelerometer();

fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);

function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void

{

          accelX = event.accelerationX;

          accelY = event.accelerationY;

}

 

 

ball.addEventListener(Event.ENTER_FRAME, moveBall);

function moveBall(evt:Event){

          ball.x -= accelX*30;

          ball.y += accelY*30;

 

          if(ball.x > (480-ball.width/2)){

                    ball.x = 480-ball.width/2;

          }

          if(ball.x < (0+ball.width/2)){

                    ball.x = 0+ball.width/2;

          }

          if(ball.y > (800-ball.width/2)){

             ball.y = 800-ball.width/2;

          }

          if(ball.y < (0+ball.width/2)){

                    ball.y = 0+ball.width/2;

          }

}

 

 

Now the problem is that i would like to make some walls within this maze, some restricted areas if i may, so it becomes an actually maze. Have tried and failed several times and have only managed to make a but not limit it. so it just cuts the screen in half etc.

 
Replies
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 17, 2012 8:59 AM   in reply to Tixzy

    if each wall is a rectangle, you can simplify your coding by creating a parent for the walls and adding rectangular movieclip walls to that parent. 

     

    you could then loop through all the children of that parent checking a hitTestObject between your and the child walls.  if it's negative, store the ball x and y.  if positive, re-assign the ball x and y to the previously stored values.

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 18, 2012 10:11 PM   in reply to Tixzy

    create your maze.  convert each rectangle in the maze to a movieclip and assign an instance name (eg, m1,m2,...,m44).  don't convert any L-shaped, U-shaped or rotated L-shaped, U-shaped etc sections.  only rectangular blocks.

     

    you can then use:

     

    var prevX:int;

    var prevY:int;

    var i:int;

     

    var mP:Sprite=new Sprite();

    addChild(mP);

    for(i=1;i<=44,i++){

    mP.addChild(this["m"+i]);

    }

     

    function moveBall(evt:Event){

              ball.x -= accelX*30;

              ball.y += accelY*30;

     

              if(ball.x > (480-ball.width/2)){

                        ball.x = 480-ball.width/2;

              }

              if(ball.x < (0+ball.width/2)){

                        ball.x = 0+ball.width/2;

              }

              if(ball.y > (800-ball.width/2)){

                 ball.y = 800-ball.width/2;

              }

              if(ball.y < (0+ball.width/2)){

                        ball.y = 0+ball.width/2;

              }

    checkWallF();

    }

     

    function checkWallF():void{

    for(i=0;i<mP.numChildren;i++){

    if(ball.hitTestObject(mP.getChildAt(i))){

    ball.x=prevX;

    ball.y=prevY;

    break;

    }

    prevX=ball.x;

    prevY=ball.y

    }

    }

    }

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 19, 2012 6:32 AM   in reply to kglad

    this line:

     

    for(i=1;i<=44,i++){

     

    should b:

     

    for(i=1;i<=44;i++){

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 21, 2012 9:28 AM   in reply to Tixzy
     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 28, 2012 6:00 AM   in reply to Tixzy

    you're welcome.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 30, 2012 9:09 PM   in reply to kglad

    Hi again, Kglad. I've downloaded this flash file with code for the maze (i too am keen to make a maze), but when i test the movie, the ball fails to move.

     

    Am i missing something?

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 30, 2012 9:54 PM   in reply to minimalcomfort

    are you testing on an iDevice?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 1, 2012 12:51 AM   in reply to kglad

    not at all, am testing on a big pc. And, BTW, when i test the Flash file, an xml file is created. Is that supposed to happen?

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 1, 2012 5:45 AM   in reply to minimalcomfort

    check the publish settings.  you're publishing for iOS, that's why flash is creating the xml.

     

    also, that code uses the accelerometer to move the ball.

     

    you need to change both to test ball movement in that file.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 2:11 AM   in reply to kglad

    Hi. U are right, of course. I got that code and flash file from the above link, and it is set up for mobile devices. As i understand, Accelometer is for mobile devices, and while i changed the publish settings to use Flash as the Player (instead of Air for ios which was how the file was set up), i am wondering if it is possible to use this code as such, on a big PC, not mobile device.

     

    And, i dont know how to change the accelemeter in hte publish settings....

    thank you.

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 5, 2012 10:22 AM   in reply to minimalcomfort

    yes, but instead of using the accelerometer, use something else (like a keyboardevent listener) to move the ball.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 28, 2012 9:46 AM   in reply to kglad

    Hi Kglad

     

    I have downloaded http://www.kglad.com/Files/forums/maze.fla

     

    I'm having the same problem as Tixy where just one wall of the left of the maze.fla file has collision, the ball MC just passes through the other walls in the project, is there something I need to change with the checkwall function?

     

    Rgeards

     

    Sam

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points