"dvd cannot be resolved to a variable"

I’ve got a different problem while trying to add a sprite and it says "dvd cannot be resolved to a variable

float x = random(200,300);   // x location of square
float y = random(200,300);     // y location of square
float xspeed = 1;   // speed of square
float yspeed = 1;   // speed of square


void setup() {
  size(800, 600);
frameRate(240);
//surface.setResizable(true);
PImage dvd;          /// dvd
dvd = loadImage("dvd.png"); /// dvd

}

void draw() {
  background(255);

fill(0,0,0);
rect(0,0,10000,-1);


  // Display the square
  fill(dvd); /// highighted line
  stroke(0);
  rect(x, y, -200, -150);

  // Add speed to location.
  y = y + yspeed;
  x = x + xspeed;
  // bounce
  if(x > width){
    xspeed = xspeed * -1;
  }
  if(y > height){
    yspeed = yspeed * -1;
  }
    if(x < 200){
    xspeed = xspeed * -1;
  }
  if(y < 150){
    yspeed = yspeed * -1;
  }
if (keyPressed) {
    if (key == 'b' || key == 'B') {
      xspeed = xspeed - 0.1f;
      yspeed = yspeed - 0.1f;
    }
  }

for variable declaration,
loading images
and show them in draw pls use like:

PImage img;

void setup() {
  size(640, 360);
  img = loadImage("data/moonwalk.jpg");
}

void draw() {
  image(img, 0, 0);
}

did you check ?
https://processing.org/reference/PImage.html

2 Likes

Just to expand on the above point by kll – if you declare a variable (PImage dvd) in setup(), then its scope means it is only available in setup, and you get an error when you try to access it elsewhere.

If instead you declare it globally 00 at the top of your sketch – then you can assign it in setup and it is still available globally throughout your sketch. For example, you can define it in setup() and use it in draw().

1 Like