We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › count mousePressed
Page Index Toggle Pages: 1
count mousePressed (Read 1542 times)
count mousePressed
May 22nd, 2007, 2:11pm
 
hi,

I'm fairly new to processing and want the program im writing to play a different set of images each time the mouse is pressed. I'm told that I need to include a counter but so far I havent come across anything that has worked. If anyone can offer any advice that would be greatly appreciated.

thanks!

heres my code



Balloons series1, series2;
float xpos, ypos;


void setup()
{

 size(450, 450);
 
 series1 = new Balloons("balloon", 5);
 series2 = new Balloons("ball", 5);

}

   
void draw()
 {
//int count = 0;
   if(mousePressed)
 {
 frameRate(7);
 series1.display(xpos-series1.getWidth()/2, ypos);
   //noLoop();
 }
 

//else if (mousePressed)
//{
  //counter = 2;
 //frameRate(3);
 //series2.display(xpos-series1.getWidth()/2, ypos);
//}

 
else  
{
 //frameRate(1);
background(0);    //having the value as 500 will make it pause
 }
 //println(reading);

}  
 
class Balloons
{
 PImage[]bal;
 int frame;
 int numFrames;
 
 Balloons(String imageName, int frameCount){
   numFrames = frameCount;
   bal = new PImage[numFrames];
   loadImages(imageName);
 }
 
  void loadImages(String name) {
   for(int i=0; i<numFrames; i++) {
     String imageName = name + ((i < 4) ? "" : "") + i + ".jpg";
     bal[i] = loadImage(imageName);
   }
  }
 void display(float xpos, float ypos)
 {
   frame = (frame+1)%numFrames;
   image(bal[frame], 1, 1);
 }
 
 int getWidth() {
   return bal[0].width;
 }

}
Re: count mousePressed
Reply #1 - May 23rd, 2007, 8:27am
 
Hi! The only thing i am sure about is your counter thing. Basically what you want is to count how many times your mouse has been pressed.

Here's how you do that...

Code:

//SET AN INT VARIABLE
int counter;

//VOID SETUP
void setup()
{
size(200, 200);
}

//VOID MOUSERELEASED
void mouseReleased()
{
counter = counter + 1;
}


Thats basically how you would do the counting part, if you were to view the counter, you would type...

Code:

//SET AN INT VARIABLE
int counter;

//VOID SETUP
void setup()
{
size(200, 200);
}

//VOID DRAW
void draw()
{
text(counter, 10, 10);
}

//VOID MOUSERELEASED
void mouseReleased()
{
counter = counter + 1;
}


Obviously, you would add the text configurations before everything within the code.

Unfortunately, i'm unsure about the displaying of different images, but i'm sure someone else will help you.

Thanks,
Steve.
Re: count mousePressed
Reply #2 - May 23rd, 2007, 9:35am
 
hey,

thanks for the quick reply. i've included the code you
put up,and it works, it knows its counting, but it doesnt
do anything with it... i need it to recognise it and then
change the output.

heres what i've got so far, but it continues to play the
same images. i assume i need to put 'counter' in draw with
the if / else statements, however i'm not quite sure what
to do with them after that...


thanks!



Balloons series1, series2;
float xpos, ypos;
//SET AN INT VARIABLE
int counter;

void setup()
{

 size(450, 450);
 
 series1 = new Balloons("balloon", 5);
 series2 = new Balloons("ball", 5);

}

void mouseReleased()
{
counter = counter + 1;
println("one");
}
   
void draw()
 {
counter = 0;
   if(mousePressed)
 {
 frameRate(7);
 series1.display(xpos-series1.getWidth()/2, ypos);

 }
 

else if (mousePressed)
{
  counter = 1;
 frameRate(3);
 series2.display(xpos-series1.getWidth()/2, ypos);
}

 
else  
{
 //frameRate(1);
background(0);    //having the value as 500 will make it pause
 }
 //println(reading);

}  
 
class Balloons
{
 PImage[]bal;
 int frame;
 int numFrames;
 
 Balloons(String imageName, int frameCount){
   numFrames = frameCount;
   bal = new PImage[numFrames];
   loadImages(imageName);
 }
 
  void loadImages(String name) {
   for(int i=0; i<numFrames; i++) {
     String imageName = name + ((i < 4) ? "" : "") + i + ".jpg";
     bal[i] = loadImage(imageName);
   }
  }
 void display(float xpos, float ypos)
 {
   frame = (frame+1)%numFrames;
   image(bal[frame], 1, 1);
 }
 
 int getWidth() {
   return bal[0].width;
 }

}
Page Index Toggle Pages: 1