How to end animation? (processing) - image

I've written this code which creates a sketchbook.
I'm sure that there's a simple error, but why won't it stop playing at the end of the images?
Here is the code
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer sou; //variable name;
final int NUMBER_IMAGES = 27;
PImage[] images; //sets PImage array
int framerate = 10;
int currentImage = 0;
String getImageName(int image_number) {
if (image_number == 0) { // == double equals checks for equality
return "title.gif"; //missing k0.gif and k26.gif until this line //added
} else if (image_number == 26) {
return "title2.gif";
} else {
return "data/K" + image_number + ".gif";
}
}
void setup () {
minim = new Minim(this); //define construction
sou = minim.loadFile("ambience.mp3");
sou.loop();
size (300, 300);
background (255);
frameRate(framerate);
imageMode (CENTER); // Tells the images to display relative to CENTRE
images = new PImage[NUMBER_IMAGES]; // initialises the array (not images)
for (int image_number = 0; image_number < NUMBER_IMAGES; image_number++) {
String filename; // Declared a String called filename
filename = getImageName(image_number);
images[image_number] = loadImage(filename);
}
}
void draw () {
// Set framerate
frameRate(framerate);
// Draws first image
image(images[currentImage], width/2.0, height/2.0);
currentImage++;
currentImage = currentImage % NUMBER_IMAGES;
}
void keyPressed() {
if (keyCode == UP) { // up arrow increases frame rate by one
framerate ++;
}
if (keyCode == DOWN) { //down arrow decreases framerate by one
framerate --;
}
}
I can't think of more details to add although I'm being told I can't post this as it is mostly code.

This line is the one that achieves the recurrent number loop.
currentImage = currentImage % NUMBER_IMAGES
What the % (Modulo) operator does is to calculates the remainder when one number is divided by another. So lets say for example that your NUMBER_IMAGES is 10, at first you'll have 1 & 10 and the value stored in currentImage would be 1. This continues until you reach 10 % 10 the value stored would be 0 and there you'll start all over again.
Here you can find more about the (Module) in Processing: https://www.processing.org/reference/modulo.html
Perhaps a more simple approach to achieve what you'll looking for would be to add a condition to stop when you reach the number of images.
void draw () {
// Set framerate
frameRate(framerate);
// Draws images
image(images[currentImage], width/2.0, height/2.0);
if(currentImage < NUMBER_IMAGES){
currentImage++;
}
}
Hope this helps.
Regards
Jose

Because you have this line inside your code it will show images forever
currentImage = currentImage % NUMBER_IMAGES
If you want stop drawing nex image simply change this line into something like this:
if(currentImage == NUMBER_IMAGES) noLoop()
noLoop() will stop whole draw() animation so it will display your last image. If you want then exit the animation you can add this to your keyPressed():
if (keyCode == ESC){
exit();
}
exit() will correctly exit your program. You can use this function instead of noLoop to end after last image.

Related

Make image disappear after X seconds (processing)

I'm currently working on a project in which I want an image to pop up after 3 seconds. Once that image has popped up the user has to click on the image to make a "done" image pop up that will disappear automatically after 3 seconds.
I've got most of it working except for the disappearing part. Does anyone know how I can time the image to disappear after 3 seconds?
PImage medic;
PImage medicD;
float time;
float startTime;
final int waitpopup = 3000;
final int DISPLAY_DURATION = 3000;
boolean showimage = true;
boolean showclock = true;
boolean showimagedone = true;
boolean hasClicked;
Clock clock;
void setup (){
size (1080, 1920);
medic = loadImage("medic.png");
medicD = loadImage("medicD.png");
clock = new Clock(width /2, height /2);
time = millis();
}
void draw() {
background (0);
imageMode(CENTER);
if (showclock) clock.display();
if (showimage && millis() - time > waitpopup) {
image(medic, width/2, height/2, 540, 540);
} if (hasClicked == true) {
showimage = false;
image(medicD, width/2, height/2, 540, 540);
} if (millis() > startTime + DISPLAY_DURATION) {
showimagedone = false;
}
}
void mousePressed() {
hasClicked = true;
startTime = time;
}
You can use the millis() function or the frameCount variable to check how much time has gone by, then do something after X seconds or after X frames.
You're already doing some of the work with the showimagedone variable, but you need to use that variable to conditionally draw your image.
I recommend starting with a simpler example and getting that working. Here's one example:
int clickedFrame;
boolean on = false;
int duration = 60;
void draw(){
if(on){
background(255);
if(frameCount > clickedFrame + duration){
on = false;
}
}
else{
background(0);
}
}
void mousePressed(){
clickedFrame = frameCount;
on = true;
}
This code show a white background for one second whenever the user clicks the mouse. You need to do something similar with your images.
Related posts:
How to make a delay in processing project?
How can I draw only every x frames?
Removing element from ArrayList every 500 frames
Timing based events in Processing
How to add +1 to variable every 10 seconds in Processing?
How to create something happen when time = x
making a “poke back” program in processing
Processing: How do i create an object every “x” time
Timer using frameRate and frame counter reliable?
Adding delay in Processing
Please also consult the Processing reference for more information.
If you still can't get it working, please post a MCVE (not your full project!) in a new question and we'll go from there. Good luck.

