I'm making a 2D platformer game and i'm trying to add collisions to the platforms so that when the character hits it it cannot pass through. I'm struggling to find the syntax to use to create this collision. So far this is what I have.
Also i would still like to be able to use hitTestObject within the if statement.
thanks
public function platform1Collision():void
{
if (fireboy1.hitTestObject(Platform1))
{
//fireboy1 cannot pass through
}
}
You'll probably want to prevent fireboy1
's y
property from extending past Platform1
's y
property:
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;
}
}
}
NOTE: The above code sample assumes top-left orientation for both fireboy1
and Platform1
.
EDITED: The above edited code will allow fireboy1
to walk beneath Platform1
, but not pass through it.
This is a very rudimentary example to give you an idea of the type of logic you can use. If you want to allow fireboy1
to pass through Platform1
from below, you'll have to update the logic to allow for that. For example, if you take out the if/else
and just automatically place fireboy1
above Platform1
every time they collide, it will appear as if player1
is jumping onto Platform1
when it is approached from below.