Jump to content

Spawn Sprites as they get close to player


Recommended Posts


I'm making a game where the player traverses through a fairly large area and occasionally encounters enemy sprites.

I'm adding around 50 enemy sprites at game start and I'm noticing some performance drops, when I remove the enemy sprites the performance is fine.  

There's only about 5 sprites on screen at once, so I'm trying to figure out the best way to spawn enemies as they enter camera range.


After reading  http://www.html5gamedevs.com/topic/16191-is-it-possible-to-make-an-sprite-re-spawn-after-a-short-time/

It looks like creating a handful of sprites and using revive() or changing their x/y coordinates as the player moves around the world is a viable option, but I was wondering if there was another way to do it that doesn't result in a good deal of refactoring for me.

Any ideas to save me some time? :D

Link to comment
Share on other sites

In your game are the enemies scattered around the map outside of the camera boundaries? If so enabling autoculling on the enemy sprites might solve some performance hits. https://photonstorm.github.io/phaser-ce/Phaser.Component.AutoCull.html

If the enemy class do alot in its update method then that might be a problem too. Killing and reviving the enemy when outside camera boundaries is another solution. You should look into object pooling.


Link to comment
Share on other sites

I do something similar - I have a single large area for my entire game, and thousands of entities of various types. I "suspend" all entities that are not relevant to the player (basically I put them in a frozen state, by disabling elements and pausing behaviours). You need to keep some logic running for certain entities to allow things like "opening a distant door remotely", mostly message processing. I also use a FlyWeight pattern to share as much data as possible between entities of the same type. And object pooling too, to minimise the cost of destroying/creating entities ; just re-use pre-allocated ones.

Link to comment
Share on other sites

Thanks guys! 

My enemies are outside the camera boundaries so I tried enabling autoCull on all my sprites, there was a slight performance increase but not as significant as what I was hoping for.

I also tried messing around with kill/revive by putting all the enemies in an array and calling this in the update loop

    function checkSpawn(game){
        for(var i =0; i< this.enemyArr.length;i++){
            var distance = player.y - this.enemyArr[i].y;
            if(distance < 1000 && distance > -1000){

This actually worked pretty well, but I'm weary of performance hits as enemyArr grows.


There is a moderate amount of work for each sprite in it's update loop, so it looks like pooling might be the way to go.


One thing I noticed was each sprite's update method is called regardless if alive/renderable is true or false. It's too bad there's no way to stop a sprite from updating without using destroy()  

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.

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.


  • Recently Browsing   0 members

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