Jump to content

Phaser: object pooling attempt


PhasedEvolution
 Share

Recommended Posts

Greetings. Trying to implement object pooling in my isometric game. Here is my code. Any idea why no "swords" are showing up?

 


// methods //

the_sword_item : function () {
    
        //the_sword = items_group.createMultiple(10,'sword'); // for some reason this stops the rest of the game from rendering only showing the background colour so I used a for loop... //

        var swords_number = 10;
        for (var i = 0; i < swords_number; i++) {
        the_sword = items_group.create(20,20,'sword');
        }
        the_sword.scale.x = 0.05;
        the_sword.scale.y = 0.05;
        game.physics.isoArcade.enable(the_sword);
        the_sword.body.collideWorldBounds = true;
        the_sword.body.immovable = true;
        the_sword.checkWorldBounds = true;
        the_sword.outOfBoundsKill = true;
        the_sword.kill();
    },


spawn_item: function(x, y, type) {
  var the_sword;
  if (type == 1) {
  the_sword = items_group.getFirstDead(); 
      if (the_sword) {
         the_sword.reset(x, y);
         return the_sword;
      }
    
  }
  return the_sword;
},

// 

var the_sword, items_group;

// Inside my create function //

 items_group = game.add.group();
 this.the_sword_item();


// Inside my update function //

this.spawn_item(30,30,1); // will give later random X any Y inside viewport but still should work for now...


 

Link to comment
Share on other sites

25 minutes ago, tips4design said:

Because you use swords_group.getFirstDead(). But when you create the sword it's alive not dead, so it won't be returned. Try adding the_sword.kill() at the end of your  the_sword_item_create function

I updated the code. Still not working. However I get the background screen color now.

Link to comment
Share on other sites

45 minutes ago, Milton said:

Well, put up your code somewhere. It's too hard to guess what you did wrong :)

Yeah you are right... Well I examined the code with all my attention and changed it a little bit to make it more simple and effecient. Now it renders the rest of the world but still doesn´t display any sword. Can you please check it again? Thank you...

Link to comment
Share on other sites

8 minutes ago, tips4design said:

What do you mean by not working? You have syntax errors?

Maybe. I am kinda new to programming too so I wouldnt be surprised... However I examined my code once again and made it more simple. The rest of the world is now rendered except for the swords. Don't get the black screen this time...

Link to comment
Share on other sites

I think you're on the right path here, but probably 'kill' is too much. That sets 'invisible', so it isn't going to show up...
As I mentioned before, I don't know Phaser, but maybe try using the 'alive' property.

PS. Maybe your closing bracket is wrong. You need to do everything up to 'kill' in the create block...

Link to comment
Share on other sites

19 hours ago, Milton said:

I think you're on the right path here, but probably 'kill' is too much. That sets 'invisible', so it isn't going to show up...
As I mentioned before, I don't know Phaser, but maybe try using the 'alive' property.

PS. Maybe your closing bracket is wrong. You need to do everything up to 'kill' in the create block...

Well, one of the problems was the for loop. When I created the multiple swords I closed the for loop too early... I think it is almost good. However for a proper check I need to get the bounds of my world set correctly because never actually did that. Well this setworldbounds in an isometric world seems kinda confusing to me

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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