0 Favourites

How to prevent sprites overlaping each other

  • 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 =/

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • http://imgur.com/5vxKU

    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.

  • http://imgur.com/5vxKU

    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?

  • 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)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
Sprite Font Generator - v3
368 112,280
RetroDaddy's avatar
RetroDaddy
Unread hot topic
86 28,707
St4rDu57's avatar
St4rDu57
Unread hot topic
153 104,430
DravenX's avatar
DravenX