Skip to main content
Serifaz
Known Participant
March 19, 2021
Answered

As3 Collision help needed

  • March 19, 2021
  • 1 reply
  • 776 views

Hello I am wondering is there a proper way to implement player collision with objects.

I have tried figuring this out for over a week now. I probably should have asked on here earlier.

I would rather not have to install a lib to do this if possible.

If a lib is neccissary please link me to some documentation on how to use it.

I tried nape with no luck. I am trying to make it so when I collide with an object it stops the player from moving further without skipping them back a few steps to reset them allowing them to move back away from the collision without being stuck to the collider.

    This topic has been closed for replies.
    Correct answer kglad

    Thank you soo much, it works perfect.

    Just one last question. how would i remove the event listener for movement because it is stacking the movement speed every time I start the application for some reason.


    you can use removeEventListener to remove an event listener, but if you're re-adding event listeners because a frame is playing it's better to use:

     

    var alreadyExecuted;

    if(!alreadyExecuted){

    // any code you don't want re-executed. eg,

    whatever.addEventListener(someevent,somefunction);

    alreadyExecuted = true;

    }

    1 reply

    kglad
    Community Expert
    Community Expert
    March 19, 2021

    canvas or as3?

     

    what problem are your having?

    Serifaz
    SerifazAuthor
    Known Participant
    March 19, 2021

    Thank you for your reply. 

     

    In my player script I check if the player is colliding here

    if (varLeft == true){
    				if(_collision){
    					char.x += speed;
    				_collision = false;
    
    				return;
    				}
    				char.x -= speed;				
    			}

    Im simply resetting the char.x back a step if it collides with a collision object because if I dont set it back a step _collision will still be true.

    I want to make it so the char will not need to be reset back a step but allow them to move away still without _collision equaling true. I hope Im making sense.

    kglad
    Community Expert
    Community Expert
    March 19, 2021

    if you're trying to make the char reverse direction upon collision, use:

     

    if (varLeft == true){
    if(_collision){

    speed *= -1;
    _collision = false;

    }
    char.x += speed;