Callum Singh
Callum Singh

Reputation: 79

AS3 character falls through platforms, collisions not working properly

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;
            }
        }

Upvotes: 2

Views: 448

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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;
    }
}

Upvotes: 0

Related Questions