0

I'm creating simple flash game. For now I'm coding character's control. I've added to character collisions with the ground & walls, but can't successfully add collisions with the ceiling, character jumping through the ceiling. Could you help me, please? Thank you.

Here is part of code, what I've tried:

      if(space){
                            if(myCollisionsList.checkCollisions().length > 0) {
                                if (hitTestPoint(hero.x + 28, hero.y, true)){
/////////////////////////////////////////////////////////////////////////////////////
                            //here I need to add any code for character's collision with the ceiling.
/////////////////////////////////////////////////////////////////////////////////////

Hero.y_speed = 0; // this doesn't help me

                        }else{
                            Hero.y_speed = -jumpspeed;
   }

I have created myCollisionsList where I keep all stages, walls for collisions with character. This method working for collisions when character is moving to right or left, because I just set character's x_speed to "0" when It touching the wall. But how can I stop It when touching the ceiling? Set character's y_speed to "0" doesn't help for me.

Image explains what's going on for now: jumping

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Can you please be a bit more specific about your problem? Do you get an error? `Hero.y_speed = 0;` should throw an error, as your character instance is clearly "hero". Are you using hitTestPoint against a big displayObject with its children as obstacles, or are you trying to call hitTestPoint on every obstacle instance stored in myCollisionsList? – golyo Sep 16 '13 at 14:08
  • No I don't get any error, just when jumping I need to add collision. I have used `private var Hero:hero = new hero;` I'm using hitTestPoint on every obstacle instance stored in myCollisionsList, because I don't know how to make as you said "big displayObject with its children", but I would like to use this method. –  Sep 16 '13 at 15:25

1 Answers1

0

For collision replace your character with rectangle and test the distance between this rectangle and all in myCollisionsList. This is very simple solution but for rectangle shapes it is perfect. If your speed is low you can use pixel perfect collision: check this for example http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/

Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22
  • When I use this I got error code `Error #1009: Cannot access a property or method of a null object reference.` –  Sep 16 '13 at 15:57
  • What use? How use? The rectangle describes your character is ( hero.x, hero.y, hero's width, hero's height ) so if you have obstacle at position x,y with size width and height its rectangle will be ( x,y,width, height ). To find the distance check read this for example: http://stackoverflow.com/questions/4978323/how-to-calculate-distance-between-two-rectangles-context-a-game-in-lua – Azzy Elvul Sep 16 '13 at 16:07
  • But what function I need to use that character not jumped through the rectangle? –  Sep 16 '13 at 16:28