How can I stop a colliding object from continuing falling after several seconds (AS3)?

82 views Asked by At

I have a problem that is related to AS3, it involves two code locations (MainTimeline and an MC-extending class), as well as a basic physics engine function. The problem is that when the objects collide (enemy and ground), they stay collided, but after a short period of time (2-6 seconds), the object that is gravitated teleports under the object it collided with, and continues to fall. Thank you for any assistance. P.S. I do not prefer document class solutions.

Timeline code: `

        stop();

        var ground: MovieClip;

        var enemy:Enemy = new Enemy();

        stage.addChild(enemy);

        enemy.x = 400;

        enemy.y = 150;

        enemy.rotation = 0;

        enemy.addEventListener("enterFrame", collisions);

        function collisions(e: Event): void {

        var collision: Boolean = false;

        if (ground.hitTestObject(enemy)) {
            collision = true;
        }

        if (collision) {
            while (collision) {
                enemy.y -= 0.1;
                collision = false;
            if (ground.hitTestObject(enemy)) {
                collision = true;

            }
            }
            Class(Enemy).yVel = 0;
        }
}

Class code:

package  {
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
        
        var xVel: int;
        var yVel: int;
        
    public function Enemy() {
        // constructor code
        addEventListener("enterFrame", onEnterFrame);
        
    }
    
    function onEnterFrame(e: Event): void {
        
        yVel += 2;
        this.x = xVel;
        this.y = yVel;
    }
    
    
}

}

0

There are 0 answers