What is wrong with this array

Get help using Construct 2

Post » Wed Jan 23, 2013 11:11 am

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


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)


It creates 10 to 10 sprites ?!?

Why not 11 ?

B
7
S
1
G
1
Posts: 76
Reputation: 904

Post » Wed Jan 23, 2013 11:37 am

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)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Wed Jan 23, 2013 12:50 pm

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 ?
B
7
S
1
G
1
Posts: 76
Reputation: 904

Post » Wed Jan 23, 2013 1:09 pm

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 :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Wed Jan 23, 2013 1:59 pm

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.

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

Thank you very much.Spider_hip2013-01-23 17:39:10
B
7
S
1
G
1
Posts: 76
Reputation: 904

Post » Wed Jan 23, 2013 3:39 pm

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.

https://dl.dropbox.com/u/56268958/test-procedural2.capxSpider_hip2013-01-23 15:56:30
B
7
S
1
G
1
Posts: 76
Reputation: 904


Return to How do I....?

Who is online

Users browsing this forum: Muini, Yahoo [Bot] and 17 guests