using foreach with multidimensional arrays

Previous topic - Next topic

Richard Rae

Another question:)

Can the foreach command be used on a multidimensional array.I have tried using the following code but it only shows one line

// Project: alpha
// Start: Sunday, July 22, 2007
// IDE Version: 4.237



   
   LOADSPRITE "red.png", 1



   
   LOADSPRITE "green.png", 2


   
   LOADBMP "background.png"



   TYPE ball
   x
   y
   colour

   ENDTYPE

GLOBAL balls[] AS ball
 DIM  balls[9][9]

   FOR y=0 TO 8
   FOR x=0 TO 8

       balls
  • [y].x=x*64
       balls
  • [y].y=y*64
      balls
  • [y].colour=RND(1)+1
      NEXT
      NEXT




WHILE TRUE


ALPHAMODE -1

foreach item in balls[]
      xx=item.x
      yy=item.y
      col=item.colour
     
      sprite col,xx,yy
next
   
   
   

SHOWSCREEN


WEND

Kitty Hello

No. It only acts on the first array index. Multidimensional arrays will return the first item
  • always. Sorry.

Richard Rae

No problem Gernot.I will just use a nested for next loop instead.Thanks for the quick reply.