Collision Detection: How to handle Collision Oversteps?

Hey guys!

So I have this simple platformer that I am working on, and everything went smoothly until I hit an annoying barrier. After collision had been detected, the object would go through the boundary (wall, ground, platform, whichever it may be) before it went back to stand ontop/next to it. The following code and the attached file shows a simple example of what I am trying to do;

package 
{

    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event;

    public class Platformerhowto extends MovieClip
    {
        
        private var box:BoxMC;
        private var ground:WallMC;
        
        private var leftKey:Boolean = false;
        private var rightKey:Boolean = false;
        private var jumpKey:Boolean = false;
        private var runKey:Boolean = false;
        private var onGround:Boolean = false;
        
        private var gravity:Number = 2;
        private var vx:Number = 0;
        private var vy:Number = 0;

        public function Platformerhowto()
        {
            init();
        }

        private function init():void
        {

            box = new BoxMC  ;
            box.x = stage.stageWidth * 0.5;
            box.y = 30;
            addChild(box);

            ground = new WallMC  ;
            ground.x = stage.stageWidth * 0.5;
            ground.y = stage.stageHeight - 50;
            addChild(ground);

            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKEYDOWN);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKEYUP);
            addEventListener(Event.ENTER_FRAME, onENTERFRAME);

        }

        private function onKEYDOWN(event:KeyboardEvent):void
        {

            var pressedKey = event.keyCode;

            if (pressedKey == Keyboard.LEFT)
            {
                leftKey = true;
            }
            if (pressedKey == Keyboard.RIGHT)
            {
                rightKey = true;
            }
            if (pressedKey == Keyboard.Z)
            {
                runKey = true;
            }
            if (pressedKey == Keyboard.X)
            {
                jumpKey = true;
            }

        }

        private function onKEYUP(event:KeyboardEvent):void
        {

            var releasedKey = event.keyCode;

            if (releasedKey == Keyboard.LEFT)
            {
                leftKey = false;
            }
            if (releasedKey == Keyboard.RIGHT)
            {
                rightKey = false;
            }
            if (releasedKey == Keyboard.Z)
            {
                runKey = false;
            }
            if (releasedKey == Keyboard.X)
            {
                jumpKey = false;
            }

        }

        private function onENTERFRAME(event:Event):void
        {


            if (leftKey)
            {
                vx = -4;
                if (runKey)
                {
                    vx = -8;
                }
            }
            if (rightKey)
            {
                vx = 4;
                if (runKey)
                {
                    vx = 8;
                }
            }
            
            if(jumpKey && onGround){
                gravity = 2;
                vy -= 30;
                onGround = false;
            }

            

            if (box.y + (box.height * 0.5) > ground.y - (ground.height * 0.5))
            {
                box.y = ground.y - (ground.height * 0.5) - (box.height * 0.5);
                vy = 0;
                gravity = 0;
                onGround = true;
            }

            vy +=  gravity;
            box.y +=  vy;
            box.x +=  vx;

        }

    }

}

If you run the .swf you’ll see that everytime you jump, it goes through the ground, and THEN it goes back ontop of it. I am trying to avoid this “overstep” and I’ve tried different methods as follows:

Predicting next step -
Basically I had a variable called nextYstep = currentYposition + velocityY, that would hold the NEXT stepping point. Then I compared this value to the boundaries of the ground. So IF nextYstep was larger than the boundaries, I would take a SMALLER step that was EXACTLY the step that was needed (the distance remaining between object and ground). So, if(nextYstep > ground Boundaries), then velocityY would be changed to whatever the distance there was left between the objects instead of the originally too large step.
And this worked. No overstep ever. BUT the problem here was that the object would travel at a certain velocityY, and then right before it landed or hit a wall, it would take a SMALLER step, which would result in inevitable slow down of speed, right before it hit the ground/wall.

Taking smaller steps WITHIN one frame -
One method that was suggested by another member at actionscript.org, was having a for-loop that would take the velocityY and divide it to more steps. Since the velocity takes to big of a step right before it collides, which causes it to Overstep the boundary, the suggestion was to take smaller steps at all times. So if velocity is 10, you would divide it by how many steps you want to take, velocity/5 (the five being the steps you are going to take within one frame). And then every frame, you would take five 2-steps, instead of one big ten-step, which would allow for more frequent collision checks, and thus avoid the Overstep.
This also worked very accurately, it even allowed for a dynamic variable that could be changed. If I wanted more accuracy, I could just change the step number and make take more steps within one frame. BUT this drained the FPS and made the program run slower. It is highly inefficient cause now every frame you have 5 (or more) movements and collision detections instead of one, which demands CPU-power.

So I’m still trying to find a fairly efficient method, that is more importantly accurate. If you guys have any suggestions to the methods already described or any techniques of your own, please do share.

Thanks for listening guys!