how to prevent objects from spawning on top of each other?

0 favourites
  • 11 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • as the title says, how do i make it so that when an object spawns, it doesn't spawn on top of another object?

    edit: just to clarify, in my game, whenever the character sprite comes in contact with the object item that it collects, the item's position is set to another area randomly (basically kinda like the game snake). i want to make it so that when the item randomly spawns, it doesn't spawn on top of the character sprite because the collision polygons don't collide and there's this awkward moment where you have to move the character off the item and then collide it with the item again to collect it.

  • Don't use the "Spawn another Object" method. Instead use "System" - "Create Object" and enter the X ,Y coordinates you want it to show up at.

  • If you want to use spawn you could always give the first sprite multiple image points outside its actual graphic and choose one of these to spawn on.

  • Don't use the "Spawn another Object" method. Instead use "System" - "Create Object" and enter the X ,Y coordinates you want it to show up at.

    If you want to use spawn you could always give the first sprite multiple image points outside its actual graphic and choose one of these to spawn on.

    i made an edit to my post to clarify the situation a bit more. sorry about that :S

  • I think this should do it. Please excuse the sprites...

    This does not use the on collision check because I'm not sure that it would work all the time (I could be wrong). Here you go for my solution:

  • I think this should do it. Please excuse the sprites...

    This does not use the on collision check because I'm not sure that it would work all the time (I could be wrong). Here you go for my solution:

    awesome =] thanks for the reply. can you explain to me what happens?

  • The Is Overlapping triggers the event and Trigger Once ensures it acts only once. You want an overlap check for this because if you use a collision I think it might be possible for a collision to trigger when there is no overlap (I am not sure and could be wrong), I elect to use the same check for both conditions to ensure that the while-loop tests for exactly the same condition (and there is no Is Colliding check). The last thing you want is for the collision to trigger but then the move to fail because there is no overlap. The while loop then works until the overlap condition no longer exists - this could loop once or many times depending on the likelihood of an overlap for each random position change, so there will be a small cpu load associated with this - but as long as your player is not huge and there is a lot of free space then it should be ok. As soon as the enemy moves to a non-overlapping space the while loop stops because the other condition is no longer met.

  • Alternatively you could spawn the object and then in the next step destroy it if it's overlapping whatever obstacle you want it to avoid.

    i.e.

    1) Spawn object somewhere

    2) If object is overlapping obstacle => destroy object.

    You won't see this in the screen because the creation/destruction would have happened before the screen refreshes.

  • Psynaptik - Creating an object takes up system resource (albeit small) and doing so a number of times in one tick can cause a slight delay in the game play, especially on mobiles devices. By unnecessarily creating and destroying sprites you will kill your free memory pretty quickly and cause slowdowns. Imagine that the while loop example above has to loop 100 times before finding a suitable position for the enemy object. In the attached example, one function group moves a sprite 100 times per tick and the other creates and destroys 100 sprites per tick. There's no collision checking here - this is just an example of how much resource is required to create and destroy a sprite. Enable/disable each group and run in the debugger to compare cpu utilization.

    So, if you want to destroy an object on collision then it would be far better to create only one new sprite and then use a while loop as above to position it. The OP was asking about moving a sprite hence the answer was to move the sprite - which is also the most efficient on your memory.

  • Wow, thanks for the correction and detailed explanation. Much appreciated!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Psynaptik - no problem

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)