JLevstein Posted February 8, 2014 Share Posted February 8, 2014 Hi there! I'm having an issue with trying to figure out best practices with creating multiple sprites on a screen. I've been using this blog post as a reference for how to structure my game into classes: http://toastedware.com/?p=258 and so far it's been great. I like how neat it leaves my code. However, I've been trying to create multiple instances of a class and whenever a new instance appears, the previous one loses all of its properties and just drops into the ether. Any advice on how to move forward? I've tried creating a group, but I'm not sure how to pass the class into it. For reference, here is my game so far: https://github.com/JLevstein/tigertail/tree/master/js Link to comment Share on other sites More sharing options...
jpdev Posted February 9, 2014 Share Posted February 9, 2014 hi JLevstein, The problem is the following: You have one global variable named "tiger". You assign a new Tiger Instance to this variable (in index.html preload). Now your level.js calls the tiger.create() function this happens: the one global variable tiger has one sprite variable, which is initially null.So on the first call, your one global tiger creates a sprite, saves it in the empty sprite variable, and goes on to run through the level happily. But now level.js calls create again (we want another tiger!) .. but it calls this on the one global tiger.. So we enter the create function of this (already happy active tiger) and you create another sprite and assign it to the sprite variable.. (which already has a sprite in it).So the new tiger sprite is now managed by the tiger routines.What happens to the old sprite? - it's just a sprite, it does not know better then to fall through the screen (since no collision is called for it any longer, since it's not referenced by the one global tiger anymore.) Okay, I hope you get the picture. Now for the fix: Let's not have one global tiger - but a list of all our tigers. I'll fork your game and create a patch, hopefully it will work out.. I'll post back when I am done. JLevstein 1 Link to comment Share on other sites More sharing options...
jpdev Posted February 9, 2014 Share Posted February 9, 2014 Here is the changed code: https://github.com/JLevstein/tigertail/pull/1 It's not perfect, I tried to not change too much of your code. Now all tigers are controlled by the code. They are now all in a phaser group. JLevstein 1 Link to comment Share on other sites More sharing options...
JLevstein Posted February 9, 2014 Author Share Posted February 9, 2014 Thank you! This worked perfectly! Your explanation really helped me to understand how this works. Link to comment Share on other sites More sharing options...
Recommended Posts