How do I object attack one of the overlapping objects?

Get help using Construct 2

Post » Sun Apr 30, 2017 11:55 pm

I'm making a project where you have your castle and need to defend him by sending troops that will fight the enemy troops and attack the enemy castle.
When the troops begins to fight each other, they hurt all enemies that are being overlapped by them. A soldier dies with 4 hits of another soldier, this way, a single soldier can kill 3 overlapping enemy soldiers with 4 hits, a single arrow hurts 10 overlapping enemy soldiers, etc.
I would like to fix this and make a soldier fights and hurts only one of the overlapping enemies in its front, make an arrow hits only one of the overlapping targets.
How can I do it?
B
21
S
8
G
1
Posts: 29
Reputation: 2,000

Post » Mon May 01, 2017 12:45 am

You can do this in a few ways... I'm guessing that your enemy units are all individual sprites, and not one sprite that represents a whole group of enemies?

If they're all separate, you can set the target to the projectile when the projectile is being created (on shoot). You could make some instance variables for the arrow, or the soldier, and call them something like TargetX and TargetY. Then you can pick the enemy that's closest to your defending unit, and set the instance variables TargetX and TargetY to be the coordinates of the enemy.

You can then use the coordinates (pick by comparison) to tell your soldier or arrow which unit to apply damage (and also to trigger to destroy your arrow sprite). Simply compare the TargetX and TargetY values to the coordinate position of the enemy. I would recommend setting a comparison RANGE of say, 3-5 pixels or more (depending on the size of your graphics).

Hopefully this hint helps. If you still have problems just let me know here and I will see if I can make a quick example.

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
83
S
34
G
40
Posts: 3,032
Reputation: 30,474

Post » Mon May 01, 2017 12:52 am

something like this should work:

for each troop
troop is overlapping enemy

add subevent > enemy > pick nearest/furthest >
x: troop.x
y: troop.y
B
25
S
16
G
26
Posts: 125
Reputation: 16,103

Post » Mon May 01, 2017 2:02 am

SoldjahBoy wrote:You can do this in a few ways... I'm guessing that your enemy units are all individual sprites, and not one sprite that represents a whole group of enemies?

If they're all separate, you can set the target to the projectile when the projectile is being created (on shoot). You could make some instance variables for the arrow, or the soldier, and call them something like TargetX and TargetY. Then you can pick the enemy that's closest to your defending unit, and set the instance variables TargetX and TargetY to be the coordinates of the enemy.

You can then use the coordinates (pick by comparison) to tell your soldier or arrow which unit to apply damage (and also to trigger to destroy your arrow sprite). Simply compare the TargetX and TargetY values to the coordinate position of the enemy. I would recommend setting a comparison RANGE of say, 3-5 pixels or more (depending on the size of your graphics).

Hopefully this hint helps. If you still have problems just let me know here and I will see if I can make a quick example.

~Sol


Hi, thanks for the atention. :D
The sprites aren't individual sprites, they are all clones made by the system to defend the "CPU's" castle.
My event sheet of the soldier being attacked by another soldier is:
EnemySoldier is overlapping Soldier
Is "Attack" animation playing.
Animation frame="4"> Soldier subtract 25*EnemySoldier.PickedCount from life.

The event for the arrow is:
Soldier On collision with Arrow > Subtract 15 from vida.

And then i'd like to maintain this codes and add the necessary to make sprites choose just one enemy at time to fight, and the arrows hurt just one enemy even when they are overlapped.

A great exemple of what I'm talking is the game EpicWar 1 and 2. Even when the troops are all overlapped, they aim at only one sprite at time.
B
21
S
8
G
1
Posts: 29
Reputation: 2,000

Post » Mon May 01, 2017 2:12 am

You mention that your enemies are "clones" of each other, which is fine. Is each "clone" though, just a SINGLE enemy as the sprite or do you have like 2 or 3 enemies per sprite that act as one object?

If each enemy is it's own individual "clone" then you simply need to use object picking nearest/furthest as mentioned by celstrider. He basically explained in a shorter way what I was talking about in my post before. Just pick the enemy that's closest to your defender, and apply the damage to that enemy only (because you picked it specifically based on it's distance to the defending player).

I think the reason that it's damaging ALL units right now, if because it hasn't been told to pick ONE unit specifically.

----------------------------------
If overlapping ENEMY
-> deal damage to enemy

This will cause ALL enemies to be damaged when only ONE enemy is being overlapped.

----------------------------------
If overlapping enemy & distance to enemy is less than 10
-> deal damage to enemy

This will cause only enemies that are within 10 pixels of you to take damage.

----------------------------------
If overlapping enemy
-> pick enemy closest to player
---> deal damage to enemy

This will cause only the enemy that's closest to the player/defender/arrow to take damage.

----------------------------------
The key is to make sure the game knows WHICH enemy you want it to pick, otherwise it will simply pick them ALL whenever there is overlapping.

Hopefully this helps a bit more :)

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
83
S
34
G
40
Posts: 3,032
Reputation: 30,474

Post » Mon May 01, 2017 3:27 am

SoldjahBoy wrote:You mention that your enemies are "clones" of each other, which is fine. Is each "clone" though, just a SINGLE enemy as the sprite or do you have like 2 or 3 enemies per sprite that act as one object?

If each enemy is it's own individual "clone" then you simply need to use object picking nearest/furthest as mentioned by celstrider. He basically explained in a shorter way what I was talking about in my post before. Just pick the enemy that's closest to your defender, and apply the damage to that enemy only (because you picked it specifically based on it's distance to the defending player).

I think the reason that it's damaging ALL units right now, if because it hasn't been told to pick ONE unit specifically.

----------------------------------
If overlapping ENEMY
-> deal damage to enemy

This will cause ALL enemies to be damaged when only ONE enemy is being overlapped.

----------------------------------
If overlapping enemy & distance to enemy is less than 10
-> deal damage to enemy

This will cause only enemies that are within 10 pixels of you to take damage.

----------------------------------
If overlapping enemy
-> pick enemy closest to player
---> deal damage to enemy

This will cause only the enemy that's closest to the player/defender/arrow to take damage.

----------------------------------
The key is to make sure the game knows WHICH enemy you want it to pick, otherwise it will simply pick them ALL whenever there is overlapping.

Hopefully this helps a bit more :)

~Sol


IT WORKED!
Thank you so much! You're awesome!
B
21
S
8
G
1
Posts: 29
Reputation: 2,000


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 30 guests