Jump to content

Endless runner - object pool


Recommended Posts

Hello there.


For my first Phaser game I want to create an endless runner. I want to be sure right from the start that everything I'm doing is done in the best possible way.

I know there might be no straight / easy solutions, but I want to know your opinion, as my experience with Phaser is rather short (1 week).

1. Idea 

I'm having one big Phaser.Group called Platforms, which is called right in the main game and that group handles everything when it comes to the groups.

Inside of Platforms you can spawn platform which is also a group. And that group (platform) has many instances of Ground, which is extension of the Phaser.Sprite

// ground.js
class Ground extends Phaser.Sprite {
  constructor(game, x, y, frame) {
    super(game, x, y, 'tileset', frame);

    const BODY_OFFSET = 3;

    this.anchor.setTo(0, 1);
    this.body.immovable = true;
    this.body.allowGravity = false;
    this.body.offset.y = BODY_OFFSET;
    this.body.height -= BODY_OFFSET;
    this.body.velocity.x = -this.game.global.speed;
    this.body.friction.x = 0;


export default Ground;
// platform.js
import Ground from './ground';

class Platform extends Phaser.Group {
  constructor(game, { startX = 0, startY = game.height, width = game.width } = {}) {
    super(game, startX);

    this.settings = { startX, startY, width };
    this.flags = {
      extended: false

    // Platform building loop
    // 1. Prepare variables
    let tileLevel = 1,
        tileFrame = 8;

    // Y axis
    for(let y = startY; y <= this.game.height + this.game.global.tileSize; y += this.game.global.tileSize) {
      // X axis
      tileLevel === 1 ? tileFrame = 8 : tileFrame = 1;
      for(let x = 0; x < width; x += this.game.global.tileSize ) {
        this.add(new Ground(this.game, x, y, tileFrame));


  update() {
    // When off bounds - spawn another platform
    if ((this.centerX < this.game.width) && (!this.flags.extended)) {
      this.flags.extended = true;

    if(this.right < 0) {


export default Platform;

Screen Shot 2016-08-19 at 10.44.43.png

Every little piece of ground has the same velocity and settings. Player collision is checked based on platforms.children.

For now I'm destroying platform when it goes off screen and calling global spawnPlatform function when middle of a platform is on the left border of the screen (to spawn it a little earlier).

And here come my questions and ideas:

2. Ideas & questions

Recently I've learned about Object pools. My platforms were done by doing some random numbers (width, height etc) but I've noticed that even with one platform performance goes dramatically down. I mean maybe not dramatically, but it's not a good sign. There was that "sawtooth" pattern on the memory section (DevTools). 

Screen Shot 2016-08-19 at 18.40.53.png

Here are my ideas:

2.1 Leave it as it is

Platform spawns, platform goes off the screen, platform gets destroyed. 

2.2 Generate many random platforms at the beginning, and then just re-use them

At the beginning game would generate a big array of defined platforms, that would still be much-less random. When the platform goes off the screen it's not destroyed. Instead it's killed. But here comes my problem - you can't kill groups. Only sprites inside (Ground instances) could've been killed. But will it be really a better solution? Killing every ground, moving the platform out of 'living' array, and then when platform gets spawned revive them?

2.3 Generate object pool of Ground instances and use them as 'blocks' for platforms

I think that's pretty much the same as 2.2, but maybe a little bit different. Creating big arrays of different types of blocks, and then using them to build platforms. I think like max 5 platforms will be visible at one moment on the screen so I wouldn't have to create many blocks. Platform goes off - Ground(s) get killed, group destroyed (?)


Also before I end - on the screenshot you can see that sometimes the connections between tiles are visible. I don't think that's the spritesheet case. Maybe something with small delays between creating each platform? Not quite sure. Any ideas?


Another problem lies in gaps between each platform. I guess that's caused by a delay between creating a platform and spawning it.

Screen Shot 2016-08-19 at 15.59.55.png

I want to ensure I'm doing everything right from the beginning, so pardon me if I sound stupid or that's just silly question. Hopefully someone will be able to answer my question and help me out.

Thanks for creating Phaser. Making games have always been my dream, but recently I was focused on the front-end development. Now I can combine those two :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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