Jump to content

Icy Tower like platform collisions


Recommended Posts

Hi everyone!


I want to create an Icy Tower like game where the player has to jump upwards over a series of platforms. How can I achieve that the player can jump through a platform that is above and then land on it?


I had a look at Physics.Arcade.Body.checkCollision but it seems not to do what I intend.


My platforms and the player are all Phaser.Sprite instances (no tilemaps).

Link to comment
Share on other sites

I have an idea how it can be done but I'm stuck again - I want to have one group that contains all platforms above the player (no collision) and below the player (with collision).

When the player jumps, platforms will be shifted to the corresponding group.

Unfortunately, I can't get it to work - here's the basic code:

var Level = function(state) {  var self = this;  this.platforms = {    above: state.add.group(),    below: state.add.group()  };  // this.data is loaded from a JSON file and contains the platform coordinates and textures  this.data.platforms.forEach(function(data, i) {    tempSprite = self.platforms.above.create(data.x, data.y, data.texture);    tempSprite.body.immovable = true;  });};Level.prototype.constructor = Level;// called in the state update functionLevel.prototype.update = function(player) {  var y = player.body.bottom;  // move platforms to colliding group  var shiftPlatform = function(platform) {    if(platform.y > y) {      this.platforms.above.remove(platform); // generates error      // this.platforms.below.add(platform);      //console.log(this.platforms, platform); // works if line above is commented out    }  };  this.platforms.above.forEachAlive(shiftPlatform, this);};

With this code it produces this error Uncaught TypeError: Cannot read property 'alive' of null

The group iteration fails as soon as the group is modified - if only the console.log statement is left it works without error and all variables are logged correctly.

I've been trying this for two days now in different combinations (regular for-loop, group.forEach, group.iterate) and nothing worked.


What am I doing wrong here?

Link to comment
Share on other sites

  • 2 weeks later...

Ooh, your idea of swapping platforms between groups is a good one. I was looking to achieve the same effect in my platformer, and your solution works great.


Regarding your 'Uncaught TypeError' error, did you ever manage to sort this issue out?


I don't know if it's a bug in Phaser or not, but it seems that the forEachAlive loop seems to loop too often. The ".remove(platform)" method gets called more than once for each platform. This means that the a platform gets removed, and then gets removed again, which of course throws an error as the sprite no longer exists in that group. ( I think that's what is happening anyway. I'm still new to OO JS and Phaser, so please correct me if I'm wrong).


Anyway, I "fixed" it by using a for loop instead:


Instead of:

this.platforms.above.forEachAlive(shiftPlatform, this);

Use a for loop:

for (i=0; i < this.platforms.above.countLiving(); i++){shiftPlatform(this.platforms.above.getAt(i));}

Thanks :)

Link to comment
Share on other sites

So you are working with phaser < 1.2 - can't you just set a custom collision process handler that only returns true when the player is not moving up?

How do you set collisions in 1.1.x ?


Function collisionHandler(){if (player.velocity.y < 0){ return false;}else{return true;}}

Link to comment
Share on other sites


  • Recently Browsing   0 members

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