Tell how many of object A overlap object B

0 favourites
  • 10 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • That's what I'm trying to do. There's many object A's flying around, and a few stationary object B's. I want to tell how many A's are within a certain distance of each B at any time. It can either be within a certain distance or overlapping an invisible radius, either one will do. This is what I already tried:

    <img src="" border="0" />

    That works great... if there's a single object B. Once there's more than one, the counters start flipping out and it doesn't work at all. I can see why it does that, I just don't know how to get around it... Any ideas? <img src="smileys/smiley5.gif" border="0" align="middle" />

  • Can you just encapsulate this entire thing in an outer For Each Object B call? You want to check each B against the entire list of Object A.

    EDIT: Keep in mind that a single object A could be in range of more than one Object B though. So if you want to account for this, you'll need to have an Object A boolean instance variable HasBeenCounted to make sure that each object A can only get "caught" once.

  • Oh, duh, forgot to say I also tried that. That screencap is from a barebones capx I was using to test stuff.

    The problem is that ObjectA's private var, Overlap, can only tell if it's overlapping ANY ObjectB, not a specific one. So if A is overlapping B1, and not B2, A's Overlap is 1, so for B2 it's constantly setting A's Overlap to 0 (and B1 sets it back to 1) and subtracting from its own Overlap.

    I don't know if that explanation made any sense, but that's what appears to be happening.

    EDIT: I see what you mean about the boolean, but I'm not sure exactly how to set it up... I'll post the test capx if anyone wants to mess with it.

  • +System: every tick
      -> ObjectB: set Overlap to 0
    +System: foreach ObjectB
      +System: foreach ObjectA
        Local variable else = 1
        +System: distance(ObjectA.X,ObjectA.Y,ObjectB.X,ObjectB.Y <= 300)
          ->ObjectB: Add 1 to Overlap
          ->System: set else to 0
        +System: else = 1
        +ObjectA is overlaping Radius
          ->ObjectB: Add 1 to Overlap[/code:14n7r9sb]
    There's only one issue
    The radius sprite... how do you associate it to each ObjectB ?
  • I saw your post pre-edit, Yann, and already put it in and it looks like it's working perfectly. What's the problem with the first version? <img src="smileys/smiley5.gif" border="0" align="middle" />

  • In your attempt you don't filter specific instances of ObjectB

    so the distance check will be true as soon as there's an ObjectB close...

    Also System expression doesn't filter the SOL.

    So it will change all the overlap variable of all instances of ObjectB

  • oh yeah, I see what you're getting at. Bleh, that *IS* annoying. Maybe something like this, a second boolean for keeping track if A is already in a "overlapping state" with any B this tick.


    For(B) --> B.Overlap = 0

    For(A) --> A.AlreadyOverlapsThisCycle = false

        if A overlaps B


          A.AlreadyOverlapsThisTick = true

        if A !overlaps B

          if !AlreadyOverlapsThisTick then A.Overlap = false

  • Yann can you explain what you mean by:

    "Also System expression doesn't filter the SOL."?

    Are you referring to the System distance call? Are you saying that it doesn't act on the current SOL list?

  • Well, I dunno exactly what you mean Yann, because like I said... the first version you gave me works perfectly. <img src="smileys/smiley17.gif" border="0" align="middle" /> It doesn't have to be absolutely flawless anyway, since it's for a minor feature in a minor game that I have to finish building for Thursday.

    So uhh, thanks a bunch! <img src="smileys/smiley36.gif" border="0" align="middle" />

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Nevermind, I think Yann is referring to this from the manual:

    "The System object

    In Construct 2 the System object represents built-in functionality. It has no instances. This means most system conditions do not pick any instances: they are either true or false. If they are false the event stops running, otherwise the event continues without the picked instances having been changed. There are exceptions, though: if a system condition uses an object, such as Pick random instance, that will affect the picked objects.

    System actions do not run on any picked objects: they simply run if all of the event's conditions were met."

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