Random video generated for every mouse click

Hi,

I am trying to get a random video to play from an array but a selected number only for when I press the left or right click. For this I created two seperate variables to generate a random number. Every time I click left it seems to be working yet it is glitching. I don’t think I am going about it the right way either…I can’t quite understand what I am doing wrong.

Any help will be appreciated.

import processing.video.*;

Movie myMovie3;
Movie[] mov = new Movie [8];


final static int IDLE = 1;
final static int ANIM1 = 2;
final static int ANIM2 = 3;
int state = 1;
int count = 0;
int rIndex; //variable for random R poster to display - 6 videos 
int gIndex; //variable for random G poster to display - only two videos 


void setup() {
  size(405, 720);

  for (int i = 0; i < mov.length; i ++) {
    mov[i] = new Movie(this, "mov"+i+".mp4");
  }

  myMovie3 = new Movie(this, "background.mov"); // background video

  myMovie3.loop();
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  background(0);
  rIndex = int(random(2, 8));
  gIndex = int(random(0, 2));
  if (state == IDLE) {
    image(myMovie3, 0, 0, width, height);
    textSize(26);
    text("Count Variable: " + count, 130, 50);
  } else if (state == ANIM1) {
    for (int i = 0; i < mov.length; i++) {
      image(mov[rIndex], 0, 0, width, height);
      if (mov[rIndex].time() >= 6.0) state = IDLE;
    }
  } else if (state == ANIM2) {
    for (int i = 0; i < mov.length; i++) {
      image(mov[gIndex], 0, 0, width, height);
      if (mov[gIndex].time() >= 10.0) state = IDLE;
    }
  }
}

void mousePressed() {
  if (mouseButton == LEFT) { 
    state = ANIM1;
    mov[rIndex].jump(0.0);
    mov[rIndex].play();

    count = count + 5;
  } else if (mouseButton == RIGHT) {
    state = ANIM2;
    mov[gIndex].jump(0.0);
    mov[gIndex].play();
    count -= 3;
  } else state = IDLE;
}

Thanks in advance!

2 Likes

-a- please format your code above,
use the

</> button from the editor menu

looks like
```
type or paste code here
```


on the first look i would say that a random movie to play
with the random number made in ( every / 60 per sec ) draw loop
your computer / OS will refuse…

move the index random generation to beginning of
? mouse click ?

Hi,
Sorry I for got about formatting it. Thanks for the help. Its working now! quick question though

for this line of code

rIndex = int(random(2, 8));
gIndex = int(random(0, 2));

because my videos are stored in an array for rIndex when I say generate a random number from 2,8 does this mean that in my array if the number generated was 3 then the whatever video at 3 will be played? and also because my array has 8 items, if I set it to 8 it will mean I will not get 8 as 8 will be exclusive for the random function?

thanks!

general idea DEBUG easy way
println("rIndex "+rIndex+" gIndex "+gIndex);
if you would have tested that inside draw of your first code, you might have seen how you torture the OS


(int)random(2,8)

2 3 4 5 6 7 NOT 8

ok I was just confirming. So if you notice that my Movie array has 8 and so for the rIndex I want 8 to be inclusive but what happens is when I write

rIndex = int(random(2, 9));

I get the ArrayIndexOutOfBoundsException:8 error…

?? array[8] goes from 0,1,2,3,4,5,6,7
so your code was GOOD,

Hi yeah I forgot it started from 0, :sweat_smile: Thanks heaps! but I changed it to being

rIndex = int(random(2, mov.length));
1 Like