Looping through objects

For questions about using Classic.

Post » Wed Apr 14, 2010 3:14 pm

I'm trying to make a map editor and i need to save information from objects to array. How can i loop through every object and insert their x and y positions and other values to their own slots in array?
B
2
S
1
G
4
Posts: 71
Reputation: 1,342

Post » Wed Apr 14, 2010 3:33 pm

Use a 'for each' loop, under the System object.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Wed Apr 14, 2010 4:45 pm

I know about for each loop, but i don't know how to use it in this case.

here's something i tried: http://dl.dropbox.com/u/4322334/array.cap
(use the debugger to see the array values)

I used unique id's to pick objects but it doesn't really work. There is also a bug, if i add behavior to the sprite, every other value in the array appears as zero.
B
2
S
1
G
4
Posts: 71
Reputation: 1,342

Post » Wed Apr 14, 2010 7:30 pm

Don't use UID use for each:
[code:2o5p8cj4]+ Button: On Button clicked
-> System: Set global variable 'das' to 1
+ System: For each Sprite
-> Array: Set index (global('das'), 1) to Sprite.x
-> System: Add 1 to global variable 'das'[/code:2o5p8cj4]

A UID is assigned to every object (text, sprite, mouse and keyboard, etc...) and every behavior. Say you have a cap with only one "Sprite" with a "drag and drop" behavior. The Sprite will have a UID of 0, and it's behavior "drag and drop" will have a UID of 1. So if you then add another instance of Sprite it's UID will be 2.
B
79
S
24
G
54
Posts: 4,738
Reputation: 40,739

Post » Wed Apr 14, 2010 7:51 pm

That seems to work perfectly, thanks.
B
2
S
1
G
4
Posts: 71
Reputation: 1,342


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 8 guests