Array Problems
-
but got a bit lost !
It's probably in the help but I couldn't find it.1 stars=200
2 zdepth=60
3 fudge=128.0
4 speed=0.19
5 star_size=6
6 array star0[201]
7 array star1[201]
8 array star2[201]
9 // generate star list...
10 for i = 1 to stars loop
11 star0[i] = (random(50)-25)
12 star1[i] = (random(50)-25)
13 star2[i] = (random(zdepth))
14 repeat
15 //ensure screen centre
16 cx = gWidth()/2
17 cy = gHeight()/2
18
19 while 1 loop
20 for pp = 1 to stars loop
21 star2[pp] -= speed
22 if star2[pp] <= 0 then //off screen? redo
23 star0[i] = (random(50))-25
24 star1[i] = (random(50))-25
25 star2[i] = zdepth
26 endif
27 //x,y,z to x,y
28 w = fudge/star2[pp]
29 x = (star0[pp] * w +cx)
30 y = (star1[pp] * w +cy)
31 col = {1,1,1,1}
32 //plot(x,y,col) //dots or
33 // circles...
34 circle(x,y,w/star_size,32,col,false)
35
36 repeat
37 update
38
39 clear()
40 repeat -
Arrays are 0 indexed, so each of your for loops should start at 0 rather than 1.
-
probably a typo, but update isn't written as a function call (update()). And Richard beat me to it with the 0 based comment, but here is your code formatted anyway:
stars=200 zdepth=60 fudge=128.0 speed=0.19 star_size=6 array star0[201] array star1[201] array star2[201] // generate star list... for i = 1 to stars loop star0[i] = (random(50)-25) star1[i] = (random(50)-25) star2[i] = (random(zdepth)) repeat //ensure screen centre cx = gWidth()/2 cy = gHeight()/2 while 1 loop for pp = 1 to stars loop star2[pp] -= speed if star2[pp] <= 0 then //off screen? redo star0[i] = (random(50))-25 star1[i] = (random(50))-25 star2[i] = zdepth endif //x,y,z to x,y w = fudge/star2[pp] x = (star0[pp] * w +cx) y = (star1[pp] * w +cy) col = {1,1,1,1} //plot(x,y,col) //dots or // circles... circle(x,y,w/star_size,32,col,false) repeat update() //added the () that where left out clear() repeat
-
@PB____ Sorry about the errors. Had to retype code into note pad. There really should be a better way of sharing code.
-
-
Yes, Richard's suggestion sorted out the array problem a treat. I remember with python, if you didn't format correctly - tabs etc - the compiler came up with a bucket load of errors which were a pain to find ! LUA was easier in that respect. Thanks again for the help.
-
Also, always a good idea to use
len(array)
as the upper bound of your for loop to save any accidental errors as well. Assuming that you want to loop all the way to the end of course. There could be times when you genuinely don't want to loop over all elements.