How to check nearest sprites (no clones) each time?

0 favourites
  • 6 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • Hello guys!

    I have 1 sprite X 10 (no clones).

    So, each Sprite let's say has instance variable "Nearest" -> Boolean.

    What i need is, this variable to change to yes or no, each time a sprite is near to another. I hope make sense.

    Would be great also if can control how near or not is each other, so the variable to enable/disable.

    I tried to use family, but i think missing something.

    Any help? Thanks!

  • Put all the sprites in a family called canBeClose and test all members of the family for nearest member of the family canBeClose (excluding the family member of canBeClose doing the testing of course). If distance is < how_close_is_near then set the boolean to true in the canBeClose member doing the testing.

    do this every tic or every .03 seconds.

    yours

    winkr7

  • Do you have time to share a test project? Thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi, any help, anyone? Thanks again!

  • So you have ten instances of one type and the type has a Boolean you want to set when there’s another instance nearby? I think using the variable name “nearest” is throwing me off, “near” could make more sense if I’m understanding it right.

    A common way to reference other instances of an object type is to add a family and add that object type to it. Here I named the family “other” and the events could look like this:

    Every tick
    — sprite: set near to false
    
    For each Sprite
    For each other
    Compare: Sprite.uid <> other.uid
    Compare: distance(sprite.x, sprite.y, other.x, other.y)<50
    — sprite: set near to true

    An alternate way is to use sprite(iid).x to reference other instances. No family needed.

    Every tick
    — sprite: set near to false
    
    For each Sprite
    Repeat sprite.count times
    Compare: Sprite.uid <> sprite(loopindex).uid
    Compare: distance(sprite.x, sprite.y, sprite(loopindex).x, sprite(loopindex).y)<50
    — sprite: set near to true
  • Thanks a lot bro, it works!!! :)

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