Copy link to clipboard
Copied
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;
}
}
}
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
...Copy link to clipboard
Copied
there's nothing calling myResponse(). you should also have something like:
stage.addEventListener(KeyboardEvent,myResponse);
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
Enable debugging in Flash (Publishing Options/Extended/Allow Debugging). What does the Output window say?
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
thats what i have
Copy link to clipboard
Copied
no, you don't and that's why your code won't work for more than one wall unless you change to the code i suggested.
Copy link to clipboard
Copied
ok i see
solved the problem
thanks
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
Code works fine for me. So you probably named the instances on your stage not correct.
Is there a Wall-MovieClip with name "walls1_mc" on your stage?
Is there a pacman-Movieclip with name "pacman_mc" on your stage?
Copy link to clipboard
Copied
i have them and now it works
now my question is how i can make more than just the one wall?
thanks
Copy link to clipboard
Copied
Depending on what you intend to achieve (learing Actionscript or gettig a running game)
It could be a first start to look at a pac-man game thats done in as3 and works, and analyze the code.
Here is one.
If you instead want to learn AS3 from the Basics, a Pac-Man game is not the right place to start imo.
Copy link to clipboard
Copied
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;
}
}
}