A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Is there a better way to do collision detection?

Hybrid View

  1. #1
    Junior Member
    Join Date
    Sep 2009

    Is there a better way to do collision detection?

    Hey flashkit,

    I'm currently building the engine for a platforming game, and am having some problems with my collision detect. It works for the most part...but when my player lands on 'HitObject' (the object I'm having him collide with), he goes all the way through the object, then back to the top, where I want him to be. Now granted, this happens very fast, so it's not that big a deal. What is, however, is that when I jump multiple times, eventually player will fall completely through the object. Is there a better way to do collision detect then what I'm already doing? (if you want to ctrl f right to the code where I do the collision detect, just search for 'f(_player.hitTestObject(_hitObject))'

    And while I'm at it...I might as well ask another question I've been wondering about. Is there a way to add a display object to the display list from your library without explicitly creating a class for it? i.e I have a movieclip in my library, and I just want to bring it onto the stage.

    Here's my code...if it helps.


    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 = 0;
    		private var _adjustHit:Number;
    		public function TestGame() {
    			_hitObject = new HitObject();
    			_player = new Player();
    			_hitObject.x = (stage.width * 0.5) + 50;
    			_hitObject.y = (stage.height * 0.5) + 150;
    			_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 = true;
    				_jumpCount = 0;
    			if(!_player.hitTestObject(_hitObject) && _fall) {
    				_player.y += _i;
    				if(_i < 5) {
    					_i = 5;
    				_hit = false;
    			if(_upArrow && _hit && _jump) {
    				_player.y -= 5;
    				_jumpCount += 1;
    				_fall = false;
    				if(_jumpCount > 15) {
    					_jump = false;
    					_fall = true;
    			if(!_upArrow) {
    				_fall = true;
    		private function keyDownHandler(e:KeyboardEvent):void {
    			if(e.keyCode == 39) {
    			_rightArrow = true;
    			if(e.keyCode == 37) {
    			_leftArrow = true;
    			if(e.keyCode == 38) {
    			_upArrow = true;
    			_rightArrow = false;
    			_leftArrow = false;
    		private function keyUpHandler(e:KeyboardEvent):void {
    			_upArrow = false;
    			_rightArrow = false;
    			_leftArrow = false;

  2. #2
    Junior Member
    Join Date
    Jul 2006
    but bitmapdata based collision detection for exactness.

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Huh? I'm not sure I understand...

  4. #4
    Senior Member
    Join Date
    Oct 2009

    I suggest that you use axis based collision detection rather than hitTestObject. It's a bit more work to implement, but very precise.

    Have a look at page 308 onward from this book:


    hitTestObject won't tell you from which side (top, bottom, left or right) the collision is occuring on but axis based collision will.

    Regarding your second question, no you don't need to create a class for every Library object. Flash does this for you automatically as long as you tell it to export it for ActionScript in the Library's Symbol properties. So if you've got a Symbol called Enemy in the library, just use:

    var _enemy:Enemy = new Enemy();

    Hope that helps a bit? Just ask if you have any more questions or need more detail


  5. #5
    Junior Member
    Join Date
    Sep 2009
    Hey, thank you. That helps a lot, and I'll be sure to check out that book.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center