How do I know which tile is being hit on collision

Get help using Construct 2

Post » Sun Jun 28, 2015 11:50 am

@nimos100, thanks a lot!
B
8
S
2
Posts: 48
Reputation: 646

Post » Mon Jul 20, 2015 8:40 pm

@nimos100,

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

Image

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
B
8
S
2
Posts: 48
Reputation: 646

Post » Tue Jul 21, 2015 12:06 pm

heliogame wrote:@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.

Image

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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Tue Jul 21, 2015 1:35 pm

nimos100 wrote:
heliogame wrote:@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.

Image

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.


Image
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).
B
8
S
2
Posts: 48
Reputation: 646

Post » Tue Jul 21, 2015 11:02 pm

heliogame wrote:Image
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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Wed Jul 22, 2015 12:46 pm

nimos100 wrote:
heliogame wrote:Image
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
Image


Or it can be like that
Image

So when a bullet2Player overlaps TilemapBoxEnemy,
Image
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)
Image

IF Condition 1 is satisfied THEN bullet2Player is destroyed Image

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

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

So, is it possible to identifiy whether TurretBody is present or not around the TilemapBoxEnemy and how do we arrange these conditions?
B
8
S
2
Posts: 48
Reputation: 646

Post » Wed Jul 22, 2015 3:24 pm

heliogame wrote: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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Wed Jul 22, 2015 10:32 pm

nimos100 wrote:
heliogame wrote: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) Image.
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?
Image . If so, the system does not understand this expression.
For "Else", should I use "System - Else" ?
B
8
S
2
Posts: 48
Reputation: 646

Post » Wed Jul 22, 2015 10:44 pm

heliogame wrote:
nimos100 wrote:
heliogame wrote: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) Image.
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:
heliogame wrote:@nimos100,

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

Image

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?
Image .

If so, the system does not understand this expression.
For "Else", should I use "System - Else" ?
B
8
S
2
Posts: 48
Reputation: 646

Post » Thu Jul 23, 2015 12:30 am

heliogame wrote: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:

Image
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.

Image
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.

Image
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.

Image
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
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests