News:

Function Finder  Find all the functions within source code files

Main Menu

For Next Loop Problem

Started by Mick Berg, September 26, 2005, 11:25:36 AM

Previous topic - Next topic

Mick Berg

Hi Could you please explain to me why the for- next loop doesn't work, but the expanded out version works fine. Sorry to be so simplistic,] but we all have to learn somehow! I'm simply putting five similar characters on the screen.  
;This simple code works;
POSITIONSPRITE 51,(EdXpos+100),EdYpos+RND(20)
ROTATESPRITE 51,-5
SPRITEIMAGE 51,(2+i)
POSITIONSPRITE 52,(EdXpos+200),EdYpos+RND(20)
ROTATESPRITE 52,-5
SPRITEIMAGE 52,(2+i)
POSITIONSPRITE 53,(EdXpos+300),EdYpos+RND(20)
ROTATESPRITE 53,-5
SPRITEIMAGE 53,(2+i)
POSITIONSPRITE 54,(EdXpos+400),EdYpos+RND(20)
ROTATESPRITE 54,-5
SPRITEIMAGE 54,(2+i)
POSITIONSPRITE 55,(EdXpos+500),EdYpos+RND(20)
ROTATESPRITE 55,-5
SPRITEIMAGE 55,(2+i)

;and here's the for-next that doesn't work
FOR lp=51 TO 55
  POSITIONSPRITE lp,(EdXpos+(lp*100)),EdYpos+RND(20)
  ROTATESPRITE lp,-5
  SPRITEIMAGE lp,(2+i)
NEXT

empty

#1
FOR lp=51 TO 55
POSITIONSPRITE lp,(EdXpos+(lp*100)),EdYpos+RND(20)
ROTATESPRITE lp,-5
SPRITEIMAGE lp,(2+i)
NEXT

I marked the problem in bold. The first iteration would result in
lp*100 = 51*100 = 5100
So way out of the screen.

Solution

FOR i = 1 to 5
  POSITIONSPRITE lp+50,(EdXpos+(lp*100)),EdYpos+RND(20)
  ROTATESPRITE lp+50,-5
  SPRITEIMAGE lp+50,(2+i); NOTE: I don't what image number you want to assign here.
NEXT

Mick Berg

Well,I'm glad it was just a silly mistake on my part, but sorry to waste your time with it.
Thanks!

empty

No problem. Glad I could help.