LoginLogin

Can't figure out why FOR loop ends?

Root / Programming Questions / [.]

NoxusCreated:
I'll add the download key thingy once it uploads. Also as dumb as it sounds, the stone is the first layer, and the dirt is underneath it. I'm trying to make a semi-random terrain piece the size of the screen, and basically I'm trying to figure out how close the stone is to the bottom, then running a second FOR loop to place DIRT until it reaches the bottom, aka fills the screen.
STONE=0   'x coord of the stone
STONEH=10 'height/y coord of the stone
DIRTH=10
SKIP=1 `to skip the loop if the STONEH value is too high (otherwise the dirt will be placed off screen and result in an error)

FOR I=0 to 24 `24 is the length of the screen BG wise
 SKIP=0
 IF STONE >14 THEN STONEH=14
 BGPUT 0,STONE,STONEH,2
 INC STONE,1
 DIRTH=STONEH+`
 STONEH=STONEH+(-RND(2))+RND(2) `the actual random terrain factor
 IF DIRTH>14 THEN INC DIRT,1:SKIP=1
 DIRTX=16-STONEH '(filling in the bottom part with dirt)
  WHILE SKIP==0
   FOR X=0 TO DIRTX
   BGPUT 0,DIRT,DIRTH,332
   INC DIRTX,1
   NEXT
 WEND
NEXT

Your code here:
WHILE SKIP==0
 FOR X=0 TO DIRTX
  BGPUT 0,DIRT,DIRTH,332
  INC DIRTX,1
 NEXT
WEND
You never set SKIP to 1 after it finishes. I think an IF statement would be appropriate instead of a WHILE loop. So,
IF SKIP==0 THEN
 FOR X=0 TO DIRTX
  BGPUT 0,DIRT,DIRTH,332
 NEXT
ENDIF
Another problem: You never allow the FOR to end. You keep increasing DIRTX each loop. That never allows X to catch up. That's why I removed INC DIRTX,1 in the code example. If you're doing INC SOMETHING,1 then you can remove the ,1. It's implied if there's no second argument. So INC DIRTX is equally correct and shorter to write than INC DIRTX,1.