stop enemy health from dropping to zero

Get help using Construct 2

Post » Tue Jan 15, 2013 8:08 pm

Thought I had this solved by adding in hit sensors, been stumped for the past few days.

Problem: When two player units attack an enemy unit the enemy unit's health drops by 13-17 points when it's only supposed to drop by 2 (one for each player hit).

If their animations are staggered the health subtraction works perfectly.

Here is my capx:
https://www.dropbox.com/s/dzzdlsloa6ibqna/basedef.capx

I have it set to spawn two player units with one click of the buy button to simulate when they get stacked up after defeating an enemy unit together, hopefully someone can help!

(Mouse over the enemy unit to see it's health)wretchedshark2013-01-15 20:36:35
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Tue Jan 15, 2013 8:14 pm

I imagine you are subtracting the health in an event that is occurring "every tick" instead of just once.

Try adding a "trigger once while true" as a condition to the event that subtracts the health.

Sorry i can't look at the capx right this moment.
B
50
S
21
G
8
Posts: 844
Reputation: 10,299

Post » Tue Jan 15, 2013 8:45 pm

I do, that is what baffles me. Why the stacked animation causes the problem. 1 vs 1 works, 2 vs 1 when the attack animations are on different frames also works, but if they end up attacking on the same frame then the health of the enemy drops by a large portion.

Here is the code for health subtraction if that helps without looking at the capx.

wretchedshark2013-01-15 20:46:56
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Tue Jan 15, 2013 9:45 pm

I'm fiddling about with this at the moment - quick question: If you spawn two playerUnits and one enemyUnit, should the one enemyUnit stop both playerUnits or just one of them?
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Tue Jan 15, 2013 9:54 pm

It should stop both of them, it's a prototype for setting up a side-view base defense game, so to get to the other base I wanted both sides to go through (kill) one another before proceeding.

Also I forgot to mention that the way the current health subtraction is set up is supposed to pick the nearest enemy to the hitbox and subtract that enemies health, so if it's 2 vs. 1 - the single unit won't be able to fend off both at the same time.
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Wed Jan 16, 2013 12:09 am

I think there were a few issues together which were causing the problem. I refactored some of your capx and it seems to be working now:

Capx Download

However:

1) If I've completely misunderstood what you are trying to do, let me know
2) I've only fixed the player sprites for now. Assuming I understood the post correctly it should be fairly easy to update the enemy AI.

Hopefully, if I got it right, this helps in some way
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Wed Jan 16, 2013 2:20 am

Thank you! I really appreciate this, it's hard to know and learn how to create these conditions properly and without creating a whole bunch of extra work (like overlap instead of comparing range).

I do have two questions if you don't mind:

1) How would I go about re-implementing the feature where one player can only attack one enemy unit at a time? In my capx the only way I could get it to work was by adding the pick closest enemyunit to hitsensor. As you noted this reiterates construct already picking an enemyunit, but if two enemy units overlap the hitsensor they do both take damage. (Event 33 in your capx)

If the player spawn is set to only one unit and you spawn two enemy units the player can hit both of them at the same time.

2) Event 34 (every X seconds -> destroy hitsensor) Is there a better way to destroy the stray hitsensors? Or does your event system for attacking prevent this? With mine sometimes the enemy would die just before the hitsensor spawned leaving an extra hitsensor on the layout. This is just in case in future testing.

Thank you again for your time!
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Wed Jan 16, 2013 4:54 am

1) Ah good point. So I think what's happening is that the pick nearest is only selecting one single enemy (which is good), but then spawning a hit sensor that is overlapping both enemies (which is bad).

Download V2 Capx

What this is now doing is spawning a hit sensor, but we are recording the UID (a unique ID that Construct assigns to every object) to it's instance variable. You can then use the "Pick by unique ID" to ensure that, when a sensor is overlapping the enemy, it only picks the one with the ID that we assigned to it.

2) I noticed that a few times myself. The reason it's happening is because it's saying only if the hit sensor is overlapping the enemy, then apply damage and destroy it. If the enemy is destroyed before this event is run (in the case of two players attacking one enemy), the second hit sensor is never destroyed because the enemy is already dead. To get around this I think you can add a condition at line 29 that when an enemy dies, any hit sensors that are overlapping it are also destroyed.



Combined with the first note above, this screenshot is a little out of date (since we are now only destroying hit sensors assigned to the enemy), but you get the idea.


Hope this helps. You've done an impressive amount of work so far already, it's looking to be a really cool game
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Thu Jan 17, 2013 9:19 pm

Thank you again! I am learning a lot from this, going to add more enemies and players using your event system :)
B
14
S
6
G
2
Posts: 136
Reputation: 3,210


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Colorfish, zenox98 and 27 guests