Skip to main content
Participant
January 14, 2013
Answered

Walls in flash

  • January 14, 2013
  • 2 replies
  • 2565 views

how do you make walls in flash for pacman?

why doesnt the code work?

var walls:Array = [walls1_mc];

for (var i:uint= 0; i<walls.length; i++)

{

          trace("wall x " + walls.x +" y " +walls.y);

}

function myResponse(evt:KeyboardEvent):void

{

          var i:uint = 0;

          if (evt.keyCode == Keyboard.RIGHT)

          {

                    for (i= 0; i<walls.length; i++)

                    {

                              if (walls.hitTestPoint(pacman_mc.x+24,pacman_mc.y))

                              {

                                        trace("OH!");

                              }

                    }

                    pacman_mc.x +=  5;

                    if (pacman_mc.x >= 550)

                    {

                              pacman_mc.x = 0;

                    }

          }

}

This topic has been closed for replies.
Correct answer kglad

then your next problem is your logic is faulty.  each of those for-loops should be changed from:

       for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x + 40,pacman_mc.y))

                                        {

                                                  return;

                                        }

                                        pacman_mc.x +=  5;

                              }

to:

       for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x + 40,pacman_mc.y))

                                        {

                                                  return;

                                        }

}

                                        pacman_mc.x +=  5;

2 replies

Participant
January 10, 2020

this is my code. why poop go through the walls?

 

import flash.events.KeyboardEvent;

stop();


wall.enabled=false;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

poop.addEventListener(Event.ENTER_FRAME, fl_ClickToGoToWebPage);

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);

//stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function fl_ClickToGoToWebPage(event:Event)

{

if (upPressed)

{

poop.y -= 5;

}

if (downPressed)

{

poop.y += 5;

}

if (leftPressed)

{

poop.x -= 5;

}

if (rightPressed)

{

poop.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(poop.hitTestObject(wall)) {

poop.x += 5;

mazehit = true;

//break;

}

}

if (downPressed) {

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

if(poop.hitTestObject(wall)) {

poop.x += 5;

mazehit = true;

//break;

}

}

if (leftPressed) {

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

if(poop.hitTestObject(wall)) {

poop.x += 5;

mazehit = true;

//break;

}

}

if (rightPressed) {

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

if(poop.hitTestObject(wall)) {

poop.x += 5;

mazehit = true;

//break;

}

}

}

 

 

 

 

 

 

 

kglad
Community Expert
Community Expert
January 14, 2013

there's nothing calling myResponse().   you should also have something like:

stage.addEventListener(KeyboardEvent,myResponse);

Participant
January 15, 2013

sorry this is my full code

var walls:Array = [walls1_mc];

for (var i:uint= 0; i<walls.length; i++)

{

          trace("wall x " + walls.x +" y " +walls.y);

}

stage.addEventListener(KeyboardEvent.KEY_DOWN,pacMove);

function pacMove(evt:KeyboardEvent):void

{

          var i:uint = 0;

          switch (evt.keyCode)

          {

                    case Keyboard.RIGHT :

                              pacman_mc.rotation = 0;

                              for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x + 40,pacman_mc.y))

                                        {

                                                  return;

                                        }

                                        pacman_mc.x +=  5;

                              }

                              break;

                    case Keyboard.LEFT :

                              pacman_mc.rotation = 180;

                              for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x - 40,pacman_mc.y))

                                        {

                                                  return;

                                        }

                                        pacman_mc.x -=  5;

                              }

                              break;

                    case Keyboard.UP :

                              pacman_mc.rotation = -90;

                              for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x,pacman_mc.y - 4))

                                        {

                                                  return;

                                        }

                                        pacman_mc.y -=  5;

                              }

                              break;

                    case Keyboard.DOWN :

                              pacman_mc.rotation = 90;

                              for (i= 0; i<walls.length; i++)

                              {

                                        if (walls.hitTestPoint(pacman_mc.x,pacman_mc.y + 5))

                                        {

                                                  return;

                                        }

                                        pacman_mc.y +=  5;

                              }

                              break;

          }

}

Inspiring
January 15, 2013

Enable debugging in Flash (Publishing Options/Extended/Allow Debugging). What does the Output window say?