How do I know which tile is being hit on collision

0 favourites
From the Asset Store
Each tile 64 by 64 pixels - Ultimate Tile Set for your awesome games
  • [quote:1rt5t5gc]Strange, but it did not worked for me. Nothing happens except that Bomb object is erased.

    Does it work if you remove the "Wait 3 seconds" ?

  • [quote:br3t1pi0]Strange, but it did not worked for me. Nothing happens except that Bomb object is erased.

    Does it work if you remove the "Wait 3 seconds" ?

    nimos100

    Ah, I missed "1" in the code (functionBomb.Param(1)-1) . Now , it works great. Wow. Cool.

    By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?

    I just want to replace the location of objects by position

    as it seems to be not too rational.

  • By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?

    Since the bullet impact is always at (0,0) in tile value or what to call it, meaning this will always be by default the original tile coordinate of (TileX, TileY) regardless of where on the tilemap the bullet hit. Knowing this and looking at the image below you can see which value to add to this coordinate to get one of the surrounding tiles.

    The reason you can do it this easy, is because you convert the bullets coordinate X,Y to a tile coordinate and therefore its easier to work with.

    So in your case to get the different tiles you just add the correct amount to the TileX, TileY after the conversion:

    Top:

    Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y) - 1

    Left:

    Tilemap. PositiontoToTileX(bullet.X) - 1, Tilemap.PositionToTileY(bullet.Y)

    Right:

    Tilemap. PositiontoToTileX(bullet.X) + 1, Tilemap.PositionToTileY(bullet.Y)

    Down:

    Tilemap. PositiontoToTileX(bullet.X), Tilemap.PositionToTileY(bullet.Y) + 1

  • > By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?

    >

    Since the bullet impact is always at (0,0) in tile value or what to call it, meaning this will always be by default the original tile coordinate of (TileX, TileY) regardless of where on the tilemap the bullet hit. Knowing this and looking at the image below you can see which value to add to this coordinate to get one of the surrounding tiles.

    The reason you can do it this easy, is because you convert the bullets coordinate X,Y to a tile coordinate and therefore its easier to work with.

    So in your case to get the different tiles you just add the correct amount to the TileX, TileY after the conversion:

    Top:

    Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y) - 1

    Left:

    Tilemap. PositiontoToTileX(bullet.X) - 1, Tilemap.PositionToTileY(bullet.Y)

    Right:

    Tilemap. PositiontoToTileX(bullet.X) + 1, Tilemap.PositionToTileY(bullet.Y)

    Down:

    Tilemap. PositiontoToTileX(bullet.X), Tilemap.PositionToTileY(bullet.Y) + 1

    nimos100,

    can you show a part of the code using these coordinates? I used them previously but none did not work for me. An object just does not appear. Maybe I am missing something.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • nimos100,

    can you show a part of the code using these coordinates? I used them previously but none did not work for me. An object just does not appear. Maybe I am missing something.

    The reason it doesn't work is because you are using a tile coordinate in your creation but it requires a value in (x , y). so to solve that you have to first convert the bullet (x , y) value to a tile value (TileX, TileY) and then you use that value to convert to a (x, y) value so you can place it on the tilemap.

  • nimos100, thanks a lot!

  • nimos100,

    I am wondering what's wrong with the sequence of the following code:

    When a turretBody appeares , I want to trigger the following events:

    1) If turretBody is destroyed with the bullet2Player, tilemapBoxEnemy is replaced with Tilemap1

    2) If turretBody is not destroyed, bullet overlaps TilemapBoxEnemy and bullet is destroyed

  • nimos100,

    I am wondering what's wrong with the sequence of the following code:

    When a turretBody appeares , I want to trigger the following events:

    1) If turretBody is destroyed with the bullet2Player, tilemapBoxEnemy is replaced with Tilemap1

    2) If turretBody is not destroyed, bullet overlaps TilemapBoxEnemy and bullet is destroyed

    I think the problem is in your structure of the code, meaning you are trying to do to many things in one event that would be better splitting up into several events. So there are some things that to me looks, not to good.

    Using "Create" together with other "Actions" in C2 can be a bit tricky, because you have to be careful about top levels and sub levels and know how it works to avoid problems with the "Create" action. But think everyone runs into this problem at some point.

    Your newly created object is not available before you get to a new top level. but will be available in sub levels from where it is created.

    So this will cause some problems as I see it, because you create the turretbody in a sub level, but you haven't reached a top level when using the "On collision with Bullet2Player" so the turretBody doesn't exist at this point. Another thing is the "On collision" which is a "trigger" or what to call it, indicated by the little green arrow, and it triggers whenever an collision happen that meets its condition in this case a collision with bullet2Player, so it works the same way as a mouse click on an object does. In my experience any such trigger should be placed on top level and not within a sub event. So in your case I would move the "On collision" to its own event and restructure your code so it works with it being there, and then it will most likely sort it self out.

  • > nimos100,

    >

    > I am wondering what's wrong with the sequence of the following code:

    >

    > When a turretBody appeares , I want to trigger the following events:

    > 1) If turretBody is destroyed with the bullet2Player, tilemapBoxEnemy is replaced with Tilemap1

    > 2) If turretBody is not destroyed, bullet overlaps TilemapBoxEnemy and bullet is destroyed

    >

    I think the problem is in your structure of the code, meaning you are trying to do to many things in one event that would be better splitting up into several events. So there are some things that to me looks, not to good.

    Using "Create" together with other "Actions" in C2 can be a bit tricky, because you have to be careful about top levels and sub levels and know how it works to avoid problems with the "Create" action. But think everyone runs into this problem at some point.

    Your newly created object is not available before you get to a new top level. but will be available in sub levels from where it is created.

    So this will cause some problems as I see it, because you create the turretbody in a sub level, but you haven't reached a top level when using the "On collision with Bullet2Player" so the turretBody doesn't exist at this point. Another thing is the "On collision" which is a "trigger" or what to call it, indicated by the little green arrow, and it triggers whenever an collision happen that meets its condition in this case a collision with bullet2Player, so it works the same way as a mouse click on an object does. In my experience any such trigger should be placed on top level and not within a sub event. So in your case I would move the "On collision" to its own event and restructure your code so it works with it being there, and then it will most likely sort it self out.

    Yes, I previously did all three parts separately.

    The problem is that I am losing X, Y coordinates in 39 and 40 and I cannot find something to rely upon. A collision of turretBody and a bullet in 40 does not help to define old tilemap coordinates since turretBody is un objet (not tilemap). In 39, I should define again coordinates of the TilemapBoxEnemy after the objet TurretBody is created in the event #37.

    Moreover, in 39 I got a problem with priority of events. I need to make it trigger only if TurretBody (Important: TurretBody from event #37 only) is not destroyed. If TurretBody (from event #37) is destroyed, 39 event is deactivated for TilemapBoxEnemy (in X,Y coordinates of event #37).

  • Yes, I previously did all three parts separately.

    The problem is that I am losing X, Y coordinates in 39 and 40 and I cannot find something to rely upon. A collision of turretBody and a bullet in 40 does not help to define old tilemap coordinates since turretBody is un objet (not tilemap). In 39, I should define again coordinates of the TilemapBoxEnemy after the objet TurretBody is created in the event #37.

    Moreover, in 39 I got a problem with priority of events. I need to make it trigger only if TurretBody (Important: TurretBody from event #37 only) is not destroyed. If TurretBody (from event #37) is destroyed, 39 event is deactivated for TilemapBoxEnemy (in X,Y coordinates of event #37).

    Im not sure what you mean, I don't see any turretBody in #37 that checks whether its destroyed or not, you mean turretUp? But you create a turretBody in #38.

    Its not easy for me to even understand what the code is suppose to do, so this might be wrong. But it seems to me that you rely on or try to use data that you can't really be sure is correct or not. Meaning that you set GetTileX and GetTileY in 37. So if you need to use these coordinates in another event, you shouldn't rely on the data from this one, unless you are 100% sure that they are correct. So if turretBody is created in #38 based on the condition in #37 that "Bullet is overlapping TilemapTurretUp" and then in #40 "TurretBody on collision with bullet2Player" is using the GetTileX and GetTileY in #37 how can you be sure that these are correct? or are there only 1 bullet, turretBody and TilemapTurretUp on the screen at a given time?

    If you need something like GetTileX and GetTileY in an event like #40 you should store them in the object it self. In your case turretBody when it is created and only use this when you need to get the tile X and tile y position for that object. Otherwise I think you will end up loosing track of what GetTileX and GetTileY is when you need them.

    But as I said im not even sure what exactly you are trying to do. Maybe show some screenshots of what is suppose to happen. But at least to me it seems like you are mixing a lot of objects together without really knowing whether its the correct objects or not or the correct data you are using. How is C2 going to know which turretBody you are referring to in #40 with the GetTileX and GetTileY position you have stored in #37 that rely on a bullet is overlapping a TilemapTurretUp?. If there are only 1 bullet in your game its probably ok, but if there are several bullets GetTileX and GetTileY will constantly be overwritten causing you to not know what is stored in them when you need them.

    Also in #40 you are destroying a Turret object that you haven't picked in the event, if that event is ever true all Turret objects in the game will be destroyed.

    Whenever you work with an object in an event, unless there are only 1 of them. You always have to pick them or things wont work.

    The way I make my events if they are confusing, is to write down each step that I want to happen in them and make each part, that way its very easy to maintain an overview, for instant.

    ///

    A turret consist of 2 parts and if both parts are destroyed the whole turret should be removed. If only 1 part is damage, that part should be replaced with a damaged part until repaired or the other part is destroyed as well.

    1. Bullet hit Bottom part of turret

    Deal damage to turret

    2. Check if Bottom part is destroyed

    3. Check if Top part is destroyed

    3a. If top part is also destroyed, destroy whole turret.

    3b. top part is not destroyed, replace bottom part with a damaged part.

    So based on this example I know there are 3 objects involved (Bullet, Top part of turret and Bottom part of turret) so i have to pick all 3 parts to complete the event. And normally I do all picking as early as possible, because then you know you have the stuff you need to complete the event.

  • >

    >

    > Yes, I previously did all three parts separately.

    > The problem is that I am losing X, Y coordinates in 39 and 40 and I cannot find something to rely upon. A collision of turretBody and a bullet in 40 does not help to define old tilemap coordinates since turretBody is un objet (not tilemap). In 39, I should define again coordinates of the TilemapBoxEnemy after the objet TurretBody is created in the event #37.

    > Moreover, in 39 I got a problem with priority of events. I need to make it trigger only if TurretBody (Important: TurretBody from event #37 only) is not destroyed. If TurretBody (from event #37) is destroyed, 39 event is deactivated for TilemapBoxEnemy (in X,Y coordinates of event #37).

    >

    Im not sure what you mean, I don't see any turretBody in #37 that checks whether its destroyed or not, you mean turretUp? But you create a turretBody in #38.

    Its not easy for me to even understand what the code is suppose to do, so this might be wrong. But it seems to me that you rely on or try to use data that you can't really be sure is correct or not. Meaning that you set GetTileX and GetTileY in 37. So if you need to use these coordinates in another event, you shouldn't rely on the data from this one, unless you are 100% sure that they are correct. So if turretBody is created in #38 based on the condition in #37 that "Bullet is overlapping TilemapTurretUp" and then in #40 "TurretBody on collision with bullet2Player" is using the GetTileX and GetTileY in #37 how can you be sure that these are correct? or are there only 1 bullet, turretBody and TilemapTurretUp on the screen at a given time?

    If you need something like GetTileX and GetTileY in an event like #40 you should store them in the object it self. In your case turretBody when it is created and only use this when you need to get the tile X and tile y position for that object. Otherwise I think you will end up loosing track of what GetTileX and GetTileY is when you need them.

    But as I said im not even sure what exactly you are trying to do. Maybe show some screenshots of what is suppose to happen. But at least to me it seems like you are mixing a lot of objects together without really knowing whether its the correct objects or not or the correct data you are using. How is C2 going to know which turretBody you are referring to in #40 with the GetTileX and GetTileY position you have stored in #37 that rely on a bullet is overlapping a TilemapTurretUp?. If there are only 1 bullet in your game its probably ok, but if there are several bullets GetTileX and GetTileY will constantly be overwritten causing you to not know what is stored in them when you need them.

    Also in #40 you are destroying a Turret object that you haven't picked in the event, if that event is ever true all Turret objects in the game will be destroyed.

    Whenever you work with an object in an event, unless there are only 1 of them. You always have to pick them or things wont work.

    The way I make my events if they are confusing, is to write down each step that I want to happen in them and make each part, that way its very easy to maintain an overview, for instant.

    ///

    A turret consist of 2 parts and if both parts are destroyed the whole turret should be removed. If only 1 part is damage, that part should be replaced with a damaged part until repaired or the other part is destroyed as well.

    1. Bullet hit Bottom part of turret

    Deal damage to turret

    2. Check if Bottom part is destroyed

    3. Check if Top part is destroyed

    3a. If top part is also destroyed, destroy whole turret.

    3b. top part is not destroyed, replace bottom part with a damaged part.

    So based on this example I know there are 3 objects involved (Bullet, Top part of turret and Bottom part of turret) so i have to pick all 3 parts to complete the event. And normally I do all picking as early as possible, because then you know you have the stuff you need to complete the event.

    I rethought the concept of events.

    Here is the starting point

    Or it can be like that

    So when a bullet2Player overlaps TilemapBoxEnemy,

    we identify the coordinates 0,0 and then this event should trigger 2 different actions depending on the condition.

    Condition 1: At least one TurretBody is present in any of these coordinates (see in circles)

    IF Condition 1 is satisfied THEN bullet2Player is destroyed

    Condition 2: No TurretBody is present in any of these coordinates (see in circles)

    IF Condition 2 is satisfied THEN bullet2Player is destroyed and TilemapBoxEnemy is replaced with Tilemap1 (set tile X, Y)

    So, is it possible to identifiy whether TurretBody is present or not around the TilemapBoxEnemy and how do we arrange these conditions?

  • So, is it possible to identifiy whether TurretBody is present or not around the TilemapBoxEnemy and how do we arrange these conditions?

    If you store the TileX,Y position of TilemapBoxEnemy you can use that to check whether a Turretbody is placed in any of the 4 areas surrounding it if you also store the TileX,Y position for the TurretBody. Then you can just add the position difference to TilemapBoxEnemy TileX,Y position when you need to check it. So:

    Bullet is overlapping TilemapBoxEnemy

    //Check if there is a turretBody above TilemapBoxEnemy

    TurretBody.TileX = TilemapBodyEnemy.TileX

    TurretBody.TileY = TilemapBodyEnemy.TileY - 1

    ELSE

    (There are no TurretBody at that position)

    //Check if there is a turretBody below TilemapBoxEnemy

    TurretBody.TileX = TilemapBodyEnemy.TileX

    TurretBody.TileY = TilemapBodyEnemy.TileY + 1

    ELSE

    (There are no TurretBody at that position)

    ...

    ..

    .

    (And the same with left and right side, just adding +/- 1 to X instead.)

    If any TurretBody have these TileX,Y coordinate then you know there is a TurretBody.

    So when you create TilemapBoxEnemy and TurretBody you store there TileX and TileY position in two variables in the object itself and then you can just use these for checking.

  • >

    > So, is it possible to identifiy whether TurretBody is present or not around the TilemapBoxEnemy and how do we arrange these conditions?

    >

    If you store the TileX,Y position of TilemapBoxEnemy you can use that to check whether a Turretbody is placed in any of the 4 areas surrounding it if you also store the TileX,Y position for the TurretBody. Then you can just add the position difference to TilemapBoxEnemy TileX,Y position when you need to check it. So:

    Bullet is overlapping TilemapBoxEnemy

    //Check if there is a turretBody above TilemapBoxEnemy

    TurretBody.TileX = TilemapBodyEnemy.TileX

    TurretBody.TileY = TilemapBodyEnemy.TileY - 1

    ELSE

    (There are no TurretBody at that position)

    //Check if there is a turretBody below TilemapBoxEnemy

    TurretBody.TileX = TilemapBodyEnemy.TileX

    TurretBody.TileY = TilemapBodyEnemy.TileY + 1

    ELSE

    (There are no TurretBody at that position)

    ...

    ..

    .

    (And the same with left and right side, just adding +/- 1 to X instead.)

    If any TurretBody have these TileX,Y coordinate then you know there is a TurretBody.

    So when you create TilemapBoxEnemy and TurretBody you store there TileX and TileY position in two variables in the object itself and then you can just use these for checking.

    Ahh, I am lost.. Will take a break for a bit to rethink again what you are advising.

    So far , I am stuck on 2 things:

    1) Unfortunately, I am not storing X,Y coordinates of TurretBody. At least, I cannot use them since they are in a separate event where another bullet overlaps TilemapBoxEnemy ( i described it earlier)

    .

    It would have been cool to continue this event , since i have all the necessary coordinates of TilemapBoxEnemy and of Turret Body. But I cannot do it cause 2 different bullets cannot overlap an object in the same event. So I started a new event where a new bullet (bullet2Player) overlaps a TilemapBoxEnemy ( A turretBody has been already created so I cannot create it again), and I can store only coordinates of TilemapBoxEnemy. But I understand that without storing of X, Y coordinates of turretBody in this new event , nothing can be done.

    2) For checking if there is a turretBody above TilemapBoxEnemy .etc. should I use Compare two variables?

    . If so, the system does not understand this expression.

    For "Else", should I use "System - Else" ?

  • >

    > >

    > > So, is it possible to identifiy whether TurretBody is present or not around the TilemapBoxEnemy and how do we arrange these conditions?

    > >

    > If you store the TileX,Y position of TilemapBoxEnemy you can use that to check whether a Turretbody is placed in any of the 4 areas surrounding it if you also store the TileX,Y position for the TurretBody. Then you can just add the position difference to TilemapBoxEnemy TileX,Y position when you need to check it. So:

    >

    > Bullet is overlapping TilemapBoxEnemy

    >

    > //Check if there is a turretBody above TilemapBoxEnemy

    > TurretBody.TileX = TilemapBodyEnemy.TileX

    > TurretBody.TileY = TilemapBodyEnemy.TileY - 1

    > ELSE

    > (There are no TurretBody at that position)

    >

    > //Check if there is a turretBody below TilemapBoxEnemy

    > TurretBody.TileX = TilemapBodyEnemy.TileX

    > TurretBody.TileY = TilemapBodyEnemy.TileY + 1

    > ELSE

    > (There are no TurretBody at that position)

    > ...

    > ..

    > .

    > (And the same with left and right side, just adding +/- 1 to X instead.)

    >

    > If any TurretBody have these TileX,Y coordinate then you know there is a TurretBody.

    >

    > So when you create TilemapBoxEnemy and TurretBody you store there TileX and TileY position in two variables in the object itself and then you can just use these for checking.

    >

    Ahh, I am lost.. Will take a break for a bit to rethink again what you are advising.

    So far , I am stuck on 2 things:

    1) Unfortunately, I am not storing X,Y coordinates of TurretBody. At least, I cannot use them since they are in a separate event where another bullet overlaps TilemapBoxEnemy ( i described it earlier)

    .

    It would have been cool to continue this event , since i have all the necessary coordinates of TilemapBoxEnemy and of Turret Body. But I cannot do it cause 2 different bullets cannot overlap an object in the same event. So I started a new event where a new bullet (bullet2Player) overlaps a TilemapBoxEnemy ( A turretBody has been already created so I cannot create it again), and I can store only coordinates of TilemapBoxEnemy. But I understand that without storing of X, Y coordinates of turretBody in this new event , nothing can be done.

    Maybe I should come back to where I started and try to unite again the script that did not work with the main event where a turretBody is created:

    > nimos100,

    >

    > I am wondering what's wrong with the sequence of the following code:

    >

    >

    >

    > When a turretBody appeares , I want to trigger the following events:

    > 1) If turretBody is destroyed with the bullet2Player, tilemapBoxEnemy is replaced with Tilemap1

    > 2) If turretBody is not destroyed, bullet overlaps TilemapBoxEnemy and bullet is destroyed

    >

    2) For checking if there is a turretBody above TilemapBoxEnemy .etc. should I use Compare two variables?

    .

    If so, the system does not understand this expression.

    For "Else", should I use "System - Else" ?

  • Ahh, I am lost.. Will take a break for a bit to rethink again what you are advising.

    So far , I am stuck on 2 things:

    1) Unfortunately, I am not storing X,Y coordinates of TurretBody. At least, I cannot use them since they are in a separate event where another bullet overlaps TilemapBoxEnemy ( i described it earlier)

    It would have been cool to continue this event , since i have all the necessary coordinates of TilemapBoxEnemy and of Turret Body. But I cannot do it cause 2 different bullets cannot overlap an object in the same event. So I started a new event where a new bullet (bullet2Player) overlaps a TilemapBoxEnemy ( A turretBody has been already created so I cannot create it again), and I can store only coordinates of TilemapBoxEnemy. But I understand that without storing of X, Y coordinates of turretBody in this new event , nothing can be done.

    2) For checking if there is a turretBody above TilemapBoxEnemy .etc. should I use Compare two variables?

    If so, the system does not understand this expression.

    For "Else", should I use "System - Else" ?

    1. If you store the TileX,Y coordinates in the objects as variables you can use them wherever you want, its no different than storing something else.

    If I understand you correct regarding two bullets not being able to overlap a TilemapBoxEnemy in the same event, its because you need to add a for each bullet, otherwise C2 wont know that you are talking about individual objects. There should be no issues doing this. But of course you have to make your event able to handle it when designing it.

    2. Yeah if you have stored the TileX, Y its just comparing two variables with each other.

    Here is a simplified version of what I believe you are trying to do:

    The red dot is a bullet, TB is the TurretBody and the cactus is the TilemapBoxEnemy.

    TB are sprite objects just placed on the tilemap. The cactus is on the tilemap it self and the bullet is also just a sprite.

    The text keep track of the bullet Tile X, Y coordinates which I convert from its X,Y coordinates.

    The list is just a log which will give feedback when the bullet hit the Cactus.

    In this example the bullet is just linked to the mouse, but could be a sprite with bullet behaviour if that is what you use. As it moves around the tilemap its X,Y coordinates are converted to TileX, TileY and shown in the text object. So TileX and TileY are just 2 variables that I have added to the bullet sprite.

    The TurretBodies are created at the start of layout and in this case since I know where the cactus is, I just add the predefined numbers to there variables. But would be no different if these where converted based on where the cactus was placed on the tilemap. I just did it this way to keep it simple. But also the TB object have 2 variables added just like the bullet.

    The last part is what checks for collision with the cactus and whether there are any TBs around it. So first I make sure that the bullet is actually overlapping the cactus. If that is the case I check each of the tiles around the bullet since I know at since point that the bullet have hit the cactus. So using the TB TileX and TileY and the bullets TileX and TileY and adding the +-1 to either X or Y I can check if these match any TBs. And then just output the result to the log.

    But if there were more bullets I would just add a "For each Bullet" to the start of the event.

    CapX:

    https://dl.dropboxusercontent.com/u/109921357/Collision%20test%203/Collision_test.capx

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