If hitTestObject = true return current position on screen?

131 Views Asked by At

I'm making a 2d platform game and i'm not knowledgeable enough to know how to set up collisions with my character and platforms.

I basically want when fireboy1 hits basePlatform, fireboy1 to not be able to go any further

I have got this code so far i'm just struggling to understand what to put in the if statement itself. I have commented the part I am unsure about.

I'm not sure if this is the right way to go about it.

public function platformCollision():void
    {
        if (fireboy1.hitTestObject(basePlatform))
        {
            fireboy1.y = fireboy1.y = //current position on screen?
        }
    }
2

There are 2 best solutions below

0
On BEST ANSWER

If you don't want fireboy1 to go past the basePlatform, you should probably do something like:

fireboy1.y = basePlatform.y - fireboy1.height

Please note that this all depends on both fireboy1 and basePlatform having top-left orientation.

0
On

no need for a hitTestObject.... lets assume that the regstration point for fireboy and the base platform are in its centre:

public function loop(e:Event){
 if(fireboy1.y + fireboy1.height > basePlatform.y - basePlatform.height){
  fireboy1.y = basePlatform.y - fireboy1.height;
 } 
}