Loading...
Logo
Processing Forum

Need Help I'm new.

in Programming Questions  •  6 months ago  
Hi I'm new to processing and programming. Now I have several images that I want to be able to click and cycle though them. 1st I tried the 
      image(name, x,y);
      if(mousePressed){
       image(name2,x,y)


So, this one works but once I release my mouse it takes me back to the 1st image. I then tried the background(); giving me the same problem. 

Thanks in advanced.

Replies(8)

Re: Need Help I'm new.

6 months ago
  • 1st of all, you're gonna need to use an Array[] to store all instances of your loaded PImages.
  • next, have a field variable to store current PImage index for that Array.
  • implement a mousePressed() event function to increase that index or go back to 0.
  • within draw(), use image() to display that Array variable w/ current index variable.

Re: Need Help I'm new.

6 months ago
thanks I will try that out. :) 

thanks again.


It worked!! Now the clicks make it switch too fast I have to figure out how to slow it down. Also I get an ArrayIndexOutOfBoundsException:14 error. I think it should go around once because the memory is being used up.

Re: Need Help I'm new.

6 months ago

Now the clicks make it switch too fast
maybe just use mouseReleased instead of mousePressed()


Also I get an ArrayIndexOutOfBoundsException:14 error.
I assume you got 13 images.
When you increase the index you have to check if it's bigger than the array size.
If so, set index to zero or set it to array size-1.
(when you decrease the index you need to check for zero too)

Greetings, Chrisir   









I was suppose to actually not put the mousePressed function inside the draw function. 


here's why, the draw function will draw everything 60 frames per second. So, when you think you click fast its actually processing the images very fast. 

it should be 
int x = 0;
void draw(){
      image(name[x], x,y);

}

void mousePressed(){
x++;

}

This will cycle the images properly per click. now I should add an if and else if statement to check that it doesn't go beyond the image.length of the array. 

Thanks to the both of you for you help :)

Re: Need Help I'm new.

6 months ago

implement a mousePressed() event function to increase that index or go back to 0.
That was my 3rd tip! 

Here're 2 algorithm choices to guarantee index won't extrapolate its limit:

void MousePressed() {
  if (++idx == name.length) idx = 0;
}

void MousePressed() {
  idx = (idx + 1) % name.length;
}


Re: Need Help I'm new.

6 months ago
Wow that is way more simple, than the way I had coded it. Here is what I had. 

Copy code
  1. PImage thor1,thor2,thor3,thor4,thor5,thor6,thor7,thor8,thor9,thor10,thor11,thor12,thor13,thor14;

  2. PImage [] images = new PImage[14];
  3. int x = 0;
  4. void setup() {
  5.   thor1 = loadImage("thor.gif");thor2=loadImage("thor2.gif");thor3=loadImage("thor3.gif");thor4=loadImage("thor4.gif");thor5=loadImage("thor5.gif");thor6=loadImage("thor6.gif");thor7=loadImage("thor7.gif");thor8=loadImage("thor8.gif");thor9=loadImage("thor9.gif");thor10=loadImage("thor10.gif");thor11=loadImage("thor11.gif");thor12=loadImage("thor12.gif");thor13=loadImage("thor13.gif"); thor14=loadImage("thor14.gif");  
  6.   size(600,400);

  7.   images[0]=thor1;
  8.   images[1]=thor2;
  9.   images[2]=thor3;
  10.   images[3]=thor4;
  11.   images[4]=thor5;
  12.   images[5]=thor6;
  13.   images[6]=thor7;
  14.   images[7]=thor8;
  15.   images[8]=thor9;
  16.   images[9]=thor10;
  17.   images[10]=thor11;
  18.   images[11]=thor12;
  19.   images[12]=thor13;
  20.   images[13]=thor14;
  21. }

  22. void draw(){
  23. //  if(x < images.length){
  24. //  background(images[x]);
  25. //  }else{
  26. //    x = 0;
  27.     background(images[x]);
  28. //  }
  29.   
  30.   
  31. }

  32. void mousePressed(){
  33. // if(mousePressed){
  34. //  x = x+1;
  35. // } else{
  36. //   x = 0;}
  37. // x = (x+1) % images.length;  // your code
  38. if( ++x == images.length) //your code
  39.   x = 0;
  40.   
  41. }
and i tried what you posted and commented out what I coded.

thanks 

Re: Need Help I'm new.

6 months ago
Here's an even shorter version. Just rename 1st "thor.gif" to "thor1.gif" for it to work though! 
[EDIT] -> newer version: left-click to go back or right-click to go forward! 
Copy code
    final static byte IMAGES = 14;
    final static PImage[] images = new PImage[IMAGES];
    
    final static String NAME = "thor", EXT = ".gif";
    
    byte idx;
    
    void setup() {
      size(600, 400);
      noLoop();
    
      for ( int i = 0; i != IMAGES; 
      ( images[i] = loadImage(NAME + ++i + EXT) )
      .resize(width, height) );
    }
    
    void draw() {
      background(images[idx]);
    }
    
    void MousePressed() {
      redraw();
    
      if (mouseButton == LEFT && --idx == -1)  idx = IMAGES - 1;
      else if (++idx == IMAGES)  idx = 0;
    }
    

Re: Need Help I'm new.

6 months ago
Thanks, I had to make some changes to your code. I should of said that I am learning to code for Android. Your code crashed. I believe it was too intensive on memory (not really sure just crashed). Other than that I really appreciate your help. time to move onto my next little project to learn.