Use an Array for Image Loading?

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

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

    I'm not sure if this is the best solution.. <img src="smileys/smiley24.gif" border="0" align="middle" />

  • 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.

  • 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.

  • If you want the array to be a virtual representation of the grid, going the vector way is definitely not the way to go. Going 3d is more intuitive and human readable, and take as much as memory space.

    However, I don't think he needs any kind of array representation of anything. Normally, you would build a grid or map using the info you've got stored in the array, not the other way around.

  • and take as much as memory space.

    Are you totaly sure about this ?

    However, I don't think he needs any kind of array representation of anything. Normally, you would build a grid or map using the info you've got stored in the array, not the other way around.

    As I said, he aks, I answer ^^

  • Are you totaly sure about this ?

    eah, in the end it all comes down to the number of elements in the array. A 10x10x2 3d array is pretty much the same as a 100x2 2d array in term of memory allocation.

  • hen, 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)

    This is the information i was searching for. Just didnt know how to access more than one value for an instance.

    I will try this and post a little tut for success in time. :-)

  • Is there no way to set the array grid data without having a grid as sprites?

  • Is there no way to set the array grid data without having a grid as sprites?

    Event

    Array:For each element X or XY or XYZ

    Depending on how you want to use the array.

  • Is there any keyword list here on Scirra to take a look at the sense of keywords like "Loopindex" in the Event Editor?

    A kind of expression / function list for c2?

  • xeed, haven't you seen the object panel while the parameters dialog is up? It lists every expression available in the project.

  • I'd see it, but it is not available for keywords like "loopindex", which are defined before the action.

    Anyway, i tried to fit the coordinates of the grid into an array, but the output doesn't work. Text keeps "0". U got any ideas?

    <img src="http://85.214.39.20/shared/arrayAccess2.jpg" border="0" />

  • I'd see it, but it is not available for keywords like "loopindex", which are defined before the action.

    I don't understand. It's right there in the list, under the System object, next to the "objectcount" expression.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)