Array inside a function

I'm trying to create a function has an array of images. However, I keep getting an error. Is it possible to put an array inside a function? If so, how do I do it correctly? If not, what is another way to create a function that has images in it?

Answers

  • Would be helpful if you posted some code and told us what error messages you are getting.

  • edited October 2013

    "error message: the method image (PImage,float,float,float,float) in the type PApplet is not applicable for the arguments(PImage[], float,float,int,int)"

    void monument(PImage[] img, float x, float y, int sz) {
    img = new PImage[10];
    for ( int i = 0; i< img.length; i++ ) {
      img[i] = loadImage( i + ".jpg" );
    }
    if (dist(x, y, mouseX, mouseY) < sz/2) {
      if (mousePressed) {
        cursor(MOVE);
        x = lerp(x, mouseX, 0.2);
        y = lerp(y, mouseY, 0.2);
      } else {
        cursor(HAND);
      }
      cursor(ARROW);
    } 
    image(img, x, y, sz, sz);
    

    }

  • Your function above is completely outta touch on how animation is done in Processing! :-??
    There are things which should be done in setup() at the top, and the rest in draw() and event functions! [..]
    Anyways, the error there is about passing an Array w/o specifying which index to use from it.

  • Answer ✓

    As GoToLoop pointed out there are several problems here. This might get you started, but you can find more tutorials on the wiki :

    PImage[] img = new PImage[10];
    int imgIndex = 0;
    
    void setup(){
        size(400,400); // change to size you want
        // Load images here
        for ( int i = 0; i< img.length; i++ ) {
              img[i] = loadImage( i + ".jpg" );
        }
    }
    
    // The draw method is executed ~ 60 times per second
    void draw(){
        background(255); // clear background
        monument(img[imgIndex], 10, 10, 200);
    }
    
    void monument(PImage pimg, float x, float y, int sz) {
        if (dist(x, y, mouseX, mouseY) < sz/2) {
            if (mousePressed) {
                cursor(MOVE);
                x = lerp(x, mouseX, 0.2);
                y = lerp(y, mouseY, 0.2);
            } else {
               cursor(HAND);
            }
        }
        else {
            cursor(ARROW);
        }
        image(pimg, x, y, sz, sz);
    }
    
Sign In or Register to comment.