Destroy an object when surrounded

0 favourites
  • 11 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • I am trying to destroy an object when it is surrounded on four sides. I tried an event with four conditions, when object is overlapping at offset -- for four different directions.

    I am probably not understanding something about how this event should work.

    Event I have is:

    sprite is overlapping at offset(32, 0)
    sprite is overlapping at offset(0, 32)
    sprite is overlapping at offset(-32, 0)
    sprite is overlapping at offset(0, -32)     
          action:  sprite destroy
    [/code:4859lp0h]
       
    
    capx is here:  [url]https://dl.dropboxusercontent.com/u/66037205/DestroyWhenSurrounded.capx[/url]
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Select all 4 "is overlapping", click rmb, and select "or". And I think that should work.

  • Thanks for your quick reply!

    What I am needing is for "Surrounded" to be true if there is an object on all four sides (and result in the object being destroyed). By using or, I think that would make the condition true if there is an object on any of the four sides.

    It seems like my logic should work, but it does not. It may be due to the way overlap at offset is tested?

    It could be that there is a completely different way to test for this condition that I am missing.

    Basically I need to answer the question "Am I blocked in all four directions... surrounded on all four sides by something".

  • If you want same object, then you must use families. Create two: 1st, 2nd ( just for tests ), and do events like this : 1st overlapping at offset 2nd, destroy.

    Thing is... looking at the capx, this way you will destroy all, probably.

    Eventing in construct s really context dependent. Again, looking at capx it's hard to know what you're after. If in your game objects that are surrounding main objects are not the same, then what you're doing right now is, sorry to say, pointless. But if they are the same object, then you mist go deeper. So you must do conditions for two situations: one is that object s surrounding same object, but only at one offset, then use or ( and families ), but you must find a way to specify that if objet os surrounded from 4 sides, it is not being destroyed. Boo ( boolean ) var ( variable) might help in achieving this.

  • Each condition filters the objects so I think if you add a "pick all" between each condition it should work. The object that "pick all" should use is the one the Sprite is overlapping with.

  • With your help, I found one way to solve this using a family. Here is the solution that I found:

    For the purpose of this test, it is done once start of layout so event looks like this:

    (please excuse poor representation of actual events... I need to learn how to represent this better in text)

    at start of layout
    for each Sprite     -> Sprite set scale 1.1
         Sprite is overlapping Surrounders (this is a family with memeber Sprite in it)
         Surrounders.PickedCount = 4                                                                                      -> Sprite.Destroy
    [/code:2yk7dpqv]
    
    capx here: [url]https://dl.dropboxusercontent.com/u/66037205/DestroyWhenSurrounded_B.capx[/url]
  • how about instead of overlapping with offset you start using dist (sprite1.x, sprite2.x, sprite1.y, sprite2.y) <32 -> destroy sprite1 ?

    problem with overlapping - imagine my object is a big ">" form like this one (sprite 1). And now imagine your second sprite is a dot "."

    if you check for overlapping with -32 x and y - they will never overlap and therefore your event will never fire, even though the dot is under 32 distance from <.

    Also you have to take in that origin point of your sprite is the point where you calculate your stuff from. not borders. sprite x,y = origin point. So using distance from sprite 1 to sprite 2 you get this:

    if ( dist (.x,.y,<.x,<.y) < 32)

    destroy <

    (it won't matter if left, right top or down, each will work. )

    math pls.

  • how about instead of overlapping with offset you start using dist (sprite1.x, sprite2.x, sprite1.y, sprite2.y) <32 -> destroy sprite1 ?

    problem with overlapping - imagine my object is a big ">" form like this one (sprite 1). And now imagine your second sprite is a dot "."

    if you check for overlapping with -32 x and y - they will never overlap and therefore your event will never fire, even though the dot is under 32 distance from <.

    Also you have to take in that origin point of your sprite is the point where you calculate your stuff from. not borders. sprite x,y = origin point. So using distance from sprite 1 to sprite 2 you get this:

    if ( dist (.x,.y,<.x,<.y) < 32)

    destroy <

    (it won't matter if left, right top or down, each will work. )

    math pls.

    In certain circumstances distance is of no good use, for example n isometric view. But otherwise it is very useful.

  • since he asked left, right, up and down i'm pretty sure isometry isn't the case here

  • you can always have 4 hidden objects in, following the object u want to be surrounded.

    when all 4 are overlapped destroy the main one? u can also try it with the chess board plugin from r0j0hound i think ... considering at all time the slots around the main object ... then there is another way... similar to the first suggestion ... the same way u do match games... but that is a bit array based and slightly difficult since involves some more then usual math .... those algebra hours wold come in handy now... to bad i always did something else at the class...

    another way the simple c2 way is to check for position but not for overlapping... but for the X + - y + - something and in center being the position of the main object... which i think its easier to do then the rest of the above i used something similar to create a cue stick angle position and guide line for a pool example il try find it if u dont get some more help till then i guess

    ie: 1 event with 5 conditions inside pos x= mainobjx+mainobjwidth and so on ...considering the squares are equal to the objects width and height

  • Thanks to all! I have what I need for now.

    To fill your curiosity (or feed it more), here is a little more information about the game I am making:

    It is grid-based

    All objects in the game are the same size and are aligned to the grid (except during a move-to transition)

    It is not a match three game

    enemies change state when surrounded on four sides (maybe 8 later -- including diags) by either other enemies or solid objects (hence this "surrounded" test)

    The test capx files that I used here destroy the test object just so that I can see the test is working. In game the enemies simply change state when surrounded.

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