Search code examples
actionscript-3collision-detectioncollision

Can anyone help with the collision detect for my platformer? (Actionscript 3.0)


Im currently working on making a flash platformer engine...but my collision detect needs some serious help. Whenever my character 'jumps', and lands on the collision object, he goes about halfway through it for a split second, then goes back to the top (where I want him to be). If I continue to jump multiple times, the shadow of him, if you will, that appears for a split second goes further and further into the collision object, eventually making him fall all the way through it. Here's the code for my main class, and if you need me to clarify anything, please ask.

package {


import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Stage;
import Player;
import HitObject;

public class TestGame extends MovieClip {

    private var _hitObject:HitObject; 
    private var _player:Player;
    private var _leftArrow:Boolean;
    private var _rightArrow:Boolean;
    private var _upArrow:Boolean;
    private var _hit:Boolean;
    private var _fall:Boolean;
    private var _jump:Boolean = true;
    private var _velR:Number = 0;
    private var _velL:Number = 0;
    private var _scale:Number = .1;
    private var _jumpCount:Number = 0;
    private var _i:Number = .5;
    private var _i2:Number = 7;
    private var _i3:Number = 0;
    private var _adjustHit:Number;
    private var _jumpRL:Number;
    private var _jumpVel:Number;

    public function TestGame() {
        _hitObject = new HitObject();
        _player = new Player();
        addChild(_hitObject);
        addChild(_player);
        _hitObject.x = (stage.width * 0.5) + 50;
        _hitObject.y = (stage.height * 0.5) + 150;
        _hitObject.scaleY = 3;
        _hitObject.alpha = .5;
        _player.scaleX = _scale;
        _player.scaleY = _scale;
        _player.x += 200;
        _player.y += 250;
        stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    }

    private function enterFrameHandler(e:Event) {
        if(_player.hitTestObject(_hitObject)) {
            _adjustHit = _hitObject.y - _hitObject.height/2 - _player.height/2;
            _player.y = _adjustHit;
            _hit = true;
            _jump = false;
            if(_i3 > 8) {
            _jump = true;
            }
            _jumpCount = 0;
            _i2 = 7;
            _fall = false;
            _i3++;
        }

        else if(!_player.hitTestObject(_hitObject) && !_upArrow) {
            _fall = true;
        }


        if(_fall) {
            _player.y += _i;
            _i += .5;
            if(_i < 3) {
                _i = 3;
            }
            _hit = false;
        }
        if(_upArrow && _hit && _jump) {
            if(_velR > 0) {
                _jumpRL = _velR;
                _jumpVel = _velR;
            }
            else if(_velL > 0) {
                _jumpRL = -_velL;
                _jumpVel = _velL;
            }
            else {
                _jumpVel = 1;
                if(_player.scaleX == .1) {
                    _jumpRL = 1;
                }
                else {
                    _jumpRL = -1;
                }
            }

            _player.y -= _i2 + _jumpVel/2;
            _player.x += _jumpRL/2;
            _jumpCount += _i2;
            _i2 -= .5;
            _fall = false;
            if(_i2 < -3) {
              _jumpCount = 61;
            }
            if(_jumpCount > 60) {
                _i2 = 7;
                _jump = false;
                _fall = true;
                _jumpCount = 0;
            }
            _i3 = 0;
        }
        if(_rightArrow) {
            _player.startRun();
            _player.scaleX = _scale;
            _velL = 0;
            _player.x += _velR;
            if(_velR < 20) {
                _velR += 2;
            }
            if(_velR > 20) {
                _velR = 20;
            }  
        }

        else if(_leftArrow) {
            _player.startRun();
            _player.scaleX = -_scale;
            _velR = 0;
            _player.x -= _velL;
            if(_velL < 20) {
                _velL += 2;
            }
            if(_velL > 20) {
                _velL = 20;
            }  
        }

        if(_velR > 0) {
        _player.x += _velR;
        _velR -= .7;
        }
        else if(_velL > 0) {
        _player.x -= _velL;
        _velL -= .7;
        }
        if(_velR < 0 || _velL < 0) {
            _velR = 0;
            _velL = 0;
        }
    }

    private function keyDownHandler(e:KeyboardEvent):void {
        if(e.keyCode == 39) {
        _rightArrow = true;
        }
        if(e.keyCode == 37) {
        _leftArrow = true;
        }
        if(e.keyCode == 38) {
        _upArrow = true;
        }
    }
    private function keyUpHandler(e:KeyboardEvent):void {
        _upArrow = false;
        _rightArrow = false;
        _leftArrow = false;
    }
}

}


Solution

  • If you just want easy-to-use collision detection, take a look at the Collision Detection Kit for Actionscript 3. I've used it before and it beats every other collision detection framework I've ever used.