Select Nearest Sprites

0 favourites
From the Asset Store
A set of retro 16-Bit Neon UI elements to make your menus pop!
  • I'm having a bit of trouble with this code.

    Object Selection

    I want to be able to click on a sprite, and have the closest X number of similar sprites selected as well.

    In my example CAPX, I have a grid which is divided by a wall. On the left is a textbox that will allow you to alter a number. Note that when you click on a sprite, the closest X other objects that the initial selection can "see" will be selected.

    This is great if you want to do a grid based 'fog of war' type of game, but I literally just want to do it with selection. I have the wall in place because I want to select the objects within one 'room' before selecting objects in the next, rather than it just spreading out in an area around the initial selection.

    As it stands, if the initial selection can't see the next room, none of the sprites in that room will be selected, no matter how high the X number selection is.

    Does anyone know of a clever way to achieve this?

    Going to be cheeky and tag in R0J0hound on this one, seeing as I was helped a lot the last time

  • Think of it as water slowly pooling out from where you initially select. I only used line of sight to ensure it doesn't 'flow' through walls.

  • You could use "pick closest", and compare distance() using a function and variable filtering.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could use "pick closest", and compare distance() using a function and variable filtering.

    Thanks for your message, but that is what I've already done in the capx I posted a link to.

    It doesn't help in regard to the 'flow' effect I'm looking for.

  • So you want a flood fill?

  • So you want a flood fill?

    Ahh, it has a name! Much better than my use of the word "flow". Heh!

    I've been changing the code here and there, and managed to get something close to what I want, but it requires a loop with a "Wait 0 Seconds" included to force the next skip. Every time I remove the 'wait' it stops working. I'm trying to store the position of each Flooded Tile, so perhaps the wait is useful.

    Do you know of a better way to do what I'm looking for?

  • I would go with some recursive function using "is over lapping", which is where it would all go horribly wrong.

    Luckily R0j0 will be along shortly to do it correctly.

  • I would go with some recursive function using "is over lapping", which is where it would all go horribly wrong.

    Luckily R0j0 will be along shortly to do it correctly.

    See, I did think of doing it with overlapping, in fact, I started to do it using offsets, but I can very rarely get them working, and fear that I'm likely to use too much CPU checking so many collisions.

    I've updated the original post's capx to show the new version.

    Now, for some reason, which I can't figure out, nor replicate using code... If I multi-click a tile, the code appears to run faster. Why might that be?

  • Here is my attempt:

    https://www.dropbox.com/s/xtyt0o9icn9jr ... .capx?dl=0

    Not sure if this is how you want it to work, but my version doesn't require "waits". And I also don't like using arrays <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

  • I can’t open the capx right now, but Here’s another idea. I looked a your first capx briefly when I was at my computer.

    Give the grid sprite a number instance variable and call it n. You’ll also want the origin of the sprite to be centered.

    Also create another sprite and call it detector. Make it’s origin to the left and make it’s size 32,4. I’m assuming the grids are 32,32.

    Global n=1
    
    On click
    —set n to 0
    —set grid.n to 0
    —if mouse is over grid
    ——add 1 to n
    ——set grid.n to n
    
    On click
    Repeat 50 times
    —If grid.n=loopindex+1
    ——set grid.opacity to 50
    ——set detector position to grid
    —Repeat 4 times
    ——Set detector.angle to 90*loopindex
    ——if detector not overlapping wall
    ——if grid overlapping detector
    ——if grid.n=0
    ———add 1 to n
    ———set grid.n to n[/code:3d5uzgqz]
    
    Change the repeat to the number of grids to highlight. Also I used opacity to do the highlighting but you can use anything.
  • Here is my attempt:

    https://www.dropbox.com/s/xtyt0o9icn9jr ... .capx?dl=0

    Not sure if this is how you want it to work, but my version doesn't require "waits". And I also don't like using arrays <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    Oh that is clever. I completely forgot that a sprite views it's family version of itself as a different thing.

    I'm now trying to push it a little bit further, having the flooded parts remain in a second click, but spreading. By that, I mean, say you're using a flood of 5. When you click on a tile, 5 other tiles are highlighted. Then if you click the exact same tile, 5 additional tiles appear, spreading out from the additional flood, meaning you're now covering 10 tiles.

    So no matter where you click, the flood will form from the clicked tile, but consider every already flooded tile as already flooded, and just move past it to an unflooded space.

    I've changed the code enough to get this working with 2 tiles, but after 4 clicks, it won't spread past it. I think it's something to do with the TileFloor.Width*1.1, but can't see why.

    I've uploaded your example which I amended here.

    http://bearboxmedia.com/apps/Temp/flooding_.capx

  • I can’t open the capx right now, but Here’s another idea. I looked a your first capx briefly when I was at my computer.

    Give the grid sprite a number instance variable and call it n. You’ll also want the origin of the sprite to be centered.

    Also create another sprite and call it detector. Make it’s origin to the left and make it’s size 32,4. I’m assuming the grids are 32,32.

    Global n=1
    
    On click
    —set n to 0
    —set grid.n to 0
    —if mouse is over grid
    ——add 1 to n
    ——set grid.n to n
    
    On click
    Repeat 50 times
    —If grid.n=loopindex+1
    ——set grid.opacity to 50
    ——set detector position to grid
    —Repeat 4 times
    ——Set detector.angle to 90*loopindex
    ——if detector not overlapping wall
    ——if grid overlapping detector
    ——if grid.n=0
    ———add 1 to n
    ———set grid.n to n[/code:3rd394h9]
    
    Change the repeat to the number of grids to highlight. Also I used opacity to do the highlighting but you can use anything.
    

    Thanks R0j0. Wouldn't the overlapping detector use more cpu than dop2000's above method?

    Imagine my grid is 16/16 made up of hundreds of tiles, with lots of flooding. I usually find that when I included collision or overlapping in my games, things start to slow down rather quickly.

  • I think this may be solved by replacing the mentioned distance event with a "nearest" event.

  • AnD4D

    I Love challenges so I give it a try

    Capx:

    https://www.dropbox.com/s/vu4b0v2dmgbkfoe/LimitedFlood_1.capx?dl=0

    This is based on what I understood but maybe I understood it wrong though

  • AnD4D

    I Love challenges so I give it a try

    Capx:

    https://www.dropbox.com/s/vu4b0v2dmgbkfoe/LimitedFlood_1.capx?dl=0

    This is based on what I understood but maybe I understood it wrong though

    Not bad, but if you click on one tile, then click on one far away, the original tiles continue to 'flow'.

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