Jump to content

How to add new sprites to the game world when the game is running


rockcs
 Share

Recommended Posts

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

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

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

  • 3 weeks later...
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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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