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

Get help using Construct 2

Post » Tue Apr 22, 2014 6:56 pm

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.
Last edited by notalkingplz on Wed Apr 23, 2014 7:52 am, edited 1 time in total.
B
5
Posts: 26
Reputation: 283

Post » Tue Apr 22, 2014 7:41 pm

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.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Tue Apr 22, 2014 7:57 pm

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.
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
33
G
47
Posts: 1,194
Reputation: 30,812

Post » Wed Apr 23, 2014 7:53 am

troublesum wrote: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.



RamPackWobble wrote: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
B
5
Posts: 26
Reputation: 283

Post » Wed Apr 23, 2014 2:13 pm

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:
You do not have the required permissions to view the files attached to this post.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Wed Apr 23, 2014 3:12 pm

Colludium wrote: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?
B
5
Posts: 26
Reputation: 283

Post » Wed Apr 23, 2014 3:21 pm

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.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Wed Apr 23, 2014 3:44 pm

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.
B
6
S
2
Posts: 55
Reputation: 614

Post » Wed Apr 23, 2014 5:31 pm

@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.
You do not have the required permissions to view the files attached to this post.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Fri Apr 25, 2014 8:08 am

Wow, thanks for the correction and detailed explanation. Much appreciated!
B
6
S
2
Posts: 55
Reputation: 614

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 14 guests