We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've created a class which contains an image:
If I include the loadimage in the display function, it works:
class Hand {
int x;
int y;
PImage thehand;
Hand() {
x=100;
y=100;
}
void display(){
thehand=loadImage("hand.png");
image(thehand,x,y);
}
}
But that's wrong, since the loadimage keeps happening with each cycle through display (as opposed to once when the object is created.)
Putting: thehand=loadImage("hand.png") in
Hand() {
x=100;
y=100;
thehand=loadImage("hand.png")
}
leads to an error that the image can't be found with the line: "image(thehand,x,y)" highlighted.
What am I missing?
Answers
Loading images is a very slow process! 8-} Best place to do is within setup()! ;))
This way, we only display() it within draw().
Check it out how I've tweaked your class: (~~)
This is great and it works. Thanks. Just wondering for future reference. Can you load the image within the object. I'm pretty sure I've seen examples that do this. Or is this just not how it works.
You mean, within the class itself? If so, just put the loadImage() function inside the class' constructor! (*)
Got it. The key is that the file is called in the main program. Is that it?
Also, what does "final" do in line 17. It does work without it.
For the program as a whole, that
final
there does nothing! :-\"It's just a remark for any1 trying to debug the code not to worry about variable theHand changing its stored value once set. :D
More specifically, before program reaches draw()! B/c that's by default invoked at 60 FPS! $-)