For Each Object

Discussion and feedback on Construct 2

Post » Thu Sep 08, 2011 3:23 pm

Hi guys,

I'm trying to get the For Each Object event working but I am not having much success... Here is a snippet of some test code.



1. I create five sprites on start of layout, then display the sprite.count which displays 5. This is correct.

2. I then added a sub event to destroy them. It only destroys 1 sprite. The count is then displayed as 4, which is correct.

The For Each doesn't seem to iterate through all the objects. Am I misunderstanding something?

Sim
simwhi2011-09-08 16:15:03
B
26
S
5
G
5
Posts: 151
Reputation: 6,279

Post » Thu Sep 08, 2011 4:32 pm

Make another event for the "For Each" statement. I think the fact that you are creating new objects in the parent event is messing up the picking.
B
55
S
12
G
8
Posts: 339
Reputation: 9,334

Post » Thu Sep 08, 2011 8:29 pm

The 'create object' action sets the picked objects to just the created object.

Sub-events inherit the parent event's picked objects, so the subevent inherits a single Sprite (the last created one).

For-each only iterates the currently picked objects, so it runs once for that last created sprite. (In this case you don't need a for-each, it has no effect - actions naturally run "for each" picked object. You only need a for-each when this built-in behavior does not do what you want.)

To fix it, make a second 'start of layout' event that just has 'wait 5 seconds' then 'destroy Sprite'. That event has a blank slate for picked objects, so all sprites are destroyed.
Scirra Founder
B
384
S
227
G
86
Posts: 24,139
Reputation: 190,761

Post » Fri Sep 09, 2011 8:48 am

HI Ashley and Cow_trix,

Thanks for the help. I need to rethink my approach when implementing such functionality with Construct 2. I have much to learn...

Cheers

Simon
B
26
S
5
G
5
Posts: 151
Reputation: 6,279


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 2 guests