Skip to main content
June 17, 2012
Answered

How to make maze walls?

  • June 17, 2012
  • 2 replies
  • 7973 views

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.

This topic has been closed for replies.
Correct answer kglad

Hmm yes,

Am I wrong or is there one to many}?

Either way I'm left with tons of errors. I think I'll put this project on ice for now.

One last thing, do you know of any tutorials or online classes, preferably free?

Thanks for your attempt though.


no:

http://www.kglad.com/Files/forums/maze.fla

2 replies

kglad
Community Expert
Community Expert
June 19, 2012

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

}

}

}

June 19, 2012

Im very sorry mr kglad. But it seems like I have fallen of the waggon once again.I started all over, creating the walls within an own layer and converterted each one rectangular wall to a movieclip naming them m1 to m24.

But the actionscrip seems to get bugged as i try to publish. Or i get some error messages
Don't know if you ment me to use simply the last one you posted or combine them somehow.

Anyhow if I use the last one I get these errors:

Scene 1, Layer 'action', Frame 1, Line 71084: Syntax error: expecting semicolon before rightparen.
Scene 1, Layer 'action', Frame 1, Line 81084: Syntax error: expecting colon before dot.
Scene 1, Layer 'action', Frame 1, Line 91084: Syntax error: expecting identifier before rightbrace.
Scene 1, Layer 'action', Frame 1, Line 111084: Syntax error: expecting rightparen before function.

and if i try to fix them several other errors joins the party.

is it ment for me to swap something out from the code, amount of walls, wall names etc?
Again really, thanks for all the support. Wish I knew this a bit better, I am going to take some courses next year at school. Maybe i'll get it then.

kglad
Community Expert
Community Expert
June 17, 2012

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.

June 19, 2012

Tried to do as you said, but as i earlier declared. I'm terrible, so the follow-up question is: What's a parent and children etc? Il understand if you wont be bothered to help me, as it may seem useless


ty for the reply though

edit:
Would something like this be useful?

if(ball.hitTestObject(wall)){

                    ball.y = 50

As the ball is my moving character (movieclip) and the "wall" is also a movieclip but not moving ofc?