Identical objects with private variables skipping events

For questions about using Classic.

Post » Fri Sep 12, 2008 7:50 am

I'm having a little issue here. I have an object with Private Variable "Health" on the layout I want to take damage, then to be destroyed, THEN immediately respawn outside the screen after it is destroyed.

Works fine with groups of objects, works fine by themselves when they're destroyed individually, but say two (or more) happen to get destroyed at the same time, it only registers one object as being destroyed, despite the fact that two have been destroyed. Thus in my scenario, where I want a new object to be created when another object is destroyed, I only have one new respawned object, despite the fact two have been destroyed.

For instance, if I systematically destroy two objects at a time (on purpose), I can slowly dwindle the respawning objects to 1 object on screen, despite the fact that I may require 20 enemies on the screen at once. Of course, once I'm down to 1 enemy left you can't kill two at a time, so, at that point the private variable thing isn't really an issue. haha.

It's not a big deal, but it is something I've run into. I'm experimenting with different applications of weapons that a player can upgrade to, and it seems like occasionally I'll start with a group of 8 or 9 enemies, and after a little bit of playing I'm down to 3 or 4. I want to start using larger projectiles and beams that can destroy a couple of enemies at a time, but if they're destroying all at once and not all performing the same actions I want then I may have to rethink how to do this by hiding, say, 20 projectiles at a time, behind a dummy "laser beam" graphic.

Anyway to get around this?
B
5
G
4
Posts: 50
Reputation: 1,225

Post » Fri Sep 12, 2008 9:53 am

Do you have a simple "Is overlapping" event handling the destruction of your enemy objects? The way I'm assuming this is happening is this:

1. Two enemies overlap beam
2. Two enemies are destroyed
3. Spawn routine kicks in because "enemy is destroyed"
4. Spawn routine only creates one enemy

What you might want to do is a For Each loop during your check for enemy overlap, like so:

[code:2xd3vwpp]
+For each enemy.sprite
+Enemy.sprite overlaps beam.sprite
-Destroy enemy.sprite
-Add 1 to global('spawn')
[/code:2xd3vwpp]

Then later when you do your spawning code, do like so:

[code:2xd3vwpp]
+Global('spawn') > 0
+Loop global('spawn') times
-System: Create object enemy.sprite at x, y
[/code:2xd3vwpp]

That way you always spawn the same number of enemies that you kill.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Fri Sep 12, 2008 10:06 am

I was just typing up a similar solution using 'For each' :D. That'd be the way to ensure all matching objects get created, as deadeye said.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Fri Sep 12, 2008 12:36 pm

Awesome. That is exactly what I was looking for. Thanks guys. :)

You guys are fast. lol.
B
5
G
4
Posts: 50
Reputation: 1,225

Post » Fri Sep 12, 2008 12:55 pm

No sweat, let us know how it works out :)
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Fri Sep 12, 2008 1:16 pm

Yeah, just for reference, here's what's going on here. If you have something like

+ Sprite Health < 0
-> Sprite: Destroy
-> Create 'explosion' at Sprite

If by chance two or more Sprites have Health < 0 at the same time (eg. a button to destroy them all), then multiple objects will be picked by the 'Health < 0' condition. The 'Destroy' action is called once per picked instance, but system actions have no regard for picked objects, and only run once. So you only get one explosion. Adding the for-each forces it to run the actions repeatedly for each instance, so you get enough explosions as well. This is true of any system action.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Fri Sep 12, 2008 2:09 pm

Another way to do it, though it would be a little sloppier is to have a "counter" object which has the number of "bad guys" that you want to have... and compare that to the actual number of bad guys each time one or more is killed...

essentially if the number of bad gys is less than the counter number, then spawn bad guys until the number is equal.

deadeye's way of doing it is way cleaner though.
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
45
S
17
G
37
Posts: 2,853
Reputation: 25,966


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests