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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:26:42+00:00 2026-06-13T12:26:42+00:00

I’m trying to get my character to bounce back when it hits an object

  • 0

I’m trying to get my character to bounce back when it hits an object in order to not allow it to touch the objects. My problem is when its touching 2 objects, it gets stuck and moves backwards because of my else statement. Is there any way I can change my code to not allow my character to touch the objects in the first place.

My code:

o1 is the instance name of the objects all contained in a movie clip and p1 is my charcter.

import flash.events.Event;

var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var hit:Boolean = false;

p1.addEventListener(Event.ENTER_FRAME, moveChar);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);


var redRect:Rectangle = o1.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(o1);

var blueRect:Rectangle = p1.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(p1);

addEventListener(Event.ENTER_FRAME, enterFrame);

function enterFrame(e:Event):void
{
if(redClipBmpData.hitTest(new Point(o1.x, o1.y),
                            255,
                            blueClipBmpData,
                            new Point(p1.x, p1.y),
                            255

                      ))
{
    o1.filters = [new GlowFilter()];
    hit = true;
}
else
{
    o1.filters = [];
    hit = false;
}
}


function moveChar(event:Event):void{
if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_down");
    if(!hit)
        p1.y += 5;
    else
        p1.y -= 10;
}
if(upKeyDown  && !downKeyDown && !rightKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_up");
    if(!hit)
        p1.y -= 5;
    else
        p1.y += 10;
}
if(rightKeyDown  && !upKeyDown && !downKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_right");
    if(!hit)
        p1.x += 5;
    else
        p1.x -= 10;
}
if(leftKeyDown  && !upKeyDown && !rightKeyDown && !downKeyDown)
{
    p1.gotoAndStop("walk_left");
    if(!hit)
        p1.x -= 5;
    else
        p1.x += 10;
}
}

function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown = true;
}
if(event.keyCode == 68){
    rightKeyDown = true;
}
if(event.keyCode == 83){
    downKeyDown = true;
}
if(event.keyCode == 65){
    leftKeyDown = true;
}
}


    function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown = false;
    p1.gotoAndStop("still_up");
}

if(event.keyCode == 68){
    rightKeyDown = false;
    p1.gotoAndStop("still_right");
}

if(event.keyCode == 65){
    leftKeyDown = false;
    p1.gotoAndStop("still_left");
}

if(event.keyCode == 83){
    downKeyDown = false;
    p1.gotoAndStop("still_down");
}
}

Thanks in advance

EDIT:
I changed it to what you said , but now my character doesn’t want to move at all :<

CODE:

import flash.events.Event;

var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var hit:Boolean = false;
var redSpeed:Point = new Point();

p1.addEventListener(Event.ENTER_FRAME, moveChar);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);


var redRect:Rectangle = o1.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(o1);

var blueRect:Rectangle = p1.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(p1);

addEventListener(Event.ENTER_FRAME, enterFrame);

function enterFrame(e:Event):void
{
if(redClipBmpData.hitTest(new Point(o1.x + redSpeed.x, o1.y + redSpeed.y),
                    255,
                    blueClipBmpData,
                    new Point(p1.x, p1.y),
                    255))
{
    o1.filters = [new GlowFilter()];
    hit = true;
}
else
{
    o1.filters = [];
    hit = false;
}
 }


 function moveChar(event:Event):void{
if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_down");
    if(!hit)
    {
        redSpeed.y = 0;
    }
    else
    {
        redSpeed.y = 5;
        p1.y += 5;
    }
}
if(upKeyDown  && !downKeyDown && !rightKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_up");
    if(!hit)
    {
        redSpeed.y = 0;
    }
    else
    {
        redSpeed.y = -5;
        p1.y -= 5;
    }
}
if(rightKeyDown  && !upKeyDown && !downKeyDown && !leftKeyDown)
{
    p1.gotoAndStop("walk_right");
    if(!hit)
    {
        redSpeed.x = 0;
    }
    else
    {
        redSpeed.x = 5;
        p1.x += 5;
    }
}
if(leftKeyDown  && !upKeyDown && !rightKeyDown && !downKeyDown)
{
    p1.gotoAndStop("walk_left");
    if(!hit)
    {
        redSpeed.x = 0;
    }
    else
    {
        redSpeed.x = -5;
        p1.x -= 5;
    }
}
 }

 function checkKeysDown(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown = true;
}
if(event.keyCode == 68){
    rightKeyDown = true;
}
if(event.keyCode == 83){
    downKeyDown = true;
}
if(event.keyCode == 65){
    leftKeyDown = true;
}
 }


 function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown = false;
    p1.gotoAndStop("still_up");
}

if(event.keyCode == 68){
    rightKeyDown = false;
    p1.gotoAndStop("still_right");
}

if(event.keyCode == 65){
    leftKeyDown = false;
    p1.gotoAndStop("still_left");
}

if(event.keyCode == 83){
    downKeyDown = false;
    p1.gotoAndStop("still_down");
}
 }

Is this what you meant ?

  • 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-13T12:26:43+00:00Added an answer on June 13, 2026 at 12:26 pm

    I think what you will want to do is do a hittest based on the anticipated locations of the objects. In order to do that, though, you would want assign speed values, instead of moving the objects on moveChar().

    private var redSpeed:Point = new Point();
    
    //in moveChat:
    if(downKeyDown && !upKeyDown && !rightKeyDown && !leftKeyDown)
    {
        p1.gotoAndStop("walk_down");
        if(!hit)
            redSpeed.y = 5;
        else
            redSpeed.y = 0;
    }
    
    
    // in enterFrame
    if(redClipBmpData.hitTest(new Point(o1.x + redSpeed.x, o1.y + redSpeed.y),
                            255,
                            blueClipBmpData,
                            new Point(p1.x, p1.y),
                            255)) {.....
    

    Then you can just move the object by its speed.

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

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.