Author Topic: Multi-layered Tilemap Collisions  (Read 1619 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Multi-layered Tilemap Collisions
« on: Thu, Aug 5, 2010 »
Can anyone suggest a way to give my character the ability to "jump up" a layer of tilemaps? Very similar to the mechanics of Mario; how you can run sideways through tiles that you can get on top of if you jump up.
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Retro-Rob

  • Active Member
  • ***
  • Posts: 164
  • Karma: +0/-0
    • View Profile

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Multi-layered Tilemap Collisions
« Reply #2 on: Thu, Aug 5, 2010 »
http://flashgamedojo.com/wiki/index.php?title=Platform,_One-Way_%28Flixel%29

I've read through that before, but didn't notice the usefulness of precollide. Okay so I'm working with Flan's style of setting up maps and now I'm having a bit of trouble working with these two methods together.

In this bit of code here:
Code: [Select]
override public function preCollide(Contact:FlxObject):void
{
if(_falling && Contact is OneWayTileBlock && (_fallingThrough == null || _fallingThrough == Contact))
{
_fallingThrough = Contact
collideBottom = false
}
else
{
collideBottom = true
}
super.preCollide(Contact);
}

What do I replace OneWayTileBlock with if I want it to be an entire FlxTilemap? The contact needs to be a class (at least that's what my compiler says).
I'm so confused D:
« Last Edit: Thu, Aug 5, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

zez

  • Active Member
  • ***
  • Posts: 203
  • Karma: +0/-0
    • View Profile
    • Devlog
Re: Multi-layered Tilemap Collisions
« Reply #3 on: Thu, Aug 5, 2010 »
make a class, name it something, make it extend flxTilemap, then load your one way tiles in that tilemap (have it be a separate tile map) same way as normal, then adjust that code to be if Contact is (whatever you named that class.)
The custom tilemap class probable doesnt need any special code, other then extending tilemap, so you can basically make it totally empty.
Another thing you could do, is pass a tilemap to the player as a variable, and change Contact is OneWayTileBlock to Contact == the tilemap you passed as a variable, provided you only have one map of one way tiles.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Multi-layered Tilemap Collisions
« Reply #4 on: Thu, Aug 5, 2010 »
make a class, name it something, make it extend flxTilemap, then load your one way tiles in that tilemap (have it be a separate tile map) same way as normal, then adjust that code to be if Contact is (whatever you named that class.)
The custom tilemap class probable doesnt need any special code, other then extending tilemap, so you can basically make it totally empty.
Another thing you could do, is pass a tilemap to the player as a variable, and change Contact is OneWayTileBlock to Contact == the tilemap you passed as a variable, provided you only have one map of one way tiles.

Yeah, I ended up passing the tilemap into my player class. It's somewhat working now. My player is two tile blocks high, so if any of hit hitbox collides with the top of the tile he instantly zips up to the top of the tile. How can I prevent this?
« Last Edit: Thu, Aug 5, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Rybar

  • Contributor
  • ****
  • Posts: 279
  • Karma: +0/-0
    • View Profile
Re: Multi-layered Tilemap Collisions
« Reply #5 on: Thu, Aug 5, 2010 »
If you implement the preCollide stuff for jumping down thru the one-way platform, that should take care of this problem, as preCollide is only triggered when you're above the tile.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Multi-layered Tilemap Collisions
« Reply #6 on: Thu, Aug 5, 2010 »
If you implement the preCollide stuff for jumping down thru the one-way platform, that should take care of this problem, as preCollide is only triggered when you're above the tile.

In order for preCollide to work I need to collide() my platforms tilemap with the player, right? I've tried it with and without. With, it just snaps up to the top of the tile. Without it does no collision at all.
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Multi-layered Tilemap Collisions
« Reply #7 on: Thu, Aug 5, 2010 »
One-way tileblocks are also sometimes called "Clouds", I find it easier to type :P

Here's my player code that prevents my guy popping up onto a cloud while just walking from left to right, basically checks his velocity, and then check his height relative to the cloud block:
Code: [Select]
//in player class
override public function preCollide(Contact:FlxObject):void
{
if(Contact is Cloud)
{
if(velocity.y>100)
{
if((y+20)>(Contact.y+6))
{
collideBottom = false;
} else {
collideBottom = true;
}
} else {
if((y+20)>(Contact.y+2))
{
collideBottom = false;
} else {
collideBottom = true;
}

}
}
else
{
collideBottom = true;
}
super.preCollide(Contact);
}
//in cloud class

collideBottom = false;
collideLeft = false;
collideRight = false;
fixed = true;

// in playstate

FlxU.collide(_player,_clouds);

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Multi-layered Tilemap Collisions
« Reply #8 on: Fri, Aug 6, 2010 »
One-way tileblocks are also sometimes called "Clouds", I find it easier to type :P

Here's my player code that prevents my guy popping up onto a cloud while just walking from left to right, basically checks his velocity, and then check his height relative to the cloud block:
-snip-

Cloud is much easier to say, yeah xD. I'm having trouble with this. When you call Contact.y, what is that returning? The y value of the tile that the player is colliding with?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Jim

  • Guest
Re: Multi-layered Tilemap Collisions
« Reply #9 on: Fri, Aug 6, 2010 »
Yes, the Y value of whatever FlxObject "Contact" is (could be anything you collide with your player)

20 is the height of my player, so it checks the height (from the bottom of my player) against the Cloud. If it's moving fast it will only collided with the cloud if the bottom of my character is in the top 6 pixels of the Cloud.

If I'm moving slowly (like the apex of a jump), it will only check the collision with the Cloud if the bottom of my character is in the top 2 pixels of the Cloud.

Contact is whatever FlxObject the current FlxObject is colliding with. Since I only want this behavior with Clouds I only use this routine if (Contact is Cloud). Since collideBottom is a boolean you need to set it one way or the other for every collision, or else you could get some nasty glitches.