Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Manipulating non-static variables

So in my class we are making objects move acording to x and y coordinate variables, pretty simple stuff. However, I can only seem to get mine to work if I declare the x and y variables in a seperate static class whereas the teachers example code doesn't seem to do this. I've been looking at the teachers example code, and it makes sense why it works how it does, but when I try to apply similar methods to my code it doesn't seem to work and I can't find out what I'm doing wrong.

Teacher's code:

// A Bug that moves from left to right and vibrates up and down randomly in the output window 
class Bug {
    // The x- and y-coordinates as well as the speed a Bug is moving
    int x;
    int y;
    int speed;
    // The color of the Bug
    int red;
    int green;
    int blue;

    // Initialize the x- and y-coordinates, the speed and the RGB colors
    Bug() {
        x = -80;
        y = (int) random(10, height - 10);
        speed = (int) random(1, 5);
        red = (int) random(255);
        green = (int) random(255);
        blue = (int) random(255);
    }

    // Set the fill color and ellipseMode
    // Create a for loop that draw “bugs” as six consecutive attached circles 
    void displayBug() {
        fill(red, green, blue, 175);
        ellipseMode(CENTER);
        for (int ctr = 0; ctr < 90; ctr += 15) {
            circle(x + ctr, y, 20);
        }
    }

    // Add the speed instance variable to the x-coordinate
    // Add a random value between -2 and 2 to the y-coordinate
    void moveBug() {
        x += speed;
        y += (int) random(-2, 2);
    }

    // Constrain the y-coordinate between 10 and the height of the output window minus 10
    void boundaries() {
        y = constrain(y, 10, height - 10);
    }
    // If the x-coordinate is greater than width of the output window, reset the x-coordinate to 
    -80

    void resetBug() {
        if (x > width) {
            x = -80;
        }
    }
}

Example of me trying to make a circle move without a static class:

class Test{
    int x;
    int y;
    int speed;
    Test(){
        x = 1;
        y =(int) random(0,400);
        speed = 1;
    }
    void drawThing(){
        circle(x,y,10);
    }
    void moveThing(){
        x += speed;
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Here was the code I had in my driver:

Test draw;
Test move;
void setup(){
size(400,400);
draw = new Test();
move = new Test();
}
void draw(){
draw.drawThing();
move.moveThing();
}

but switching out move for move.moveThing with draw.moveThing fixed it.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda