Need help for not initializing a Variable Error
in
Programming Questions
•
1 year ago
Can somebody help me with this error:
It sais that my local variable dirX has not been initialized (probably also dirY)
My idea is that the single life obj (pixels) should come together.
Of course im making the code more complex later.
Sry for using arrays, but i really like this old school style ^^
So on its fun at coding it and i dont want to give up by this error. Thx for helping (and or reading =D)
- int xA=20;
- int yA=20;
- int[][] x;
- int[][] y;
- life lif[][];
- void setup()
- {x=new int[xA][yA];
- y=new int[xA][yA];
- lif=new life[xA][yA];
- size(500,500);
- smooth();
- ellipseMode(CENTER);
- for(int i=0;i<xA;i++)
- {
- for(int j=0;j<yA;j++){
- lif[i][j]=new life((i*5)+int(random(5,10)),(j*5)+int(random(5,10)),i,j);
- }}
- }
- void draw()
- {background(0);
- for(int i=0;i<xA;i++)
- {
- for(int j=0;j<yA;j++){
- lif[i][j].display();
- }}
- }
- class life{
- int sX;
- int sY;
- life(int curx, int cury, int cursX, int cursY)
- {
- sX=cursX;
- sY=cursY;
- x[sX][sY]=curx;
- y[sX][sY]=cury;
- }
- void display(){
- int dirX;
- int dirY;
- int Small=1000000;
- ellipse(x[sX][sY],y[sX][sY],2,2);
- for(int i=0;i<xA;i++)
- {
- for(int j=0;j<yA;j++){
- if(x[sX][sY]<x[i][j])
- {if(y[sX][sY]<y[i][j])
- {if((x[i][j]-x[sX][sY])+(y[i][j]-y[sX][sY])<Small){Small=(x[i][j]-x[sX][sY])+(y[i][j]-y[sX][sY]);
- dirX=i;
- dirY=j;}}
- if(y[sX][sY]>y[i][j])
- {if((x[i][j]-x[sX][sY])+(y[sX][sY]-y[i][j])<Small){Small=(x[i][j]-x[sX][sY])+(y[sX][sY]-y[i][j]);
- dirX=i;
- dirY=j;}}
- if(y[sX][sY]==y[i][j])
- {if((x[i][j]-x[sX][sY])<Small){Small=(x[i][j]-x[sX][sY]);
- dirX=i;
- dirY=j;}}
- }
- else if(x[sX][sY]==x[i][j])
- {if(y[sX][sY]<y[i][j])
- {if((y[i][j]-y[sX][sY])<Small){Small=(y[i][j]-y[sX][sY]);
- dirX=i;
- dirY=j;}}
- if(y[sX][sY]>y[i][j])
- {if((y[sX][sY]-y[i][j])<Small){Small=(y[sX][sY]-y[i][j]);
- dirX=i;
- dirY=j;}
- }
- if(y[sX][sY]==y[i][j])
- {if((x[i][j]-x[sX][sY])<Small){Small=(x[i][j]-x[sX][sY]);
- dirX=i;
- dirY=j;}}
- }
- else{
- if(y[sX][sY]<y[i][j])
- {if((x[sX][sY]-x[i][j])+(y[i][j]-y[sX][sY])<Small){Small=(x[sX][sY]-x[i][j])+(y[i][j]-y[sX][sY]);
- dirX=i;
- dirY=j;}}
- if(y[sX][sY]>y[i][j])
- {if((x[sX][sY]-x[i][j])+(y[sX][sY]-y[i][j])<Small){Small=(x[sX][sY]-x[i][j])+(y[sX][sY]-y[i][j]);
- dirX=i;
- dirY=j;}}
- if(y[sX][sY]==y[i][j])
- {if((x[sX][sY]-x[i][j])<Small){Small=(x[sX][sY]-x[i][j]);
- dirX=i;
- dirY=j;}}
- }
- }}
- if(dirX<x[sX][sY])
- {x[sX][sY]--;}
- else{x[sX][sY]++;}
- if(dirY<y[sX][sY])
- {y[sX][sY]--;}
- else{y[sX][sY]++;}
- }
- }
1