looping array

Get help using Construct 2

Post » Wed Oct 09, 2013 9:52 pm

hey im newby...trying to store x and y positions of instances in an array then print out the array to a text object

not quit sure how to go about it. got it working for the x diumension but confused about storing and accessing x and y together..

looks like there is no loopindex for array loops so i created a variable and am incrementing on each loop which seems to work



image of events:

image of layout:

thank you for your assistanceblade33ru2013-10-09 22:15:35
Posts: 23
Reputation: 427

Post » Thu Oct 10, 2013 1:47 am

For the array index when doing a For Each you use array.curX or array.curY or array.curZ

ArcadEd2013-10-10 01:47:32
Posts: 1,549
Reputation: 22,103

Post » Thu Oct 10, 2013 9:26 am

I always thought using "X", "Y", and "Z" for the three dimensions of an array could be misleading for beginners. And this is a good example for it.

If I understand it correctly, you're having some instances of a sprite and want to store their X/Y position values into an array?
Let's assume there are 10 instances. For each instance you need to store 2 values (X and Y). That makes 20 values to be stored.
But if you use an array with width and height set to 10, there are 100 cells (10*10), so there are 80 cells in the array that you never use.

Try to think of other names for the array's dimension. For example, call the width of an array "sprite instance" and the height "position values". Now it gives you a better approach.
If you think of it like that, you would set the dimensions of the array: "sprite instance" to 10 and "position values" to 2. That creates an array with 10 * 2 = 20 cells, exactly the amount needed.
And you would access them with an index. 0 = first instance, 1 = second instance, etc.
Array.at(0,0) -> x-position of first instance
Array.at(0,1) -> y-position of first instance
Array.at(1,0) -> x-position of second instance
Array.at(1,1) -> y-position of second instance

For the index, there are several options. For example, if the instances of the sprite are once created and then never destroyed, you could simply use their .IID or you could add an instance variable that you set to a unique number per instance, beginning with 0.

I know this sounds confusing at first, but if you get used to it you will avoid further issues. (Just think of 80 sprite instances for example, that would make your array use 6400 cells with your current method, although only 160 are needed)
Posts: 1,821
Reputation: 8,289

Post » Thu Oct 10, 2013 1:13 pm

Yeah i had understood the part about the array size conceptually, but im having trouble understanding how to implement it..

Also a problem is how to pull information from the array sequentially.
Am I correct in hearing that array.curX is more or less like loopindex?

If thats the case then there is no need for me to have that incrementing variable - ticker.

what im trying to do is have 2 rows of information : row one with the X position and row 2 with the Y position. The idea that i might be able to record all the sprites positions or the mouse position for example.

But also looking for something incredibly simple. A lot of times programming examples can get too complex to understand fundamental concepts. I think the manual needs more information on this feature. or more practical examples. the best video i found was this http://www.youtube.com/watch?v=tfN48LmwBGg

incidentally i actually got it working saving and displaying the value of the x position...then i got confused about how to save both X and Y. do you do it in the same loop?

conceptual problems.blade33ru2013-10-10 13:49:15
Posts: 23
Reputation: 427

Return to How do I....?

Who is online

Users browsing this forum: dop2000, kelany and 3 guests