Toggle function not working in Processing (ControlP5)

I just made my image generator work with PNG files. For now, it's divided into 3 categories (backgrounds, objects & texts). These are now all combined, and with every mouse click it randomises these PNGs.
I made three toggles, where you could to choose to show either the background and the objects on top, all of them, or all separate. Whenever I run the sketch, it shows the "grey" background, but when I use the toggles, it doesn't show anything, or shows a flickering image, where the mouse-click can't be used to go to the next image. I can't seem to find the problem. Hopefully, you can help. :)
import controlP5.*;
boolean showBackground = false;
boolean showObjects = false;
boolean showGrids = false;
ControlP5 cp5;
PImage[] myImageArray = new PImage[8];
PImage[] myImageArray2 = new PImage[15];
PImage[] myImageArray3 = new PImage[15];
void setup() {
size(1436, 847);
background(211, 211, 211);
for (int i=0; i<myImageArray.length; i++) {
myImageArray[i] = loadImage ( "o" + i + ".png");
myImageArray2[i] = loadImage ( "g" + i + ".png");
myImageArray3[i] = loadImage( "b" + i + ".jpg");
cp5 = new ControlP5(this);
// create a toggle and change the default look to a (on/off) switch look
cp5.addToggle("showBackground")
.setPosition(40, 250)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showObjects")
.setPosition(40, 400)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showGrid")
.setPosition(40, 600)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
}
display();
}
void display() {
image(myImageArray3[(int)random(myImageArray.length)], 0, 0, 1436, 847); // b
image(myImageArray2[(int)random(myImageArray.length)], 0, 0, 1436, 847); // g
image(myImageArray[(int)random(myImageArray.length)], 0, 0, 1436, 847); // o
}
void mousePressed() {
display();
}
void draw() {
pushMatrix();
if (showBackground==false) {
image(myImageArray3[(int)random(myImageArray.length)], 0, 0, 1436, 847); // b
} else {
background(211, 211, 211);
}
if (showGrids==false) {
image(myImageArray2[(int)random(myImageArray.length)], 0, 0, 1436, 847); // g
} else {
background(211, 211, 211);
}
if (showObjects==false) {
image(myImageArray[(int)random(myImageArray.length)], 0, 0, 1436, 847); // o
} else {
background(211, 211, 211);
}
popMatrix();
}
Here are a couple of things where the logic your wrote in your code might not match what you had in mind:
When you call display() on mouse it renders those 3 images once (also it will be different images within those since it's using a randomised index). Similarly in draw(), when an does get picked to be rendered, frames will be flickering fast as a random index is generated multiple times per second(each frame). You may want to randomise indices in a different event (e.g. mouse or key press) and store this value in a variable you can re-use.
the conditions you use in draw(): you probably meant to check if the values are true(toggled enabled/turned on in controlP5) ? (e.g. e.g. if (showBackground==true) and initialise the toggles with false, instead of true?)
a big one: in draw() , after each condition(showBackground,showGrids,showObjects), if it's false, you're clearing the the whole frame (so a previous image would be erased)
you have 3 arrays, but you use the size of the first(myImageArray.length) only, which means, even though you may have more images for myImageArray2 and myImageArray3, you're not loading, nor displaying them.
The third grid is labeled "showGrid" when it should be "showGrids": if you aren't consistent with the toggle labels and variable names, toggles won't update the variable names.
you should use more descriptive names for the arrays: it will make it easier to scan/follow your code on the long run.
there's no need to add toggles multiple times in the for loop where you load images: once will do.
Here's what I mean:
import controlP5.*;
boolean showBackground = false;
boolean showObjects = false;
boolean showGrids = false;
ControlP5 cp5;
PImage[] objects = new PImage[8];
PImage[] grids = new PImage[15];
PImage[] backgrounds = new PImage[15];
int currentImage = 0;
void setup() {
size(1436, 847);
//load objects
for (int i=0; i<objects.length; i++) {
objects[i] = loadImage ( "o" + i + ".png");
}
//load grids
for(int i = 0 ; i < grids.length; i++){
grids[i] = loadImage ( "g" + i + ".png");
}
//load backgrounds
for(int i = 0 ; i < grids.length; i++){
backgrounds[i] = loadImage( "b" + i + ".jpg");
}
//setup UI
cp5 = new ControlP5(this);
// create a toggle and change the default look to a (on/off) switch look
cp5.addToggle("showBackground")
.setPosition(40, 250)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showObjects")
.setPosition(40, 400)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showGrids")
.setPosition(40, 600)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
}
void mousePressed() {
//go to next image index
currentImage = currentImage + 1;
//check if the incremented index is still valid, otherwise, reset it to 0 (so it doesn't go out of bounds)
if (currentImage >= objects.length) {
currentImage = 0;
}
}
void draw() {
//clear current frame
background(211);//for gray scale value you can just use one value: the brightness level :)
if (showBackground==true) {
image(backgrounds[currentImage], 0, 0, 1436, 847); // b
}
if (showGrids==true) {
image(grids[currentImage], 0, 0, 1436, 847); // g
}
if (showObjects==true) {
image(objects[currentImage], 0, 0, 1436, 847); // o
}
}
Note that currently the same index is used for all 3 arrays.
You may want to add a separate index variable for each array (e.g. currentObjectIndex, currentBackgroundIndex, currentGridIndex) that you can increment independently of each other.
I recommend having a bit more patience and double checking your code first.
Visualise what each line of code will do, then check if it actually does what you expect it to do. Either you will learn something new or improve your logic.
Also, if mentally joggling 3 arrays is tricky (and it can be), go one step back: try your logic with one array only until you get the hang of it, then move on.
A step backwards is sometimes a step forward when you're going in the wrong direction.
As creative as you'd like to be with Processing, bare in mind, the interface to plugging your ideas to it is still a series of one instruction at a time, each precisely tuned to do exactly what you want it to do. There's room for fun, but unfortunately you need to get past the boring parts first

In UnityScript, why does GUI not get called within hit on collider?

I'm trying to print out text when a user touches a sprite, however, even though I get no errors when building and running the code, the code refuses to printout the text and I cannot understand what I am doing wrong. Please help me understand why it does not print.
It prints the debug text when I touch the sprite, but not the GUI text with total.
This is the code:
#pragma strict
function Start () {
OnGUI();
}
function OnGUI(){
//var total = 0;
//GUI.Label( myRect, total.ToString() ); // Displays "10".
//GUI.Label( myRect, "" + bullets ); // Displays "10".
// GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:"+total);
}
//function Update () {
//}
var platform : RuntimePlatform = Application.platform;
function Update(){
if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
if(Input.touchCount > 0) {
if(Input.GetTouch(0).phase == TouchPhase.Began){
checkTouch(Input.GetTouch(0).position);
}
}
}else if(platform == RuntimePlatform.WindowsEditor){
if(Input.GetMouseButtonDown(0)) {
checkTouch(Input.mousePosition);
}
}
}
function checkTouch(pos){
var total = 0;
var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
var touchPos : Vector2 = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if(hit){
GUI.Label(Rect(0,0,Screen.width,Screen.height),"Total:");
Debug.Log(hit.transform.gameObject.name);
hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
//total = total +1;
}
}
You cannot put a gui item outside the OnGUI Method. You need to set a boolean to turn on the label.
Something along these lines.
OnGUI(){
if(labelbool)
GUI.Label ....
}
check(){
labelbool = true;
}

