We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › loading 2d byte arrays into another array
Page Index Toggle Pages: 1
loading 2d byte arrays into another array (Read 236 times)
loading 2d byte arrays into another array
Feb 27th, 2009, 11:42pm
 
hello
i've got a bunch of 2d byte arrays and i'd like to maybe put them into another array so that i can shuffle through them. using arrow keys, one can shuffle through one of 2 byte array once (one left, one right) but ideally i'd like to index the 2d arrays and pass that index into the 2d byte array posContainer...
how would one pass 2d byte arrays into another array? would it have to be another 2d byte array?
any ideas?

-pv


Tile[][] tileMap;
Level one;
//
int cols = 14;
int rows = 5;
PImage blank01;
PImage road01;
PImage road02;
PImage rubble01;
PImage rubble02;
PImage[] tiles;
byte[][] posContainer;
//
boolean[] keys = new boolean[4];
final int L = 0;
final int R = 1;
final int U = 2;
final int D = 3;
//
void setup() {
 size(768,240);
 //(numPos, startPos);
 one = new Level(3, 1);
 blank01 = loadImage("blank01.png");
 road01 = loadImage("road01.png");
 road02 = loadImage("road02.png");
 rubble01 = loadImage("rubble01.png");
 rubble02 = loadImage("rubble02.png");
 tiles = new PImage[] {
   blank01, road01, road02, rubble01, rubble02
 };
 posContainer = one.pos02;
 tileMap = new Tile[cols][rows];
}
void draw() {
 background(0, 255, 0);
 one.loadMap();
 one.scroll();
}

void keyPressed() {
 if (key == CODED) {
   if(keyCode == LEFT) {
     keys[L] = true;
     println("left");
   }
   else if(keyCode == RIGHT) {
     keys[R] = true;
     println("right");
   }
   else if(keyCode == UP) {
     keys[U] = true;
     println("up");
   }
   else if (keyCode == DOWN) {
     keys[D] = true;
     println("down");
   }
 }
}
void keyReleased() {
 if (key == CODED) {
   if(keyCode == LEFT) {
     keys[L] = false;
   }
   else if(keyCode == RIGHT) {
     keys[R] = false;
   }
   else if(keyCode == UP) {
     keys[U] = false;
   }
   else if (keyCode == DOWN) {
     keys[D] = false;
   }
 }
}

class Level {
 int numPos;
 int scrollIndex;
 int scrollX;
 String currentPos;
 String[] grids;
 byte[][] pos00 = {  
   {
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0        }
   ,{
     0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 0, 2, 1, 0        }
   ,{
     3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 0, 0        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0        }
 };
 byte[][] pos01 = {  
   {
     2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2        }
   ,{
     2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 4        }
   ,{
     2, 1, 0, 0, 0, 1, 2, 0, 0, 0, 2, 1, 0, 0        }
   ,{
     4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 0, 0, 3        }
   ,{
     2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 2        }
 };

 byte[][] pos02 = {  
   {
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 4, 0        }
   ,{
     1, 0, 0, 0, 1, 2, 0, 0, 0, 2, 1, 0, 0, 0        }
   ,{
     3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 0, 0, 3, 4        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 2, 2        }
 };

 byte[][] pos03 = {  
   {
     2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1      }
   ,{
     2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 4, 0, 0      }
   ,{
     0, 0, 0, 1, 2, 0, 0, 0, 2, 1, 0, 0, 0, 1    }
   ,{
     4, 3, 4, 3, 4, 3, 4, 3, 4, 0, 0, 3, 4, 3        }
   ,{
     2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 2, 2, 0        }
 };

 byte[][] pos04 = {  
   {
     1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 0        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 4, 0, 0, 0        }
   ,{
     0, 0, 1, 2, 0, 0, 0, 2, 1, 0, 0, 0, 1, 0        }
   ,{
     3, 4, 3, 4, 3, 4, 3, 4, 0, 0, 3, 4, 3, 0        }
   ,{
     1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 2, 2, 0, 0        }
 };

 Level(int tempNumPos, int tempScrollIndex) {
   numPos = tempNumPos;
   scrollIndex = tempScrollIndex;
   scrollX = 48;
   //    grids = new String[numPos];
   //    grids[0] = "pos00";
   //    grids[1] = "pos01";
   //    grids[2] = "pos02";
 }

 void loadMap() {
   tileMap = new Tile[cols][rows];
   for (int i = 0; i < cols; i++) {
     for (int j = 0; j < rows; j++) {
       // Initialize each object
       byte img = posContainer[j][i];
       tileMap[i][j] = new Tile(img, i*48+scrollX, j*48);
       tileMap[i][j].display();
     }
   }
 }

 void scroll() {
   if (keys[L] == true) {
     scrollX -= 1;
   }
   else if (keys[R] == true) {
     scrollX += 1;
   }
   
   if (scrollX >= (48 + scrollIndex*48)) {
     scrollX = 48;
     posContainer = pos01;
   }
   else if (scrollX <= (-48 + -scrollIndex*48)) {
     scrollX = 48;
     posContainer = pos03;
   }
 }
}

class Tile {
 int x, y;
 byte img;
 String tileName; //should be something like "road01.png"
 // Tile Constructor
 Tile(byte tempImg, int tempX, int tempY) {
   img = tempImg;
   x = tempX;
   y = tempY;
 }
 void display() {
   image(tiles[img], x, y);
 }
 //end Tile class
}

Re: loading 2d byte arrays into another array
Reply #1 - Feb 28th, 2009, 8:51am
 
You know you can make 3D (and more) arrays?
Seeing variables named pos00, pos01 etc. makes me perplex, at best... Smiley
Anyway, you can indeed use an indirection array. Then use pos[indir[k]][i] when you need a position.
Page Index Toggle Pages: 1