We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I have an assignment where I have to draw any object and then if you press a number key (from 1 to 9) that object will appear the number of times as the number pressed (in random places). Can someone help me with what I would put in this keyPressed function. (Function1 is my drawing)
Answers
This forum generally only helps those that have made an attempt at the program and are stuck with their current method. Do you have any code where you attempted to do this?
Are you defining pigFunction() inside mousePressed()? It is hard to tell.
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Have you looked into using
setup()
anddraw()
?use int(key) to parse your key into a number. Currently, you are concatenating a string to a number. That said, you don't need to add one to it (on line 16). Also, don't call keyPressed in your draw. It's a callback function that is called automatically if you put it there. I would also move the def pigFunction outside of your keyPressed and put it right after the setup. Keep in mind that the pressed function will be called repeatedly while the key is pressed. You could take care of that by implementing a "debounce" or just fill the background first thing inside your if-statement (at line 16, before the loop).