0
votes

I am trying to program a Flash Game that utilizes all four directions to move the character. (i.e. Pressing up moves the character north, down - south, etc.) The character in the game does not actually move, but the background moves to simulate movement. I need to figure out how to go about programming the walls, though. I have code that recognizes collision, but what is the best way to do it? This is my code:

stage.addEventListener(Event.ENTER_FRAME,handleCollision);

function handleCollision( e:Event ):void{

    if(hero.hitTestObject(wall))
       {
           wall.x-=5;
           Background.x-=5;
       }
}

That recognizes the collision and stops the background and wall from moving, but that only works for when the character hits the wall from the right. Do I have to plug in this code to EVERY wall and tailor it to the situation? Or is there a more streamlined way of working with walls so that I can keep coding to a minimum?

Update: I tried using Jason's idea of utilizing an array to keep track of the walls, but now I have another problem. This is my code so far:

stage.addEventListener(Event.ENTER_FRAME,onenter);

var walls:Array = [Northwall, Southwall, Eastwall, Westwall];

function onenter(e:Event):void {

if(Key.isDown(Key.RIGHT))
{
    Background.x-=5;
    hero.scaleX=1;
    for each(var wall:MovieClip in walls)
    {
        Northwall.x-=5;
        Southwall.x-=5;
        Eastwall.x-=5;
        Westwall.x-=5;
    }
    hero.gotoAndStop('walking sideways');
}
else if(Key.isDown(Key.LEFT))
{
    Background.x+=5;
    hero.scaleX=-1;
    for each(var walls0:MovieClip in walls)
    {
        Northwall.x+=5;
        Southwall.x+=5;
        Eastwall.x+=5;
        Westwall.x+=5;
    }
    hero.gotoAndStop('walking sideways');
}
else if(Key.isDown(Key.DOWN))
{
    Background.y-=5;
    for each(var walls1:MovieClip in walls)
    {
        Northwall.y-=5;
        Southwall.y-=5;
        Eastwall.y-=5;
        Westwall.y-=5;
    }
    hero.gotoAndStop('walking down');

}
else if(Key.isDown(Key.UP))
{
    Background.y+=5;
    for each(var walls2:MovieClip in walls)
    {
        Northwall.y+=5;
        Southwall.y+=5;
        Eastwall.y+=5;
        Westwall.y+=5;
    }
    hero.gotoAndStop('walking up');
}
else
{
    hero.gotoAndStop('still 1');
}




 for each (var walls:MovieClip in walls) {  
   if (hero.hitTestObject(Northwall))
{
    Background.y-=5;
}
else if (hero.hitTestObject(Southwall))
{
    Background.y+=5;
}
else if (hero.hitTestObject(Westwall))
{
    Background.x-=5;
}
else if (hero.hitTestObject(Eastwall))
{
    Background.x+=5;
}

The code compiles and everything runs, but the walls aren't registering. I think it has to do with the movement code in the if/else statements. I think the walls are moving with the character, instead of moving with the background, which means my character never reaches the walls to register the hittest. How can I code it so that the walls move with the background so I can actually test whether or not I can hit them?

Update 2: I tried utilizing the code Jason provided in my own program, and it is working out quite nicely. The character moves and can hit walls, and most of the moving code functions as intended. However, only the Eastwall seems to function correctly. The Southwall seems to register from the side, and acts weird when I try to hit it from the North. Naturally, I would like to have it register from the North, rather than from the side, and have the same effect as when I hit the Eastwall. The Northwall and Westwall also don't seem to register at all. This is the code fully implemented:

var walls:Array = [Northwall, Southwall, Eastwall, Westwall];

stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
function keydownHandler(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.RIGHT:
            moveWalls(-5, 0);
            Background.x-=5;
            hero.scaleX=1;
            hero.gotoAndStop('walking sideways');
            break;
        case Keyboard.LEFT:
            moveWalls(5, 0);
            Background.x+=5;
            hero.scaleX=-1;
            hero.gotoAndStop('walking sideways');
            break;
        case Keyboard.UP:
            moveWalls(0, -5);
            Background.y+=5;
            hero.gotoAndStop('walking up');
            break;
        case Keyboard.DOWN:
            moveWalls(0, 5);
            Background.y-=5;
            hero.gotoAndStop('walking down');
            break;
    }
}

function moveWalls(x:Number, y:Number):void
{
    for each (var wall:DisplayObject in walls)
    {
        wall.x += x;
        wall.y += y;
    }
}

stage.addEventListener(Event.ENTER_FRAME,frameHandler);
function frameHandler(event:Event):void
{
    for each (var wall:DisplayObject in walls)
    {
        if (hero.hitTestObject(wall))
        {
            switch (wall)
            {
                case Northwall:
                    moveWalls(0, -5);
                    Background.y-=5;
                    break;
                case Southwall:
                    moveWalls(0, 5);
                    Background.y+=5;
                    break;
                case Westwall:
                    moveWalls(-5, 0);
                    Background.x-=5;
                    break;
                case Eastwall:
                    moveWalls(5, 0);
                    Background.x+=5;
                    break;
            }
        }
    }
}

I think the problem might be in the hitTest. But I'm not sure. Also, if I hold down the button, the character meets resistance, but if held down long enough, the character ends up moving through the wall entirely. How can I stop the character from traveling through the wall?

1

1 Answers

1
votes

You could keep a collection of all objects to be tested, and iterate over the loop:

var walls:Array = [ northWall, southWall, westWall, eastWall ];

for each (var wall:DisplayObject in walls)
{
    if (hero.hitTestObject(wall))
    {
        // hit wall
    }
}

Or, you could apply the inverse, by hit testing the character and path they are bound to:

if(!hero.hitTestObject(path))
{
    // left path
}

Otherwise, you could test coordinates of a predefined bounds:

var bounds:Rectangle = new Rectangle(0, 0, 400, 400);

if ((hero.x < bounds.x) ||
    (hero.x > (bounds.x + bounds.width)) ||
    (hero.y < bounds.y) ||
    (hero.y > (bounds.y + bounds.height)))
{
    // left path
}

Update: Based upon your new example, here is an example implementation of your functional request:

Example setup in Flash Pro:

Flash Pro setup

You can download this project from my website:

Flash Pro CS5.5: http://www.jasonsturges.com/labs/stack-overflow/examples/hero/Hero-cs5.5.fla
Flash Pro CS5: http://www.jasonsturges.com/labs/stack-overflow/examples/hero/Hero-cs5.fla
SWF: http://www.jasonsturges.com/labs/stack-overflow/examples/hero/Hero.swf

With walls as movie clips each with an instance name, you could implement your actions as follows:

import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.DisplayObject;

// put walls in collection
var walls:Array = [ northWall, southWall, eastWall, westWall ];

// animtion listener
addEventListener(Event.ENTER_FRAME, frameHandler);

// key listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydownHandler);


// keyboard handling
function keydownHandler(event:KeyboardEvent):void
{
    switch (event.keyCode)
    {
        case Keyboard.RIGHT:
            moveWalls(-5, 0);
            break;
        case Keyboard.LEFT:
            moveWalls(5, 0);
            break;
        case Keyboard.UP:
            moveWalls(0, -5);
            break;
        case Keyboard.DOWN:
            moveWalls(0, 5);
            break;
    }
}

// utility to move all walls
function moveWalls(x:Number, y:Number):void
{
    for each (var wall:DisplayObject in walls)
    {
        wall.x += x;
        wall.y += y;
    }
}

// animation handler
function frameHandler(event:Event):void
{
    for each (var wall:DisplayObject in walls)
    {
        if (hero.hitTestObject(wall))
        {
            switch (wall)
            {
                case northWall:
                    moveWalls(0, -5);
                    break;
                case southWall:
                    moveWalls(0, 5);
                    break;
                case westWall:
                    moveWalls(-5, 0);
                    break;
                case eastWall:
                    moveWalls(5, 0);
                    break;
            }
        }
    }
}