Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:38:17+00:00 2026-06-01T16:38:17+00:00

I am trying to program a Flash Game that utilizes all four directions to

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:38:18+00:00Added an answer on June 1, 2026 at 4:38 pm

    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;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a fullscreen chat program using Flash AS3, and so
I've got an Adobe Flash 10 program that freezes in certain cases, however only
I am just trying to add a Hello World or a program that draw
In my Flash program, I am trying to run a function within an object.
I'm having a hard time describing a Flash program that uses a sort of
I'm trying to automate some stuff on a flash game running in a webbrowser
I have a winform program that allows users to upload Shockwave Flash movies to
I'm trying to write a program that will run a program on a remote
I trying to learn how to program in Adobe Flash Professional CS5. However i
Hi Im trying to develop a C# program to scrape the urls of flash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.