We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have 8 images that I want to display via a function. Each implementation of the function needs to show a different image. What I cant work out is how to pass the image—for that function implementation—what image it needs to show...
ie.
PImage a, b;
void setup() {
a = loadImage("a.jpg");
b = loadImage("b.jpg");
}
void draw() {
myImage(???????, 40, 40); // to show a
myImage(???????, 80, 80); // to show b
}
void myImage(???????, int x, int y) {
image(???????, x, y);
}
Can anyone help clarify the ??????????'s ?
Thanks in advance.
Answers
sorry about the formatting! not sure how it works here!
you might want to consider a array of PImage
thank you so much, do you have a second to explain 'localImg' in relation to PImage as so I can learn as to how you did this?
the image localImg is known only in the function (it's a local var)
a and b are know in the whole sketch (global var)
the scope is different
am I doing your homework?
anyway, localImg holds a copy of the reference to the memory adress of the image you are passing to it as a parameter.
this also known as "call by reference"
Hence when you change the local var you might change the global as well
(dunno)
no (to the homework query), but you have just been a massive help and I appreciate your knowledge here.
a function has several parts, e.g.
a name,
it does return something (void!!! or an Image) and
it can get something (the parameters). The parameters can be zero (like in
setup()
: empty brackets), one or many. They are always type plus name. Thus you create a new local variable in the function.it has commands between { and }
it is therefore like a factory (leather in, shoes out) or a machine. A sketch are machines working nicely together.
a function can also be seen as a new command:
line()
is an in-build command, but you could write your own commandlineHorizontal(x,y,w);
. Thus you enlarge the processing language.functions are a very good concept to apply because you don't have one endless
draw()
then but single modules. The next step is using classes (OOP, object oriented programming, see tutorials)see
http://portalsfordeveloping.wikia.com/wiki/Functions
thank you, much appreciated
Seriously though, you want an array of PImages. Then you need only pass the index into that array to your function.
Also, you probably want to get rid of your function. It does the same thing as image() itself!
I see this more as a test case
Just an addendum: