0 Favourites

Help with nearest obj

  • Ok so I have a combat system set up so that when I shoot an enemy 10 times, it dies. But if I make many of that enemy, they all die when one gets shot 10 times, I've tried using nearest condition, but this error still occurs. I've even mixed and matched with the condition (switching player and enemy, etc.) but it keeps happening.

  • dwtiger

    Have you tried

    Enemy >> Health Bar >> is Less Or Equal to >>0

    Sub Event>>For Each>> Enemy >>>Destroy

    That it should apply only to the Enemies that have health = 0 or less

    You will find the " For Each" in the System >> For each>> Then choose your enemy

    Note: you do not need the Nearest condition

  • In the for each section, do i put the enemy as the object for the loop? (And thanks for the help.)

  • In the for each section, do i put the enemy as the object for the loop? (And thanks for the help.)

    yes the ones you want to destroy

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Not letting me run it. Currently running most recent version of construct 2.

  • I have the screenshots here (also i have the variable for enemy damage taken as health, planning on changing it soon.)

  • Is the beta version r245

  • I have the screenshots here (also i have the variable for enemy damage taken as health, planning on changing it soon.)

    you can install the beta or you can

    right click on my capx extract it with winzip

    it will give you 4 normal folthers and one extra black that it looks like the capx with diferent name

    then you right click in that new black capx and open it with notepad

    then look for this <saved-with-version>24500</saved-with-version>

    then just change it to your version and then save it, after that you will be able to opn it

    example if you have the r244 version then change the <saved-with-version>24500</saved-with-version>

    to the version <saved-with-version>24400</saved-with-version>

  • can you share your capx I will quick have a look

    ok I can see your picture now , use families will easier and better performance if you have many enemies

    it looks like you adding to the health instead of subtracting

    Try this then

    health >>is equal = or more than 10

    subevent >> for each enemy >> Action Destroy

    here is the pictures from my events

    https://www.dropbox.com/s/hemg3o2wapsloau/Health%20bar%20pic.png?dl=0

  • dropbox.com/s/itmt0kmsks26jau/RPG.capx

    YAOOOOO man that was so many events lol I got lost, normally you clean and share the minimum events enough events just to reproduce the problem with destroying enemy

    Anyway there is so many things there that need serious optimization and I haven't got time to check it I should be sleeping by now

    first, let's talk about the problem>> destroying enemy

    on event 13 you have

    Sprite10 is overlapping Hitbox

    Action> Sprite10 destroy

    Action> ad 1 to Enemy health

    1-You see the problem here you don't reference to which Enemy belongs the Hitbox so because of that it picks all the enemy's and add 1 to each enemy health, you see that's why you kill all in one go

    2-another thing that you need to change is instead of the overlapping use on collision is better why you don't use sprite10 on collision with enemy >> Action sprite 10 destroy, add 1 to enemy health that's it more simple

    but if you gonna use overlapping you should use trigger once so it runs only once not every thick

    you have the overlapping condition every where without the trigger once that it will cause you a lot of problems, normally the things that they need to run just once add the trigger once but if need to be constantly true then don't add the trigger once like event 73 and 74

    3- when you wanna check for the health to destroy enemy's is

    enemy health is bla bla

    For each enemy: destroy <<<<<<<<<you see is for each enemy not >>for each sprite >> because here you

    have referenced the player sprite not the enemy

    That's a few things that a quick saw I didn't check everything but you should really check everything and optimized

  • Thanks so much! You should sleep, but if you can I'd really appreciate if you could help me with the hitbox and damaging situation, I'm decently new to construct 2 anyways, either way, thanks a ton.

    How would I assign a hitbox to an enemy without having to write infinite lines? so that I can have multiple enemys and they all have their own hitboxes

  • dwtiger

    No problem man, glad to help

    here is a capx that you can open with the c2 version r244

    https://www.dropbox.com/s/otapg6806888ltg/Health%20bar%20version%20r244.capx?dl=0

    now this capx is for the health bar only but it should be exactly the same process to create the hit box for the enemies.

    Just in case that you cannot open the capx, the basic idea will be:

    first, put the hit box in a family and lets named Hitbox and to that family add an instance Variable lets named >>BelongsTo >> plus add swell the Pin behavior to that family

    second, put your enemy in a family and named example enemies and add an instance Variable lets named MyUID and another variable >> Health

    Note: the tree instances Variables (BelongsTo, MyUID and Health) are variables type >> Number

    Now let's do the events side: one more Note before we start >>when I say sub event means right click in the top event that we currently working on, and select >>Add>> then >>Add sub-event

    let's start, the first Action will be created the hitboxes at the start of the game and Link them to their enemies

    1- On the start of the Layout: >>Action :>> chose the family Hitbox >> then destroy

    now sub event of this event>> For Each >> choose the family enemy >> now the Actions> goes in this line not in the top event

    >>>Action: chose family enemy >> set variable >>MyUID to >> self.UID

    >>>Action: system>>create "hitbox family" >>on layer (you choose)>> at> enemy.X, enemy.Y

    >>>Another Action>> "hitbox family">> set Variable "BelongsTo" >> to >>enemies.UID

    >>>Another Action>> "hitbox family">>Pin to enemies

    Now with this, we finished the initial preparation, so how do we refer the enemies and their hitboxes?

    2- Create New event: sprite10 >> On collision with >> "Hitbox family" >>Action: destroy sprite10

    Sub Event of this event: >>enemies >> compare var >> MyUID >>Equal to >>hitbox family var: BelongsTo

    Action: in the same line of the sub : enemies add or substract (Amount ) >>from enemies >> Health

    3-Create New Event: >>Enemies family >> Health >>is equal or greater than 10

    Sub Event of this event >> For Each >>enemie family>>Action Destroy

    Sub event of this Sub event >> hitbox family>> var BelongsTo>> is equal to>>

    Enemies Family>>var MyUID: Action >> Hitbox familie >> destroy

    That's it Let me know if you have any problems, Make sure you put All the Actions in their correspondent line if is top event actions you put them at the top if is a sub event actions put them in the sub event if is for a sub sub event then the same put them there in the sub sub event other ways you will break the code

  • ok I've tried working this out but it seems that I might have messed up somewhere. Here's the capx, and thanks again!

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