rockcs Posted October 3, 2016 Share Posted October 3, 2016 Hi everyone, I'm new to Phaser and I know this question may be silly but it is really bothering me a lot. Say I have a parameter 'k' in my game. As the game goes 'k' will change according to user input. What I want to do is to add a new sprite to the game world each time k changes. I know that we create all the sprites in the "create" state of the game, but the create function runs only once right? How can add new sprite as the game goes on ? Thanks!!! Link to comment Share on other sites More sharing options...
mattstyles Posted October 3, 2016 Share Posted October 3, 2016 I don't think there's any restriction on when you create and add sprites to the game world, pretty sure you can just add away and during the next update the rendering stuff will become aware of it and render/update it. Maybe have a look through the examples, pretty sure there are a couple that create stuff like bullets during runtime. Link to comment Share on other sites More sharing options...
Taggrin Posted October 3, 2016 Share Posted October 3, 2016 You can do that yes. Personally I use the create function for objects I want to be there when the game starts. The rest is created on the flow. You can simply put a condition in your update function (e.g. button press) to call a function that will add a new sprite, just like you do in the create function. Make sure your conditions are correct though, as otherwise the function that adds the sprite gets called too much and will spam your game with objects. Link to comment Share on other sites More sharing options...
Noid Posted October 5, 2016 Share Posted October 5, 2016 Create if for what you need to get ready for the update loop. There is no need to create all the sprites you are ever gonna need there. You can use the update loop just fine. Link to comment Share on other sites More sharing options...
rockcs Posted October 25, 2016 Author Share Posted October 25, 2016 On 10/3/2016 at 2:46 AM, mattstyles said: I don't think there's any restriction on when you create and add sprites to the game world, pretty sure you can just add away and during the next update the rendering stuff will become aware of it and render/update it. Maybe have a look through the examples, pretty sure there are a couple that create stuff like bullets during runtime. On 10/3/2016 at 8:06 AM, Taggrin said: You can do that yes. Personally I use the create function for objects I want to be there when the game starts. The rest is created on the flow. You can simply put a condition in your update function (e.g. button press) to call a function that will add a new sprite, just like you do in the create function. Make sure your conditions are correct though, as otherwise the function that adds the sprite gets called too much and will spam your game with objects. On 10/5/2016 at 7:33 PM, Noid said: Create if for what you need to get ready for the update loop. There is no need to create all the sprites you are ever gonna need there. You can use the update loop just fine. Thanks everyone! I have solved this problem! mattstyles 1 Link to comment Share on other sites More sharing options...
Recommended Posts