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 8031465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:06:32+00:00 2026-06-05T01:06:32+00:00

My collision code is supposed to inverse the x velocity and reduce it to

  • 0

My collision code is supposed to inverse the x velocity and reduce it to a third, then when the player is not pressing either “A” or “D” keys, the velocity should slowly reduce to zero, however, once the player collides the velocity only reduces to a small decimal between .25 and .70 (either positive or negative based on previous directions.)

//Function to determine what to do with keys
function KeyHandler():void{
    //A Key Handlers
    if(aKeyPressed == true){
        if(Math.abs(_vx) < MaxSpeed){
            _vx += -6;
        }
    }
    //Player _vx somehow won't hit zero!!!
    else if(aKeyPressed == false){
        if(_vx < 0){
            _vx += 1;
        }
    }
    //D Key Handlers
    if(dKeyPressed == true){
        if(Math.abs(_vx) < MaxSpeed){
            _vx += 6;
        }
    }
    else if(dKeyPressed == false){
        if( _vx > 0){
            _vx += -1;
        }
    }
    //W Key Handlers
    if(wKeyPressed == true){
        if(Jumped == false){
            _vy = -15;
            Jumped = true;
        }
    }
    else if(wKeyPressed == false){

    }
}
//Code for Right Collision
    if(RightCollision){
        while(RightCollision){
            Player.x -= 0.1;
            RightCollision = false;
            if(_boundaries.hitTestPoint((Player.x + (Player.width / 2)), (Player.y - (Player.height / 2)), true)){RightCollision = true;}
        }
        _vx *= -.33
    }
    //Code for Left Collision
    if(LeftCollision){
        while(LeftCollision){
            Player.x += 0.1;
            LeftCollision = false;
            if(_boundaries.hitTestPoint((Player.x - (Player.width / 2)), (Player.y - (Player.height / 2)), true)){LeftCollision = true;}
        }
        _vx *= -.33
    }
  • 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-05T01:06:34+00:00Added an answer on June 5, 2026 at 1:06 am

    Note that abs(-.25) + abs(.7) ~ 1.0

    The collision sets the velocity to something that is not an integer (e.g. 2 * .33 ~ .7), so +/- 1 will skip past 0 without landing on it.

    The simple fix is to keep the velocity an integer which can be done with Math.floor, for instance. (Account for the difference in +/- velocities: floor only moves numbers one direction.)

    Happy coding.


    Also, I am not sure how the int type works in AS3 work, which might be worth exploring.

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

Sidebar

Related Questions

I'm working on a demo about collision detection. (Some of the code for this
How do I create code for collision detection on a bitmap rotated by a
I'm following an XNA tutorial and have the following code for collision detecting (detecting
I have the following simple code to test against collision on a primary key
I have working code below for collision of two circles, but now I have
i use boundingBox to check collision, it's fine for touches, but not at all
I've been having some strange problems with the collision code of a game I'm
I implemented the code from the question Ball to Ball Collision - Detection and
I've snatched this piece of tile collision code from another game, but in the
In my collision detection code, bullets are deactivated once they have been involved in

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.