Jump to content

Allowing player to jump through platforms from underneath.


owen
 Share

Recommended Posts

I'm creating a simple platform game using Tiled and Phaser.  I have reached the stage where I am moving my player around the screen jumping on platforms.  So far so good, he moves, he jumps, the game scrolls nicely.

 

I need to allow the player to jump up through the bottom of some (not all) platforms instead of colliding with their underbelly. Basic platforming really.

 

I have got a feeling this is easy but I just don't know how to do it and can't find it anywhere.

 

Any ideas?

 

Thanks.

Owen

Link to comment
Share on other sites

That sounds great but how can I do this in a 'group' context?  For example set collideDown to false for tiles a, b and c in my tilesheet?

 

Say I have a bunch of specific tiles in my tilesheet which I want this behaviour to apply, wherever they appear in my map.

Say it is the 1st, 4th, 5th and 6th tile in the sheet.  Wherever these appear in my map, I wand collidedDown = false.  What would be the syntax?  Sorry but I can't seem to figure it out from the documentation.  Ugh.

 

Thanks

Owen

Link to comment
Share on other sites

Using getTile will retrieve specific tiles, but if you want to change all tiles of a certain type forEach will let you loop through all tiles and do something with each tile, so you could do this:

map.forEach(function(tile) {  if (tile.index === 1 || tile.index === 4 || tile.index === 5 || tile.index === 6) {    tile.collideDown = false;  }}, this, 0, 0, map.width, map.height, layer);

There may be other easier ways to do this - I have to admit I'm not an expert with Tilemaps and I've not really used them myself.

Link to comment
Share on other sites

Thanks, I will give it a try.  Looks straightforward.  Presumably the indexes on the tilesheet are from left-to-right eg. if my tilesheet was 10x10 the indexes would be 1,2,3,4,5,6,7,8,9,10 for the top row and then 11,12,13...etc... for the second row etc?

Link to comment
Share on other sites

No, the index is the tile index, which corresponds to the tile in your tileset, so each index would represent all tiles of a particular type in your map. I presume this is what you want? If not, just use getTile which will allow you to manipulate a tile at the specified map coordinates.

Link to comment
Share on other sites

Yes, that's what I meant sorry.  The index of tiles in my tileset.  (or tilesheet as I wrongly called it).  If my tileset contains a tile that I want to be passable from underneath then I want to set all isntances of that tile to behave the same (passable from underneath) within the map.

 

I was just asking really how the tileset was indexed. For example does the first tile have an index of 0?   Just so I know that I'm refering to the correct tiles in my tileset when I do this. 

 

Anyway I'll give it a try today and post the results.

 

Cheers

Owen

Link to comment
Share on other sites

Before I go ahead, I presume this needs to go at the end of the create() function yes?   For starters I will apply collideDown=false to every tile in the map.  This ought to get it working initially surely?

 

I'm thinking:

map.tiles.forEach(function(t) {   t.collideDown = false;}, game, 0, 0, map.width, map.height, layer);
Link to comment
Share on other sites

Hi again, sorry about this but for some reason it does not seem to be working.  There are no errors and the game runs, but no matter what I do I cannot get the player to jump through the platforms.  The property tile.collideDown seems to be simply having no effect.

 

You can see how far I've got here: http://www.owensouthwood.com/mrgoggles/

 

Here is my create function with the collideDown bit at the end.  I probably have some stupid typo in there but I can't see it. :-(

 

function create() {    // tilesprite, for scrolling background    tilesprite = game.add.tileSprite(0, 0, 800, 600, 'mytilesprite');    tilesprite.fixedToCamera = true;    // start physics engine    game.physics.startSystem(Phaser.Physics.ARCADE);    // add tilemap, tileset and layer    map = game.add.tilemap('mytilemap');       map.addTilesetImage('mytilesheet');      layer = map.createLayer('Tile Layer 1');      layer.resizeWorld();    // not sure what this does but seems needed to initialise collision?    map.setCollisionBetween(0, 100);        // add player sprite and make camera follow him, and enable physics on the player    player = game.add.sprite(44, 64, 'player');    game.camera.follow(player);    game.physics.arcade.enable(player);    //  player physics properties. Give the little guy a slight bounce.    player.body.bounce.y = PLAYER_BOUNCE;    player.body.gravity.y = PLAYER_GRAVITY;    player.body.collideWorldBounds = true;    //  Our two animations, walking left and right.    player.animations.add('left', [0, 1, 2, 3], 10, true);    player.animations.add('right', [5, 6, 7, 8], 10, true);    // make platform tiles possible to jump through from underneath << This doesn't work, I can't get collideDown to be set    map.tiles.forEach(function (t) {        t.collideDown = false; // << this line is definitely reached... but has no effect!            }, game, 0, 0, map.width, map.height, layer); // << what about these params are they right?  (I cahanged 'this' to 'game')}

Anything obvious leap out as wrong? 

 

Cheers!

O.

Link to comment
Share on other sites

Not entire sure what's going wrong with your code but the forEach you're using there should be called on the map object, and 'this' should be left as-is. However I've just tried it in your game, and it's not working as intended for some reason. Luckily, the following does work:

map.tiles.forEach(function(tile) { tile.collideDown = false; });

Because then you're iterating through the tiles array directly. I assume this will also work the way we discussed earlier, with you only setting it on tiles matching a specific index.

Link to comment
Share on other sites

Sorry Lewster but that doesn't seem to have done anything.  It seems to iterate through just fine but does nothing at all in terms of allowing the player to jump through the tiles.  Every tile remains impassable from every angle.

I wonder if something elsewhere in my code is breaking it.  Maybe something else in the create function or even the update function. Hmmm.

Link to comment
Share on other sites

Hang on... just did another test...

    // attempt to allow player to 'jump through' ALL the tiles from underneath (does not work yet)        map.tiles.forEach(function (tile) {        alert("got tile"); // << this is reached        tile.collideDown = false;        alert("set collideDown"); // << this line does not get reached!!!!    });

...but then did same test again and it did reach the 2nd alert statement.... yet did not appear to have any effect.... so confused now.  I will take a break and look again later.  :-)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...