Repeat isn't working properly and I have no idea why

Discussion and feedback on Construct 2

Post » Thu Jul 20, 2017 8:55 pm

The situation is that:

When Space is pressed, system should pick three random instances of the same object (there are 9 copies of it on the layout) and destroy them. What is happening, though, is that the number of instances destroyed is inconsistent: sometimes it works correctly, sometimes it destroys only two, and sometimes just one. The code is literally just that:

Image

Why is this not working?

PS: This is the first time I work with both "Pick Instances" and loops, so probably I'm missing some optimization setting/tool that I'm not aware of.
B
17
S
5
Posts: 16
Reputation: 1,191

Post » Thu Jul 20, 2017 9:05 pm

The way that logic is set up, it picks the 3 random sprites first before doing anything else, so it can pick the same one more than once.
B
46
S
16
G
74
Posts: 2,088
Reputation: 44,185

Post » Thu Jul 20, 2017 9:07 pm

plinkie wrote:The way that logic is set up, it picks the 3 random sprites first before doing anything else, so it can pick the same one more than once.


Wow! But What am I doing wrong then? What should I do to fix it?
B
17
S
5
Posts: 16
Reputation: 1,191

Post » Thu Jul 20, 2017 10:21 pm

I would use a function for something like this because of the trigger event. I've left a wait in so you can actually see what it's picking.
https://www.dropbox.com/s/omt8tfzvdsvga ... .capx?dl=0
B
46
S
16
G
74
Posts: 2,088
Reputation: 44,185

Post » Thu Jul 20, 2017 10:54 pm

What's actually happening is the sprites are being marked to be destroyed at the end of the event. They aren't being destroyed right then which is why they can be picked multiple times.

One solution would be to add a Boolean variable "alive" with an initial value of true and then do this:

Space pressed
Repeat 3 times
Sprite Is alive
Pick random Sprite
--- Sprite: set alive to false
--- Sprite: destroy
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 6 guests