What is wrong with this array

0 favourites
  • 6 posts
From the Asset Store
Be quick and choose whether the shown equation is right or wrong.
  • Hi all.

    array.width = 10

    array.height = 10

    I created an array with

    -for x from 0 to array width

    --for y from 0 to array height

    *create object sprite at(loopIndex("x")*17,loopIndex("y")*17)

    *array.set value at(loopIndex("x"),loopIndex("y")) to random(3)

    *sprite.set animation frame to array.value

    <img src="https://www.dropbox.com/s/mqlv5zmtzcub2wm/first.jpg" border="0" />

    so far so good It creates randomly framed 11 to 11 sprites.

    weirdness starts from now on

    -button on click

    --for each sprite

    *delete.sprite

    --array. for each xy element

    *create object sprite at((TwoDimensionalArray.CurX)*17,(TwoDimensionalArray.CurY)*17)

    <img src="https://www.dropbox.com/s/o81ezt8qq4bsx37/duo.jpg" border="0" />

    It creates 10 to 10 sprites ?!?

    Why not 11 ?

  • array.width = 10

    array.height = 10

    That means your array is 10 elements wide and 10 elements high.

    Indexing of arrays starts from 0, so your array indexing should be from 0 .. array.width-1 (9, which is the index of last array value)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for your quick reply.

    So actually at first, latest column(11th) and latest row(11th) didn't get any values beceause there is not 11th elements at array.

    So if I do like as you said, width-1 or if I start for from 1 to array.width does the same job right ?

  • Exactly :)

    Your question:

    From 0 to width-1 (indexes from 0 to 9) is not same as from 1 to width (indexes from 1 to 10)

    Both index 10 values, but as the array only has values in indexes 0-9, the later one would try to index array values that do not exist. This is not recommended, ever :)

  • Wow thank you for your great explanations.

    Now I get it.

    Finally I just want to ask one more thing. Already everything wrotes at comments at this example.

    dl.dropbox.com/u/56268958/test-procedural2.capx

    Thank you very much.

  • I'm sorry about first .capx I get it it was a bit silly.

    I just realized that array.front doesn't mean current(X)+1. It means first x point.

    So I changed my related code part with, Array.At(Array.CurX+1,Array.CurY)

    Now the problem is just above that part. It's,

    if currentX+1 's value is 0 or 1 or 2 ( I wrote this beceause if it's last column there won't be a (x+1,y)point. So there won't be a value. So I wanted to leave last column as it is.) But It doesn't do anything.

    The funny and weird part is that code was

    if currentX+1'value => than 0 and

    if currentX+1'value =< than 2 then do smth.

    When do code was like it was doing smth(not as I expected)

    Lol I'm confused that both codes should be same right ?

    Here is my latest capx file. Thank you for any help.

    dl.dropbox.com/u/56268958/test-procedural2.capx

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