The field component is not visible.
in
Programming Questions
•
6 months ago
Hello ! I've got 2 classes and one is using a variable of the other so I got a "The field component is not visible." I know very well why this is hapenning so i'm looking for a way to make a global variable.
here is the code is you want to read it.
here is the code is you want to read it.
import java.util.List; // actually, it is already imported, but not "activated"!
// Constant field variables of the top class, the sketch itself! ;-)
// an ArrayList which only stores instances of class Balle, and initial size = 128:
final static List<Balle> balles = new ArrayList(0200);
final static List<Tour> tours = new ArrayList(0200);
void setup() {
size(600, 400);
}
void draw() {
background(0);
size(600, 400);
// this is called enhanced or for-each or even for-in loop
// iterates all of the elements stored within variable balles
// since balles only contains instances of class Balle
// iterator b is declared as of type Balle to receive an element from it:
for (Balle b: balles) b.script();
for (Tour t: tours) t.tour_script();
}
void mousePressed() {
// if it's NOT a middle-cli ck, add a new instance of Balle:
switch(mouseButton) {
case CENTER :
if (balles.size() != 0) balles.remove(0);
break;
case LEFT :
balles.add( new Balle() );
break;
case RIGHT :
tours.add( new Tour() );
}
}
// classe balle ====================================================
public final class Balle {
// fields of inner class Balle (instantiatable):
public int x, y;
byte spX, spY;
int pdv;
// static constant fields of inner class Balle (non-instantiatable):
final static byte taille = 20, rayon = taille/2; //
Balle() { // constructor of inner class Balle
x = (short) mouseX;
y = (short) mouseY;
pdv = 30;
// ternary operator ?: which demands 3 operands
// if (random(2) < 1) multiplies by -1, otherwise by 1:
spX = 3;
spY = 3;
}
void bump() {
// multiplies spX/spY by -1 in case x/y is (off canvas - size/2 of Balle)
// afterwards, adds current result of spX/spY into x/y:
if ( x < rayon) {
spX *= -1;
}
if (x> width-rayon) {
spX *= -1;
}
if ( y <rayon) {
spY *= -1;
}
if (y > height-rayon){
spY *= -1;
}
x += spX;
y += spY;
}
void show() {
ellipse(x, y,taille,taille);
}
void script() {
bump();
show();
}
}
final class Tour{
int a,b; // pos tour
int aire; //cercle de zone
int m,n; //pos tir
int v;//vitesse du tir
int zone; //variable dite "soldat est dans la zone"
int dmg; // dommages de la tour
Tour() {
dmg = 5;
a = mouseX;
b= mouseY;
v=0;
m=a;
n=b;
zone = 0;
aire = 100;
}
void tour_tir(){//v la vitesse de tir
if (zone == 1){
v = 10;
} if (zone == 0){
v = 0;
m = m + v;
} if (m > x){
m = m - v;
} if (n < y ){
n = n + v;
} if (n>y){
n = n - v;
} if ( m <= x+v && m >= x -v && n <= y+v && n>= y-v){
m = a;
n = b;
pdv -= dmg;
}
}
void soldat_mort(){
if (pdv < 0){
if (balles.size() != 0) balles.remove(0);
}
}
void tour_script() {
soldat_mort();
smooth();
tour_zone();
tour_tir();
ellipse(a,b,aire,aire);
ellipse(m,n,10,10);
}
}
1