|
Author |
Topic: When does the keyPressed() function execute? (Read 343 times) |
|
aadigi
|
When does the keyPressed() function execute?
« on: Mar 17th, 2004, 9:26pm » |
|
I've been working on my first sketch playing with particles. The result and source code so far is here http://allwatchedover.com/p5/Imarea/ Basically, it goes through an image file and creates a particle for every pixel that isn't black. I wanted to have the user be able to switch the pictures being used to make the particles in the applet, so I tried implementing a way to load in other pictures by pressing certain keys. Sometimes this works fine, but sometimes it crashes the applet with a NullPointerException inside the for loop that goes through the array containing the particles. I think this probably wouldn't be an issue if I knew that the keyPressed() function executed BEFORE the for loop each frame, but I don't know how to go about making sure this happens. Any suggestions? Also, the applet runs kind of slow with the examples that use more particles. I don't really know about optimizing code, but if there is anything I'm doing that jumps out at anyone as being really inefficient, I'd love to know about it. Thanks. -Aaron
|
|
|
|
der_rabe
|
Re: When does the keyPressed() function execute?
« Reply #1 on: Mar 17th, 2004, 10:24pm » |
|
i cannot see your code correctly
|
Bernd Salewski student of digital media Hochschule Bremen
|
|
|
der_rabe
|
Re: When does the keyPressed() function execute?
« Reply #2 on: Mar 17th, 2004, 10:26pm » |
|
now i can see the code, my browser played me a trick
|
Bernd Salewski student of digital media Hochschule Bremen
|
|
|
fry
|
Re: When does the keyPressed() function execute?
« Reply #3 on: Mar 17th, 2004, 11:53pm » |
|
keyPressed currently gets called as soon as it happens, which is bad, because it might occur in the middle of loop(), causing the sort of trouble you're having. starting with revision 69, keyPressed() will only happen outside of loop(), to avoid the situation. for the time being, if you need the tight synchronization, inside the keyPressed() function, you can set a variable to indicate that a keypress happened, and what the key was, and then you can check that inside of loop() to act on it.
|
|
|
|
fry
|
Re: When does the keyPressed() function execute?
« Reply #4 on: Sep 20th, 2004, 11:52pm » |
|
now fixed.. for megabucket, all mouse and key events are queued until the end of draw, if your piece is looping.
|
|
|
|
|