Using random() in draw()
in
Programming Questions
•
1 years ago
I have a challenge, to drop images from various positions in my Program, Im using random() to randomly generate numbers for the x- axis of my Image Location, the Problem is that since my method is called in the draw() function, it loops continuously generating random numbers each time. This makes the circle "shake" when it's droping. This is what i have to far
- int z = 10;
- int timedelay,time;
- void dropBall() {
- image(ball, generateRandom(), z);
- z += speed * direction;//speed and direction are earlier declared variables
- }
- public float generateRandom(){
- float nextX;
- if (z > height + radius) { //height = height of the Screen, radius = radius of image
- nextX = random(screenWidth);
- }
- else{
- nextX = 100;
- }
- return nextX;
- }
- void setup(){
- ...
- timeDelay = 1000;
- time = millis();
- }
- void draw(){
- background(204);
- timer.showTimer(this,600,40);
- stroke(0);//Bottom Stroke
- ...
- if (millis() > time + timeDelay) {
- dropBall();
- if (z > height + radius) {
- z = 0;
- time = millis();
- }
- }
- }
1