VizuaaLOG Posted January 2, 2015 Share Posted January 2, 2015 Hi Phaser Devs,I'm having trouble with something in the game I am currently creating. The concept of the game is that circles randomly spawn on the screen and begin to grow in size, this I have achieved using a tween object to scale the sprite. However the game is about taping the circle to get points, the bigger the circle the more points you get, but if two circle collide then it's game over.The problem I'm having is checking to see if the space where the circle wants to spawn is free, otherwise it would spawn on an existing circle causing the game to just end even though the player had no chance of preventing the loss. I have a group created which contains 20 circle sprites. I then after a set amount of time get the first dead circle in the group set its scale to 0 and reset its x and y position to a random place on the screen, then starting the tween to make the circle grow. However I can't figure out how to make sure the x and y positions are not already filled with a circle and if it is then randomly create the x and y positions again.Is there any way to do this using phaser? I did think that maybe creating an array to keep a log of the circles on the screen as well as their x, y and scale them using custom maths to work out if the x and y position has been taken or would be by the time the circle has spawned. Although I thought that if phaser has a similar feature built in it might be a much faster method.Thanks everyone in advanced, this is really making me pull my hair out . Link to comment Share on other sites More sharing options...
spencerTL Posted January 2, 2015 Share Posted January 2, 2015 I think this might be a bit specialised for Phaser to have a built in facility.Could you create a virtual grid of x and y positions a sensible distance apart? Track these so that you know which are free (2d array). Now you know at least you know starting position doesn't overlap a starting position already used. Each new circle would compare a potential starting point's distance to the centre of each circle with the size of the existing circle's radius. Adjusting for starting size and a margin for expansion you'd know that spot was okayThe reason I think I'd use the grid rather than just a pixel position is that with a few circles on screen choosing random spots based on pixel coordinates might get you a lot of potential start points that fall in a an existing circle. A while loop could have to run a lot before it hits a useful position. A grid lowers the amount of possibilities. You may also be able to make it work where each circle returns the start points it overlaps so you can discount those for the selection to start at. Link to comment Share on other sites More sharing options...
rtlehr Posted January 2, 2015 Share Posted January 2, 2015 Another option (CAVEAT.. I am not a programer, so I do not know if this is an efficient way to do this or not) Ok, so just for simplicity, let's say your stage is 100 x 100 Create a group and put 10x10 transparent sprites in it and lay them out like a chess board (so, there would be 100 of them). This group should be on the bottom of the display. Extend Phaser.Sprite for these sprites to add a isCovered boolean (set it initially to false) Create another group to put all your circles in. Set up a collision for these two groups. When the circle collides with a square sprite flip the isCovered bit to true. Now, before you create a new circle, cycle thru your squares, if the squares isCovered bit is false, then you know its safe to draw circle there. The trick well be to make sure once the circle is gone, to flip its collided with square sprites isCovered bits back to false. I'm not sure if there is a way in Phaser to know all the objects another has collided with, but if there is not a built in way, one way would be to... add an array to the circle class to record all the square sprites that it collides with. When the circle is killed, cycle thru the array and flip them all back to false. If this doesn't make since (as far as what I wrote, then let me know if you have any questions... if it's just dumb, well, chalk it up to a graphics guy trying to program Ross Link to comment Share on other sites More sharing options...
VizuaaLOG Posted January 2, 2015 Author Share Posted January 2, 2015 I think this might be a bit specialised for Phaser to have a built in facility.Could you create a virtual grid of x and y positions a sensible distance apart? Track these so that you know which are free (2d array). Now you know at least you know starting position doesn't overlap a starting position already used. Each new circle would compare a potential starting point's distance to the centre of each circle with the size of the existing circle's radius. Adjusting for starting size and a margin for expansion you'd know that spot was okayThe reason I think I'd use the grid rather than just a pixel position is that with a few circles on screen choosing random spots based on pixel coordinates might get you a lot of potential start points that fall in a an existing circle. A while loop could have to run a lot before it hits a useful position. A grid lowers the amount of possibilities. You may also be able to make it work where each circle returns the start points it overlaps so you can discount those for the selection to start at.Yes, I understand your last point it could mean that a lot of time is wasted trying to find a position to spawn the circle. If I understand what you are saying correctly I would create an array and then have x and y positions for different spawn points. Like below:var spawnPoints = [ [x, y], [x, y], [x, y], [x, y]];But I think wouldn't it also be good to have each spawn point as an object which can then have a value such as available which would be true or false, this way I can just make the spawn point true or false if its available, then on the sprite I could have a reference to the index of the array which it spawned from, so when it is tapped, which would destroy it, it would then make that index of the array available again? If that makes sense. Another option (CAVEAT.. I am not a programer, so I do not know if this is an efficient way to do this or not) Ok, so just for simplicity, let's say your stage is 100 x 100 Create a group and put 10x10 transparent sprites in it and lay them out like a chess board (so, there would be 100 of them). This group should be on the bottom of the display. Extend Phaser.Sprite for these sprites to add a isCovered boolean (set it initially to false) Create another group to put all your circles in. Set up a collision for these two groups. When the circle collides with a square sprite flip the isCovered bit to true. Now, before you create a new circle, cycle thru your squares, if the squares isCovered bit is false, then you know its safe to draw circle there. The trick well be to make sure once the circle is gone, to flip its collided with square sprites isCovered bits back to false. I'm not sure if there is a way in Phaser to know all the objects another has collided with, but if there is not a built in way, one way would be to... add an array to the circle class to record all the square sprites that it collides with. When the circle is killed, cycle thru the array and flip them all back to false. If this doesn't make since (as far as what I wrote, then let me know if you have any questions... if it's just dumb, well, chalk it up to a graphics guy trying to program Ross Thank you for your reply, however, I think it would be too much performance wise. Although they are transparent it would still mean that Phaser needs to continually update them because they still have physics applied. Although on smaller screens it might be fine but if this was released on an iPad which has a much larger screen that would also mean a lot more of these invisible sprites need to be updated every frame. Plus the collision only needs to be checked when a circle needs to be created which is every so many seconds, but gets less and less, but the way phaser works the invisible sprites would still be updated over them 3 seconds which would be a waste of processing time for other tasks. Not sure how accurate my thought is above so if anyone knows that it wouldn't be much of a problem then please let me know. Link to comment Share on other sites More sharing options...
rtlehr Posted January 5, 2015 Share Posted January 5, 2015 Thanks! That is what I am trying to learn now. What is going on behind the scenes that make all this magic work. Link to comment Share on other sites More sharing options...
Recommended Posts