2

I'm creating 2D platformer game. I'm having an issues with my collisions. When my character hits is standing on top of a platform it will stay there for 2.5 seconds then fall through all other platforms to the ground floor. I think it has to do something with my gravity function and collision function not working together properly. I really can't figure this out any help would be appreciated.

this = fireboy1

Here's gravity code from my character class:

public var gravity:int = 0;
public var floor:int = 461;

public function adjust():void
    {
        //applying gravity
        this.y += gravity;
        if(this.y + this.height <floor)
            gravity++;
        else
        {
            gravity = 0;
            this.y = floor - this.height;
        }

and here is the code for my collisions from the main class:

//collision detection of platform1
    public function platform1Collision():void
    {
        if(fireboy1.hitTestObject(Platform1))
        {
            if(fireboy1.y > Platform1.y)
            {
                fireboy1.y = Platform1.y + Platform1.height;
            }
            else
            {
                fireboy1.y = Platform1.y - fireboy1.height;
            }
        }
Callum Singh
  • 69
  • 1
  • 8
  • try to do : if((this.y + this.height) < floor) gravity++; ... – tziuka Dec 18 '14 at 19:53
  • that didn't change anything at all :( – Callum Singh Dec 18 '14 at 20:08
  • 1
    just a tangent, but it's much easier to use a physics framework for this. Like box2d etc. Does `adjust()` and `platform1Collision()` run every frame? Ideally they should be in the same function and you should NOT run the gravity (`this.y += gravity`) if the collision occurs. Update your question to include the scope of your code (eg what is `this` in the first block, and how does it relate to the second block of code) – BadFeelingAboutThis Dec 18 '14 at 20:10
  • 1
    does `firebox1` have a registration point of top left (`0,0`)? if so, it seems wierd to check for `if(fireboy.y > Platform1.y)` as that would mean the top of the player would have to be past the top of the platform. – BadFeelingAboutThis Dec 18 '14 at 20:21
  • ahh ok, this still doesn't stop the problem of my player falling through the platforms after 2 seconds though – Callum Singh Dec 18 '14 at 22:22
  • Did you resolve the issue? If so, please either accept an answer (if it led to your resolution), or provide your own answer to the question if a different approach was needed. – BadFeelingAboutThis Dec 29 '14 at 17:23

1 Answers1

0

Your issue is likely that the y position is being incremented every frame (regardless of any collisions)

A better approach, would be to create one game loop / Enter Frame handler, instead of having one for the player, one for each platform, etc. You also had some incorrect math with calculating players position relative to the platform and floor.

public var gravity:int = 0;
public var floor:int = 461;

//add this in your constructor or init of the class
this.addEventListener(Event.ENTER_FRAME, gameLoop);

function gameLoop(e:Event):void {

    //start with all your platform collisions
    if(fireboy1.hitTestObject(Platform1))
    {
        if(jumping){ //some flag that indicates the player is jumping at the moment
           //place fireboy right under the platform
           fireboy1.y = Platform1.y + Platform1.height;
        }else{
           //place fireboy on the platform perfectly
           fireboy1.y = (Platform1.y + Platform1.height) - fireboy1.height; //changed your math here
            return; //there's no use checking any other platforms or doing the gravity increment, since the player is already on a platform, so exit this method now.
        }
    }

    //any other platform checks (should be done in a loop for sanity)

    //if we made it this far (no returns), then do the gravity adjustments
    fireboy1.y += gravity;
    if(fireboy1.y - fireboy1.height < floor){  //changed your math here
        gravity++;
    } else
    {
        gravity = 0;
        fireboy1.y = floor - fireboy1.height;
    }
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • this works, but when fireboy1 hits a platform from underneath it jumps on top of the platform – Callum Singh Dec 19 '14 at 12:43
  • I see. You can either do what you were before (use the position to determine if the platform is above or below the player), or a better way would be track the direction of the player (are they moving up or down) and based the placement off that. – BadFeelingAboutThis Dec 19 '14 at 17:23