Is it possible to pass PImage as a parameter?

Is it possible to pass PImage as a parameter to ? Than just use a boolean when I want to load or reload it.

ex: void name(PImage something){}

Answers

  • edited November 2016

    How i cant seem to make it work?

  • Yes.

    In fact, notice that in the PImage example, you are passing a PImage named photo as a parameter to the image() function.

    void image(PImage img, float x, float y){}
    
  • edited November 2016

    this is what i tried. it said the float did not work

    void setup(){
    size(600,600);
    }
    
    void draw(){
    bob("https://processing.org/reference/images/PImage.png",0,0);
    }
    
    void bob(PImage img, int x, int y){
    img = loadImage(img);
    image(img, x, y);
    }
    
  • PImage img; 
    
    void setup() { 
      size(600, 600);
    
      img = loadImage("https://processing.org/reference/images/PImage.png");
    }
    
    void draw() { 
      bob(img, 0, 0);
    }
    
    void bob(PImage img, int x, int y) { 
      image(img, x, y);
    }
    
  • i am trying to load what i put into the function and move away from putting things in setup

  • setup() is the correct place to load any resources the sketch's gonna need.
    Of course you can create separate functions to deal w/ loading stuff, but they still should be called from within setup().

    Also notice loadImage() only accepts String arguments:
    https://Processing.org/reference/loadImage_.html

  • You can load a lot into an array

    Then we can just use the index of the array

  • There are tons of reasons to pass around PImages and encapsulate what you do with them in functions. But sometimes, you want to call those functions from setup.

    In general: "NEVER load images from draw"

  • You can of course call bob from draw() but if you do, remember that draw runs 50 times per second.

    So once you've called bob, set a boolean, that it's loaded and we don't want to load it again (check the boolean with if in bob)

    Also, you have to pass a string to bob when you want to use it in bob with loadImage

    so String imgName

    and PImage img are different things in bob

    bob could also return a value img to draw to let the img -- to let img be of global scope

    But all this makes sense when you tell us what you are up to

    I also noticed that a lot of questions from you didn't get an answer, so shame on us. PM me when you need more

Sign In or Register to comment.