PhasedEvolution Posted September 30, 2016 Share Posted September 30, 2016 I have set up a function that checks if "X" is alive and if it is alive it kills it and then spawns it. However when I used kill() it didn't actually kill him. However when I set destroy it would kill him. Only that way could I spawn him again. Why is that? Link to comment Share on other sites More sharing options...
lumoludo Posted September 30, 2016 Share Posted September 30, 2016 What do you mean by kill() doesn't actually kill the sprite? It should remove it from view at least. You can use either kill or destroy if you want to. You can destroy everything if you want, and you can still make a working game. The reason you would use kill is because in many cases it will be more efficient. Are you familiar with memory allocation or garbage collection concepts? It would take too long for me to explain it here, but if you don't know how they work, it may be worth doing some googling to research it. The basic idea is creating a new object causes Phaser to do some time consuming work that is hidden from you. If you revive a killed object, you get to skip that expensive step. If you destroy the object and then create a new one in its place, that expensive step has to happen again. It is especially important for games that have objects that are being removed and added often: things like bullets, constantly spawning enemies, pick up items like coins and power ups, etc. If you want to remove a sprite and never use it again, then it is best to destroy it, but if you plan on re-using it, it is a good idea to get comfortable with kill/revive now. If you design the game to use destroy now and then find out that the game is too slow later on, it can be very difficult to refactor the code to use kill/revive in place of destroy. ShadowMoon, jjwallace and PhasedEvolution 2 1 Link to comment Share on other sites More sharing options...
PhasedEvolution Posted October 1, 2016 Author Share Posted October 1, 2016 @lumoludo I searched and understood. Your explanation was also very good. Thank you Link to comment Share on other sites More sharing options...
Recommended Posts