How do I make multiples of the same enemy?

0 favourites
  • 11 posts
From the Asset Store
Pixel Enemies for SHMUP consists of 45 enemy ship sprites to be used in your game.
  • Hi, I feel like a real dummy right now, I'm having trouble figuring out why my code isn't working and I could use some help.

    Here is all of the movement code for this enemy. When they have line of sight with the player, their state changes to 3 if the player's on their left and 4 if the player's on their right. If they don't have line of sight, they randomly choose a behavior between 0 and 2 every couple seconds. This works perfectly! ...For the first enemy I plop down. Any more enemies I put down do not follow this behavior and don't move at all until the player enters their LOS. They also do not lose sight of the player upon leaving their LOS.

    What am I doing wrong here? Again, the first enemy I plop down on the layout works exactly as I'd like it to including losing the player upon exiting its LOS.

    edit: Also, when one is alerted, the awareness marker appears over all of their heads instead of just the alerted one.

  • Hey, I can't identify an obvious problem from your events and it's a bit odd your first enemy works while the rest don't. I'm wondering if it is something to do with how your events are structured which is throwing the whole thing off.

    Could you post up/PM me your capx and I'll have a play around with it?

  • I can't see any issue either from your screenshot... using a "for each" loop should pick each instance that meets the conditions you have specified.

    A capx would be beneficial so we can play around with it and see what can be done.

    ~Sol

  • I managed to fix the problem by removing everything from the 'for each' event. I've got no worldly idea why that worked, because I thought it had to be inside the 'for each' to work on an individual basis, but it did! If either of you would like to take a look at my capx and help me figure out why (which I'd definitely appreciate), here it is:

    https://www.dropbox.com/s/jpn6loz49rm41 ... .capx?dl=0

    Use the arrow keys to move. Skeletons should aggro on you individually when you come within their LOS range variable.

    edit: I should also note for posterity that the sprites aren't mine, they're from Castlevania, they're just there as placeholders until I bring in an artist to help me (I have no artistic talent whatsoever).

  • So I've had a play around with your Capx and as far as I can tell you don't need to remove all the var_state events from the for each enemy loop. The issue you described only seems to arise when you put the LOS condition inside the loop. When that condition is outside the loop, it all (seems) to work pretty well! Is that correct?

    I can only guess that this is caused by the system looping through the enemies quickly, which causes an problems with the calling of the every 2 second condition.

  • You're right, removing the line of sight stuff from the 'for each' event fixed everything. Weird! Thank you for the help.

    I am having another logic flaw issue that I can't untangle that maybe you could help me with as well. I have a state for the enemy attacking that creates a hitbox every time a countdown variable reaches 0 (along with setting the enemy to the attack animation):

    It subtracts 1*dt from the countdown whenever the enemy's state is 6, and at 0 the attack happens and it resets to 1. In theory, this should work for each skeleton individually since it's under the 'for each' event.

    But it doesn't. When more than one skeleton is in range at once, it creates dozens of hitboxes at once even though I have those 'trigger once' actions set. At 3 skeletons it stops subtracting from both their countdowns. As more skeletons come in range it gets even more broken. What am I doing wrong now?

    https://www.dropbox.com/s/jpn6loz49rm41 ... .capx?dl=0

    Here is the updated capx file for you to fiddle with.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok so I did a quick restructure and got what you want working . It was weirdness from the for each enemy loop and your subtract 1*dt. I removed the var6 code in the screenshot from the for each enemy loop and changed a couple of things.

    http://imgur.com/I9WklhU

    Ok I've added a link because the screenshot was crazy out of the box for some reason.

    This works how you want it, while the player is stood still. If you run through groups of enemies you will still see high hitbox values, because you are swapping var states as the player moves past and therefore removing the attack cool down associated with var6. I'd recommend adding another var (attack cool down) which is independent of the enemy state vars.

    Let me know if I can help anymore .

  • Ahh, thank you! That's also very odd, I don't quite get why the 1*dt was making things go screwy. The only trouble now is that when multiple skeletons are in range, they all attack at the same time, which I was trying to keep from happening (unsuccessfully, as we've seen ). It subtracts from var_attackcountdown at the same time for every skeleton, instead of subtracting on an individual basis. Any ideas?

  • You could increase your var_attackcountdown by one, which should introduce some variation in the pace at which your enemies attack. I just had a go and got two groups attacking at different times. If you want to keep the 1s attack time you could subtract 0.5 every second. That help?

  • That works! Thank you for all the help.

    I'm still a bit mystified as to why your fixes worked so if anybody can shed some light on that feel free, but other than that, I have no more issues at the moment.

  • Haha can I say magic? Honestly I'd love an explanation too - both of your problems were effectively caused by nested every X second events within a for each loop. It seems that this generally doesn't work? My best guess is that the interaction between the way the loop is executed and the every X seconds basically breaks the every X seconds.

    Feel free to PM me/quote me if you need any more help, if you just reply in here I might not necessarily see it .

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