What is a simple way to put something in void draw but only play it once (although it keeps updating?) In my code, I want to play a video but the video can be switched to a different video. The problem with this is that every time the code checks if it needs to play the new video in void draw, it resets the video, therefore never playing the video.
Here is my code
//Make movie variables
//Load every movie
//If todraw is a variable load that movie
import processing.video.*;
int up = 80;
int right = 7;
int toDraw = 1;
// Step 1. Declare a Movie object.
Movie movie;
Movie movie1;
Movie movie2;
Movie movie3;
import processing.sound.*;
SoundFile file;
SoundFile file1;
SoundFile file2;
SoundFile file3;
void setup()
{
file1 = new SoundFile(this, "music1.aiff");
movie1 = new Movie(this, "movie1.mov");
file2 = new SoundFile(this, "music2.aiff");
movie2 = new Movie(this, "movie2.mov");
file3 = new SoundFile(this, "music3.aiff");
movie3 = new Movie(this, "movie3.mov");
movie = movie1;
file = file1;
noCursor();
size(636, 476);
println("IMPORTANT: READ BELOW");
println("Right and left increase and decrease the brightness of the flashlight");
println("Up and down increase and decrease the size of the flashlight");
println("And don't make either of them a negative number");
}
// Step 4. Read new frames from the movie.
void movieEvent(Movie movie)
{
movie.read();
movie1.read();
movie2.read();
movie3.read();
}
// Step 5. Display movie.
void draw()
{
if (toDraw == 1)
{
movie=movie1;
file=file1;
} else if (toDraw == 2)
{
movie=movie2;
file=file2;
} else if (toDraw == 3)
{
movie=movie3;
file=file3;
}
file.loop();
loadPixels();
movie.loadPixels();
for (int x = 0; x < movie.width; x++)
{
for (int y = 0; y < movie.height; y++)
{
// Calculate the 1D location from a 2D grid
int loc = x + y * movie.width;
// Get the red, green, blue values from a pixel
float r = red (movie.pixels[loc]);
float g = green(movie.pixels[loc]);
float b = blue (movie.pixels[loc]);
// Calculate an amount to change brightness based on proximity to the mouse
float d = dist(x, y, mouseX, mouseY);
float adjustbrightness = map(d, 0, up, right, 0);
r *= adjustbrightness;
g *= adjustbrightness;
b *= adjustbrightness;
// Constrain RGB to make sure they are within 0-255 color range
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
// Make a new color and set pixel in the window
color c = color(r, g, b);
pixels[loc] = c;
}
}
updatePixels();
}
void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP)
{
up=up+5;
}
if (keyCode == DOWN)
{
up=up-5;
}
if (keyCode == RIGHT)
{
right=right+1;
}
if (keyCode == LEFT)
{
right=right-1;
}
if (key == '1')
{
toDraw = 1;
}
if (key == '2')
{
toDraw = 2;
}
if (key == 3)
{
toDraw = 3;
}
}
}
It looks like you want to load the videos in keyPressed()
instead of draw()
?