krabban Posted January 27, 2016 Share Posted January 27, 2016 Hi! I want to check if a coordinate is occupied before I spawn a new sprite. Are there any easy ways of checking if a sprite occupies a given coordinate? Something like: if( !isOccupied( x, y ) ) { //Do something } Any help or pointers would be greatly appreciated. Link to comment Share on other sites More sharing options...
drhayes Posted January 27, 2016 Share Posted January 27, 2016 How many sprites do you have? You probably want to iterate over them in a for loop and see if they are in that space. Link to comment Share on other sites More sharing options...
krabban Posted January 28, 2016 Author Share Posted January 28, 2016 Sorry, I didn't phrase my question as well as I should have. if(x and y equals sprite.x and sprite.y) { // Do something } If I'm understanding this right, this snippet only returns true if my x and y equals the sprites x and y. This might return false even if my x and y are within the sprites collision body. I need something like: if( !(x and y are within sprite.collisionBody) ){ // Do something } But I'm unsure what to compare against. Link to comment Share on other sites More sharing options...
drhayes Posted January 28, 2016 Share Posted January 28, 2016 On 1/27/2016 at 7:28 AM, krabban said: I want to check if a coordinate is occupied before I spawn a new sprite. If you want to do this you need to check every sprite in your game and see if it overlaps that point. If you've enabled physics for your sprite then it has a property called "body" that has properties x, y, width, and height. Those form a rectangle that you can use to check for intersection with your point. Link to comment Share on other sites More sharing options...
in mono Posted January 30, 2016 Share Posted January 30, 2016 if (!(sprite.getBounds().contains(x, y))) { // Do something } ... is probably what you're looking for. getBounds() returns a rectangle on which you can call contains() with x and y. spinnerbox, Yehuda Katz, Umz and 1 other 3 1 Link to comment Share on other sites More sharing options...
Recommended Posts