How to prevent sprites overlaping each other

0 favourites
  • 12 posts
From the Asset Store
A set of 10 pixel art animated magic effects with icons. 2 Fire, 2 Earth, 2 Wind, 2 Water, Portal, Explosion.
  • Hello,

    I am generating enemies on my map randomly. Basically on start of layout I have

    system create object x random (10,8000)

                         y random (0,500)

    then I have system repeat 20x times. So this ones creates 20 enemies. the problem is that sometimes it generates same or really similar x or y so two or more sprites are touching each other which is something I want to prevent. Is there something I can do?

    I was thinking something like. If there is collision between them then generate x and y again?

  • Without using arrays or anything, then it's your best bet, or do them on a grid by using small integers and multiplying.

    If enemies are 32x32 pix square - I'd so something like :

    X=1024+Int(Random(5))*32

    Y=512+Int(Random(5))*32

  • Upss I had to reedit post right now. I forgot to add random into that. Now it shoud look same like my original code :) sorry

  • Michal11

    You could try adding a subevent with a loop that continues to reposition the object until it's found an empty space..

    This will loop a lot of times though if there is very little space for it to occupy

    Also I haven't tested so it might not work as it is, but it's an idea.

    repeat x times

    -> system create object x random (10,8000), y random (0,500)

       [subevent]repeat (maximum number, just set to 20 or higher) times

                 object is overlapping object

                 ->set object(1) position x random (10,8000), y random (0,500)

                 else

                 -> stop loop

    object(1) is a way to single out one of the object instances after the "object is overlapping object" condition.

    It's not perfect, there is probably a better way.

  • Thank you this is exactly the way I want to do that. Hence I think there must some better way which is the reason I am asking :)

  • if you make the enemies solid then they should be moved outside of each other if they spawn on top of each other. Note I say "should" then if your enemies shouldnt be solid you can turn it off after theyre placed

  • if you make the enemies solid then they should be moved outside of each other if they spawn on top of each other. Note I say "should" then if your enemies shouldnt be solid you can turn it off after theyre placed

    I just tried that quickly but it does not work :) They still overlap each other

  • then you could try just a top level event for is overlapping to move it to some other position. That should fire if theyre created overlapping

    Itll also fire after initial placing so take that into account. Its either that, an array, or the nested loop already mentioned I guess =/

  • This seems to work, not sure its the best answer.

    btw, I started with a sprite on the screen, that's why it only repeats 19 times.

  • This seems to work, not sure its the best answer.

    btw, I started with a sprite on the screen, that's why it only repeats 19 times.

    Excellent I think this is the best way. Thanks a lot mate

  • I cant seem to get this to work - it seems like an object overlapping 'itself' immediately triggers a yes.. so in the case of this last screenshotted example, it just continually tries to generate a new instance of itself, then continually destroys itself, spawning another instance, destroying, etc.. till infinite. Does this happen for anyone else or is there something im missing?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Actually it seems the key is to have smaller sprites... the minute they are larger, all hell breaks loose... what do you guys think? I need to have larger sprites but its like the minute there's one overlap goes nuts.

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