Processing calls void setup() twice, and I need a work around

I'm working on a program that reads in some images (.jpg) and text from source files and assembling them into a single PDF. I know processing probably isn't the best language to do it in, but its the only one I know how to do it in. Anyway, I am having an issue where processing calls setup two times. I've seen that this issue is resolved when size() is the first line within setup, however I can't have that happen, because I have to read in and store all my data, find the width of the widest image, then make sure its tall enough to accommodate pages with more than one image, and add text before I can decide on how wide and tall my window is. I am looking for suggestions as to how I might structure the code so that I can get all my information without having to call setup twice, because that's causing my PDF to contain two copies of all the data. I've included setup if it helps anyone. Thanks!
void setup(){
font = loadFont("TimesNewRomanPSMT-20.vlw");
File clientsFolder = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients");
clients = clientsFolder.listFiles();
for(File x : clients){
println(x.getName());
}
//test files to see if they end in .txt, and have a matching .pdf extension that is newer
String nextClient = needPdf();
File nextClientData = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients/" + nextClient);
//println(nextClientData.getName());
//open the file for reading
//setup can't throw the exception, and it needs it, so this should take care of it
try{
Scanner scan = new Scanner(nextClientData);
while(scan.hasNextLine() ){
exercises.add(scan.nextLine());
}
//println(exercises.toString());
printedData = new Exercise[exercises.size()];
println(exercises.size());
for(int i = 0; i < exercises.size(); i++){
printedData[i] = new Exercise((String)exercises.get(i));
}
//count the width and height
int w = 0, h = 0;
for(Exercise e: printedData){
if(e.getWidest() > w){
w = e.getWidest();
}
if(e.getTallest() > h){
h = e.getHeight();
}
}
//and finally we can create the freaking window
// this cuts the .txt off
size(w, h, PDF, "C:/Users/[my name]/Desktop/" + nextClient.substring(0, nextClient.length() - 4) + ".pdf");
}catch (FileNotFoundException e){
println("Unknown error in PApplet.setup(). Exiting.");
println(e.getMessage() );
exit();
}
}
How about moving all these functions to be done before setup()? Although processing usually complains that you are "mixing static and active modes", this hack seems to work at processing 2.0.1:
int i = beforeSetup();
int szX,szY;
int beforeSetup() {
println("look! I am happening before setup()!!");
szX = 800;
szY = 600;
return 0;
}
void setup() {
size(szX,szY);
println("awww");
}
You are essentially calling a function to fill int i just as a hack to run all the functions you want, thus having to compute whatever you want before having to set the window size.
perhaps you can resize your window after calcs are done? Once I made this sketch to see how resizing would work, it is expecting an image file, see if it can help you...
//no error handling for non image files!
PImage img;
int newCanvasWidth = MIN_WINDOW_WIDTH; // made global to use in draw
int newCanvasHeight = MIN_WINDOW_HEIGHT;
java.awt.Insets insets; //"An Insets object is a representation of the borders of a container"
//from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
void setup()
{
size(200, 200); // always first line
frame.pack(); insets = frame.getInsets();
frame.setResizable(true);
/// for debuging, system depende`nt, at least screen is...
print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
print(" MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
print(" screenWidth = " + displayWidth);
println(" screenHeight = " + displayHeight);
}
void draw()
{
if (img != null)
{
image(img, 0, 0, newCanvasWidth, newCanvasHeight);
}
}
void getImageAndResize(File selected)
{
String path = selected.getAbsolutePath();
if (path == null)
{
println ("nono :-|");
}
else
{
img = loadImage(path);
// a temp variable for readability
int widthInsets =insets.left + insets.right;
int heightInsets =insets.top + insets.bottom;
// constrain values between screen size and minimum window size
int newFrameWidth = constrain(img.width + widthInsets, MIN_WINDOW_WIDTH, displayWidth);
int newFrameHeight = constrain(img.height + heightInsets, MIN_WINDOW_HEIGHT, displayHeight -20);
// Canvas should consider insets for constraining? I think so...
newCanvasWidth = constrain(img.width, MIN_WINDOW_WIDTH - widthInsets, displayWidth - widthInsets);
newCanvasHeight = constrain(img.height, MIN_WINDOW_HEIGHT - heightInsets, displayHeight -20 - heightInsets);
// set canvas size to img size WITHOUT INSETS
setSize(newCanvasWidth, newCanvasHeight);
// set frame size to image + Insets size
frame.setSize(newFrameWidth, newFrameHeight);
//// for debuging
println(path);
println(" ");
print("imgW = " + img.width);
println(" imgH = " + img.height);
print("width+ins = " + widthInsets);
println(" height+ins = " + heightInsets);
print("nFrameW = " + newFrameWidth);
println(" nFrameH = " + newFrameHeight);
print("nCanvasw = " + newCanvasWidth);
println(" nCanvsH = " + newCanvasHeight);
println(" ------ ");
}
}
void mouseClicked()
{
img = null;
selectInput("select an image", "getImageAndResize" );
}

xna 4.0 animation looping

Hello everyone I have been following this tutorial here http://www.gogo-robot.com/2011/05/30/xna-skinned-model-animations/ and so far its great got the animations playing and everything, but now I want to expand it and stop the continuous loops say for instance i press the a key to make the model jump when i release the a key i want him to stop jumping but if i hold the a key i want him to keep jumping. Here what i have tried so far
and none of it works.
I am stumped here on how to do this thanks for any help with this.
private void HandleInput(GameTime gameTime)
{
currentGamePadState = GamePad.GetState(PlayerIndex.One);
// Check for changing anims
//SkinningData skinningData = model.Tag as SkinningData;
SkinningData sd = jumper.model.Tag as SkinningData;
if (currentGamePadState.Buttons.A == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "Fire")
jumper.animationPlayer.StartClip(sd.AnimationClips["Fire"]);
}
if (currentGamePadState.Buttons.X == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "DieF")
jumper.animationPlayer.StartClip(sd.AnimationClips["DieF"]);
}
//does not work
if (currentGamePadState.Buttons.X == ButtonState.Released)
{
if (jumper.animationPlayer.CurrentClip.Name == "DieF")
jumper.animationPlayer.StartClip(sd.AnimationClips["Idel"]);
}
if (currentGamePadState.Buttons.Y == ButtonState.Pressed)
{
if (jumper.animationPlayer.CurrentClip.Name != "Idel")
jumper.animationPlayer.StartClip(sd.AnimationClips["Idle"]);
}
//does not work
if (jumper.animationPlayer.CurrentTime == jumper.animationPlayer.CurrentClip.Duration)
{
//set him back to idel
jumper.animationPlayer.StartClip(sd.AnimationClips["Idle"]);
}
I have tried these configuration with no luck in the game
// Starts playing the entirety of the given clip
public void StartClip(string clip, bool loop)
{
AnimationClip clipVal = skinningData.AnimationClips[clip];
StartClip(clip, TimeSpan.FromSeconds(0), clipVal.Duration, loop);
}
// Plays a specific portion of the given clip, from one frame
// index to another
public void StartClip(string clip, int startFrame, int endFrame, bool loop)
{
AnimationClip clipVal = skinningData.AnimationClips[clip];
StartClip(clip, clipVal.Keyframes[startFrame].Time,
clipVal.Keyframes[endFrame].Time, loop);
}
// Plays a specific portion of the given clip, from one time
// to another
public void StartClip(string clip, TimeSpan StartTime, TimeSpan EndTime, bool loop)
{
CurrentClip = skinningData.AnimationClips[clip];
currentTime = TimeSpan.FromSeconds(0);
currentKeyframe = 0;
Done = false;
this.startTime = StartTime;
this.endTime = EndTime;
this.loop = loop;
// Copy the bind pose to the bone transforms array to reset the animation
skinningData.BindPose.CopyTo(BoneTransforms, 0);
}
Can you not attach a bool on the animation clip to tell it to play only once, or an active variable that can be called.

Resources