Skip to main content
Serifaz
Known Participant
Mar 19, 2021 5:20 AM UTC
Answered

As3 Collision help needed

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 is closed to new replies. Start a new post to keep the conversation going.
    kglad
    kgladCorrect answer
    Community Expert
    Mar 21, 2021 7:48 PM UTC

    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;

    }

    11 replies

    kglad
    Community Expert
    Community Expert
    Mar 19, 2021 2:04 PM UTC

    canvas or as3?

     

    what problem are your having?

    Translate
    Serifaz
    SerifazAuthor
    Known Participant
    Mar 19, 2021 9:00 PM UTC

    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.

    Translate
    kglad
    Community Expert
    Community Expert
    Mar 19, 2021 9:59 PM UTC

    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;

    Translate