PhasedEvolution Posted September 6, 2016 Share Posted September 6, 2016 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 More sharing options...
tips4design Posted September 6, 2016 Share Posted September 6, 2016 Why do you use both `the_sword` and `the_Sword` ? PhasedEvolution 1 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 3 minutes ago, tips4design said: Why do you use both `the_sword` and `the_Sword` ? Yep, my mistake. Corrected it. However still doesn't work... Thank you anyway Link to comment Share on other sites More sharing options...
tips4design Posted September 6, 2016 Share Posted September 6, 2016 And why do you use `var` when declaring the_sword? Do you want it to be a local or a global variable? PhasedEvolution 1 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 5 minutes ago, tips4design said: And why do you use `var` when declaring the_sword? Do you want it to be a local or a global variable? I forgot to put the global variables before it all. And should be global yep... Link to comment Share on other sites More sharing options...
tips4design Posted September 6, 2016 Share Posted September 6, 2016 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 Link to comment Share on other sites More sharing options...
Milton Posted September 6, 2016 Share Posted September 6, 2016 Create a pool of swords first. So use a group with createMultiple. Then every time you need a sword, use spawn. At the moment you try to create a sword when you haven't even created the group... Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 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 More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 19 minutes ago, Milton said: Create a pool of swords first. So use a group with createMultiple. Then every time you need a sword, use spawn. At the moment you try to create a sword when you haven't even created the group... I think I did that now. But still not working... Link to comment Share on other sites More sharing options...
Milton Posted September 6, 2016 Share Posted September 6, 2016 Well, put up your code somewhere. It's too hard to guess what you did wrong samme 1 Link to comment Share on other sites More sharing options...
tips4design Posted September 6, 2016 Share Posted September 6, 2016 What do you mean by not working? You have syntax errors? Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 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 More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 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 More sharing options...
tips4design Posted September 6, 2016 Share Posted September 6, 2016 Always code with the JavaScript console open if you are new, make sure you don't have any syntax errors first. samme 1 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted September 6, 2016 Author Share Posted September 6, 2016 Just now, tips4design said: Always code with the JavaScript console open if you are new, make sure you don't have any syntax errors first. I am using microsoft edge but most of the times it doesn't show anything ... Is that normal? In the console Link to comment Share on other sites More sharing options...
Milton Posted September 6, 2016 Share Posted September 6, 2016 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 More sharing options...
PhasedEvolution Posted September 7, 2016 Author Share Posted September 7, 2016 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 More sharing options...
Milton Posted September 7, 2016 Share Posted September 7, 2016 Probably the sword isn't using the Isometric worldbounds, since it isn't an IsoSprite. Link to comment Share on other sites More sharing options...
Recommended Posts