How do I Store Object Positions in Arrays

Get help using Construct 2

Post » Sat May 30, 2015 2:43 am

Good evening! I'm trying to store the positions of instances of a single object sequentially in an array's index values. Here is the desired effect that I want to be applied to the array.

Image

As you can see, each value of the array from start to finish follows in a logical order. I want to be able to call on those values through something like an expression later on, to the effect of something like "Set position of (x) object at (Array.Width-5)"
B
9
S
2
Posts: 34
Reputation: 679

Post » Sat May 30, 2015 7:51 am

Well, there's three ways you can do this (at least).

Firstly, create them programmatically, rather than placing them on the layout with drag and drop from the C2 editor, call System.CreateObject in a for loop, then you can use loopIndex to store them in a 2D array.

Secondly, you could give each an instance variable which is their index in the array (you could use the numbers in the text if these are containers with a sprite and text object in them) and use foreach to copy those values to an array

Thirdly, if you know they are going to be top left to bottom right ordered, you can use for each (ordered) with an expression something like button.X + button.Y * 10000 which will order them for you in that order, as long as your layout is less than 10000 high :)
My tutorial through development blog is at http://myconstruct2dev.blogspot.co.uk/
B
12
S
3
Posts: 221
Reputation: 1,328

Post » Sat May 30, 2015 10:13 am

With what I have set up the first option isn't viable. Not only that, but the first option would have me call in objects at specific locations, correct? I don't want objects to be spawned at specific pixels in the layout that are solely relative to the layout. I have objects spawning relative to other objects however, which works perfectly for what I want.

I don't think the second option or third are very viable either. Again, I'm trying to store the locations of instances of objects that are already in the layout as the array's index values.
B
9
S
2
Posts: 34
Reputation: 679

Post » Sat May 30, 2015 6:57 pm

Bump. Still working on a solution for this.
B
9
S
2
Posts: 34
Reputation: 679

Post » Sat May 30, 2015 7:48 pm

Image
B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Sat May 30, 2015 8:43 pm

korbaach wrote:Image


From the looks of this it appears that the actual index values in the array are being changed to the X and Y values of the desired objects to be stored. Is that the case or am I looking at this wrong?

What I want is for the normal index values in the array (0 to Array.Width-1) to be kept so that I can call on them later, but I want their values to contain the positions of the objects.

For example, let's say I have an object at (400, 500) in the layout. I store this object's position in an index of my array. So now if I said "Set position of (X) object" to Array.Width-3 (Let's say that index in the array contains the (400,500) location), X object would go to (400,500) instead of me directly telling X object to go to (400, 500)

The idea is that no concrete position is being stored in the array because that position will change based on where the object is moved.
B
9
S
2
Posts: 34
Reputation: 679

Post » Sat May 30, 2015 10:04 pm

korbaach wrote:Image


now you have something like this:


Image

object X set position to X= Array.Width-3,0
........................................ Y=Array.Width-3,1

capx> http://s000.tinyupload.com/index.php?fi ... 8336073068
B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Sat May 30, 2015 11:11 pm

You sir, need to understand how amazing and helpful you are. Thanks again. :lol:
B
9
S
2
Posts: 34
Reputation: 679


Return to How do I....?

Who is online

Users browsing this forum: mariusvm, zenox98 and 13 guests