For/Next with changeable variable.

Discussion in 'Questions (Windows Mobile)' started by enonod, Jul 12, 2009.

  1. enonod

    enonod Well-Known Member Licensed User

    Is there anything wrong with using variables in For/Next because the code below only does 3 columns, the last next seems to skip? It should do columns 0 and 14 then 1 and 13
    Code:
    Sub AddSprites
    Lft=
    0
    Rgt=
    14
    'Add the master frames
       Master.New2(AppPath & "\Psprite.png",6,15,15,6)
    'Add remaining sprites, 3 colours
       For i=0 To 1
          
    For col=Lft To Rgt Step Rgt
             
    For j=0 To 14
                Pawn.New4(Master.FrameBitmaps,
    6,15,15,6)
    'Binary data for later XOR, zero not usable so start at 1
                Pawn.Data=Bin.ShiftLeft(1,j Mod 3)
                Pawn.X = (col)*
    16+1
                Pawn.Y = ((j+(i)*
    14Mod 15)*16+1
    'Zero frame not usable as binary so -1
                Pawn.CurrentFrame=Pawn.Data-1
                gw.SpriteAdd(Pawn.Value)   
             
    Next
          
    Next
          Lft=
    1
          Rgt=
    13
       
    Next
    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can use variables in the for/next loop.

    In the second iteration the 'for' line equals to:
    Code:
    For col=1 To 13 Step 13
    So col will first be 1 and then 14 which is higher than the upper limit.
     
  3. enonod

    enonod Well-Known Member Licensed User

    Thank you Erel, I have been on that for ages.
     
  4. agraham

    agraham Expert Licensed User

    Lft=1
    Rgt=13
    For col=Lft To Rgt Step Rgt

    First pass
    col = 1

    Second pass
    col = 1 + 13 = 14
    14 is > Rgt(13) so loop ends

    EDIT:- Damn :(, Erel got in while I was writing a reply :sign0098:
     
    Last edited: Jul 12, 2009
  5. enonod

    enonod Well-Known Member Licensed User

    Thank you also agraham. I am a person that suffers from fence post syndrome!
    Anyway, there is also another fault having I have spotted in that line, that I wouldn't have seen but for the comments posted. In the first loop I am starting from zero (necessarily) and so what will now become step(rgt-1) still wont allow the column 14 to work but 13 will. Back to the drawing board. [Edit] so the line should read For col=Lft To Rgt Step (rgt-lft)
     
    Last edited: Jul 13, 2009
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice