Jump to content

Problem recycling objects.


Cutu
 Share

Recommended Posts

Hey guys its me again, I'm having some issues recyling objects when calling the kill() method.

As you can see in the gif I call the kill() method after the player overlaps with the invisible blocks adding 1 point to the score. the problem is that when they're dead they respawn and stay on top of the screen and after the pool of invisible walls is over the game get stuck giving a "Cannot read property 'reset' of null" because they get stuck.

This is the code of the walls.

//Create

        this.walls = this.game.add.group();
        this.walls.enableBody = true;
        this.walls.createMultiple(250,"wall1");
        this.wallSpeed = 250;
        this.wallsDelay = 1000;
        
        this.timer = this.game.time.events.loop(this.wallsDelay, this.addWallRow, this);

//Update
this.game.physics.arcade.overlap(this.player, this.pointBlocks, (function(player,pointBlocks) { pointBlocks.body.enable = false;
pointBlocks.kill();
this.score += 0.5;
this.scoreText.setText(this.score.toString());
}).bind(this));

//And the create methods of the walls

addWall: function(x,y,immovable) {                
        
        if(immovable) {
            var wall = this.walls.getFirstDead();
        } else {
            var wall = this.pointBlocks.getFirstDead();
        }
        
        wall.reset(x,y);
        wall.body.velocity.y = this.wallSpeed;
        wall.body.immovable = immovable;
       
        wall.checkWorldBounds = true;
        wall.outOfBoundsKill = true;
        wall.events.onOutOfBounds.add((function() {wall.kill(); console.log(wall.exists);}).bind(this));
}

addWallRow: function() {
        
        this.wallSpeed += 5;
        
        var tilesNeeded = Math.ceil(this.game.world.width / this.wallWidth);
        
        var hole = Math.floor(Math.random() * (tilesNeeded - 3)) + 1;
        
        for(var i=0;i<tilesNeeded;i++) {
            if(i != hole && i != hole + 1) {
                this.addWall(i * this.wallWidth, 0, true);
            } else {
                this.addWall(i * this.wallWidth, 0, false);
            }
        }
}

I hope you guys can help me with this one! Thanks in advance! :)

GameTest.gif

Link to comment
Share on other sites

I have a game that I was working on a game a while back and I think you can reverse engineer my projectile code to do what you want
57be9e89546cad9dbdcbc4a7efd459fb.gif

9d41689df0a89cb4cedda2642cbbf056.gif

Player.prototype.createProjectile = function(weaponGroup, weaponType, options) {
  var projectile = false;
  var projectileType = weaponGroup.toUpperCase() + ':' + weaponType.toUpperCase();
  if (this._projectiles.indexOf(projectileType) != -1) {
    if (Object.keys(this.weapons[weaponGroup.toLowerCase()]).indexOf(weaponType.toLowerCase()) !== -1) {
      var group = options.group;

      projectile = group.getFirstDead();
      if (projectile === null) {
        projectile = new Projectile(this, options);
      }

      projectile.events.onKilled.add((function() {
        if (projectile.trail) {
          if (projectile.drunkTimer) {
            projectile.drunkTimer.stop();
          }
          projectile.trail.kill();
        }
        if (this.thrust) {
          this.thrust.kill();
          this.thrust.destroy();
        }
        projectile.destroy();
        projectile.options.group.remove(projectile);
      }).bind(projectile));

      projectile.options.group.add(projectile);
    }
  }
  return projectile;
};

 

Link to comment
Share on other sites

Hey @phreaknation thanks for your answer, I already fixed the problem, I had to delete this line

pointBlocks.body.enable = false;

in my overlap callback, because when it calls the reset method in the createWall function the sprite has no body and stay stuck in the top, anyways thank you!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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