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

  • Home
  • SEARCH
  • 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 7792587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:13:51+00:00 2026-06-01T22:13:51+00:00

I am trying to do an example of collision in Action Script 3. It’s

  • 0

I am trying to do an example of collision in Action Script 3. It’s a character that should stop when it hits a platform. It works well when I move only to the right, left, up or down directions, but if I try to move in the diagonals, if the characteris colliding with the platform, the object goes to a different area of the screen.

This is the compiled example: http://dl.dropbox.com/u/5282142/GameDemo.html

And below is my code.

Now, does anyone know a better way to do what I am doing, or how can I get the character not to go to a weird position when I try to move it in a diagonal?

var level:Array = new Array();
for (var i = 0; i < numChildren; i++) {
    if (getChildAt(i) is Platform) {
        level.push(getChildAt(i).getBounds(this));
    }
}

var speedX:int = 0;
var speedY:int = 0;

var kLeft:Boolean = false;
var kRight:Boolean = false;
var kDown:Boolean = false;
var kUp:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);

function onKeyDownHandler(event:KeyboardEvent):void {
    if (event.keyCode == 37) kLeft = true;
    if (event.keyCode == 38) kUp = true;
    if (event.keyCode == 39) kRight = true;
    if (event.keyCode == 40) kDown = true;
}

function onKeyUpHandler(event:KeyboardEvent):void {
    if (event.keyCode == 37) kLeft = false;
    if (event.keyCode == 38) kUp = false;
    if (event.keyCode == 39) kRight = false;
    if (event.keyCode == 40) kDown = false;
}

addEventListener(Event.ENTER_FRAME, loop);

function loop(event:Event):void {
    moveChar();
    bound();
}

function moveChar():void {
    if (kLeft) {
        speedX = -10;
    } else if (kRight) {
        speedX = 10;
    } else {
        speedX *= 0.5;
    }

    if (kUp) {
        speedY = -10;
    } else if (kDown) {
        speedY = 10;
    } else {
        speedY *= 0.5;
    }

    character.x += speedX;
    character.y += speedY;
}

function bound():void {
    if (character.x > (800 - character.width/2)){
        character.x = 800 - character.width/2;
    }
    if (character.x < (character.width/2)){
        character.x = character.width/2;
    }
    if (character.y > (480 - character.height/2)){
        character.y = 480 - character.height/2;
    }
    if (character.y < (character.height/2)){
        character.y = character.height/2;
    }
    for (i = 0; i < level.length; i++) {
        if (character.getBounds(this).intersects(level[i])) {
            if (speedX > 0) {
                character.x = level[i].left - character.width/2;
            }
            if (speedX < 0) {
                character.x = level[i].right + character.width/2;
            }
        }
    }
    for (i = 0; i < level.length; i++) {
        if (character.getBounds(this).intersects(level[i])) {
            if (speedY > 0) {
                character.y = level[i].top - character.height/2;
            }
            if (speedY < 0) {
                character.y = level[i].bottom + character.height/2;
            }
        }
    }
}
  • 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-01T22:13:54+00:00Added an answer on June 1, 2026 at 10:13 pm

    Your Problem lies in this part of your code :

    for (i = 0; i < level.length; i++) {
            if (character.getBounds(this).intersects(level[i])) {
                if (speedX > 0) {
                    character.x = level[i].left - character.width/2;
                }
                if (speedX < 0) {
                    character.x = level[i].right + character.width/2;
                }
            }
        }
    
    for (i = 0; i < level.length; i++) {
                if (character.getBounds(this).intersects(level[i])) {
                    if (speedY > 0) {
                        character.y = level[i].top - character.height/2;
                    }
                    if (speedY < 0) {
                        character.y = level[i].bottom + character.height/2;
                    }
                }
            }
    

    The character does not actually move to a random position. It is being set by you, above.

    You have to create a better logic (as per the scenario) where your code should not get confused when two keys are down simultaneously & a collision occurs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find a collision avoidance example that I can adapt
Trying to find an example that has css rollover using sprites & sliding door
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
i am trying one example from Here for ontouch image zoom and touch on
I am learning and trying simple example using node.js and mongoskin. here is my
I was trying the following example, but with external URLs: Using WebViews The example
I was trying the following example of I/O ports from here . #include<stdio.h> #include<unistd.h>
Hi while trying out jni example in this link http://wendro.blogspot.com/2010/03/jni-example-eclipse-dev-cpp.html?showComment=1309930446765#c5048550711511727724 with eclipse Helios windows
I am using UICatalog example and trying to find whether it is possible to
I'm using Mirven's Twitter OAuth Sinatra example and trying to figure out how I

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.