Cutu Posted January 19, 2017 Share Posted January 19, 2017 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! Link to comment Share on other sites More sharing options...
phreaknation Posted January 19, 2017 Share Posted January 19, 2017 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 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 More sharing options...
Cutu Posted January 19, 2017 Author Share Posted January 19, 2017 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 More sharing options...
phreaknation Posted January 19, 2017 Share Posted January 19, 2017 Sure thing. The code might be worth a look at still. If you care, lol Link to comment Share on other sites More sharing options...
samme Posted January 19, 2017 Share Posted January 19, 2017 sprite.reset() and sprite.body.reset() do not re-enable a disabled body (kind of surprising); you have to do it yourself: wall.body.enable = true; wall.reset(x, y); The error is triggered because the sprite reference itself (`wall`) is null when there are no remaining dead blocks. Link to comment Share on other sites More sharing options...
Recommended Posts