Use an Array for Image Loading?

Discussion and feedback on Construct 2

Post » Thu Jul 21, 2011 10:48 pm

I am from Germany. At this Point Thanks for your fast support. :)

Made the ground now as tiled background, and as a symbolic grid. On the ground are many (transparent) PNG's on the upper layer. These are so much, that it would take too long to arrange them manually with click->x,y.
I am gonna try this with an array. In the array I set the square postitions. Then I check for every sprite instance if it's near the upper left position of any square to be pointed at listed in the array. I think this is the best solution until tiled maps are supported.
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Thu Jul 21, 2011 11:22 pm

X and Y positions are the dot in the middle of your sprite (not it's left upper corner)

You should try to post a .capx with some comments in it I think. That would help me visualize what you are up to.


EDIT: More about arrays and loopsKyatric2011-07-22 02:10:42
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Fri Jul 22, 2011 3:30 pm

Are you looking for something like this ?

I create 100 tiles with a simple repeat and then order them in a 10x10 grid layout using 3 different (but similar) ways.
B
45
S
16
G
18
Posts: 845
Reputation: 13,673

Post » Fri Jul 22, 2011 6:37 pm

That helps a lot, Thanks! :)

I did put the coordinates in an Array before, to use them later again for checking the nearest position to the square midpoints.

But how can I access the single values now in this filled array? It is one-Dimensional and has 2 "Instance Variables", in which I hope they are the values for EVERY element, not for the whole array. ^^ If its global I dont see a way to solve this problem.

(If I made this, I will create a little Tutorial about adjustment with nooby-placed objects. :D )

xeed2011-07-22 18:41:00
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Fri Jul 22, 2011 7:57 pm

What exactly are you storing inside the array ?
B
45
S
16
G
18
Posts: 845
Reputation: 13,673

Post » Fri Jul 22, 2011 8:02 pm

2 numbers per instance, x_pos and y_pos

(I guess)...declared as Instance Variables

Dont really know if i need 2-dimension array to store a grid in c2. But if it is, how do i access mapGrid[4,6] for instance? I did not find a solution for this before, maybe too cause i dont know most of the expressions. But also did not find a expressions.wiki here anywhere. The link to the expression help inside c2 does not work.xeed2011-07-22 20:14:15
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Fri Jul 22, 2011 8:34 pm

The best for storing two (or more) variables of a grid of instances would be a 3-dimensional array with the x,y (width and height) indexes the coordinates of an instance and the remaining dimension (depth) would be use to store each variable.

However, if you plan on storing the positions in the array, I think it's pointless, since you can retrieve the values directly with the instances anyway.
B
45
S
16
G
18
Posts: 845
Reputation: 13,673

Post » Fri Jul 22, 2011 8:57 pm

So I should use the index of an array as pixel positions?

I'm not sure if this is the best solution.. xeed2011-07-22 22:28:40
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Fri Jul 22, 2011 10:30 pm

One way would be doing arithmetics with the pixel coordinates to transform them into array "coordinates".

("PixelX of tile" - "Grid Xoffset from (0,0)") / "tile width" = x index
("PixelY of tile" - "Grid Yoffset from (0,0)") / "tile height" = y index

Or you could go the simple way and add instance variables to the tile, one for each index, and fetch data from the array using these.
B
45
S
16
G
18
Posts: 845
Reputation: 13,673

Post » Sat Jul 23, 2011 5:06 am

Errr you are overcomplicating things imo.

First thing first, Xeed you got it wrong considering the use of the array object.
Instead of having some system loops, you have an event within the array object to help you browse through it. (Array:For each element)

There you determine if you want to go through the X coordinates, the XY or the XYZ.

That means that some loops will be automaticly done (cycling through the coordinates) and the action you set will apply to the "cell" at said-coordinates.

For example I have a 10X10 array, I want to fill each cell with a value I'll do:
System:On startup of layout
..Array:For each element XY
.....Action - Array Set at XY

This action has 3 Fields : The X coordinate, the Y coordinate and the Value.
X and Y can be obtained with the system expression Array.CurX and/or Array.CurY (Where Array is the name you gave your array object).
Then you set Value as you like.

Instance Variables apply for the whole object. In your example, you modify the values of your variables posX and posY for the size of your array, but in the end, you only have a single value for each variable.

In fact, in your case, I guess that you would like something like :
X in Array represents an instance of sprite Ground
Y in Array will hold "posX" and "posY" (so respectively Y=0 and Y=1)

This mean that in array coordinates : 0,0 will be instance 0, posX for what you're willing to achieve. 0,1 will be instance 0, posY. 1,0 will be instance 0, posX, etc...

First thing to do in the startup is to set up the size of your array.
Action Array:Set size. Three fields again Width (X), Height (Y) and Depth (Z).

In our case, Width will be the final number of Ground instances that you will deal with (if instances are already created you can access it thanks to Ground.Count), Height will be 2 (posX, posY in your logic).

Then, you will have a "For each Ground" loop in which 2 actions Array.Set at XY will be used.
First one: X is Loopindex, Y is 0, Value is Ground.X
Second : X is Loopindex, Y is 1, Value is Ground.Y

And there you have it. Your array will be filled with the X and Y positions of each Ground sprite instance.
To access it : Array.At(X,Y)

If I want the position X of my 9th instance : Array.At(9,0) (9 is the 9th instance, 0 is X).
If I want the position Y of my 52th instance : Array.At(52,1)

There you got a "proper" use of the array object.
Take the time to get used to it, it's really tricky at first.



But in the end, I'm still not convinced that what you need is an array. As you didn't explained yet clearly what you are trying to do, I answer hit by hit to your questions but I think you're overdoing at some point.

I'm thinking that you are trying to compare a character's sprite coordinates with the Ground coordinates, to know where it is.
You could achieve this by checking which ground instance is the character overlaping, and you would then have direct access to the X and Y coordinates of ground without having to store them in an array.
But yet, as I don't know if that's what you want to do, maybe I'm missing the point there.

Anyway, hope to have helped about the array.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: TRMG and 17 guests