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 › Making a function in draw() to play once
Page Index Toggle Pages: 1
Making a function in draw() to play once (Read 1077 times)
Making a function in draw() to play once
Mar 24th, 2007, 2:00am
 
Hi guys,
I'm sorry if the title is no very clear but it's hard to explain the problem i'm facing with a few words.

I'd like to know how to make a function to play once, and this especially when the mouse is only on one area of the screen.

here is the code

Quote:
void setup(){
size(1024,768, P3D);
frameRate(30);
}

void draw(){
background(0);
{
 if(mouseX <= 512 && mouseY <= 384)
 myFunction1();
 else if(mouseX <= 1024 && mouseY <= 384)
 myFunction2();
 else if(mouseX <= 512 && mouseY <= 768)
 myFunction3();
 else if(mouseX <= 1024 && mouseY <= 768)
 myFunction4();
 }
}

void myFunction1(){
.................................}

void myFunction2(){
.................................}

void myFunction3(){
.................................}

void myFunction4(){
.................................}



If the mouse cursor position remains in one of these four areas, i'd like the myFunction() of the concerned area to be executed once, then when the mouse cursor leaves the area, this area will turn back to a "ready to be executed again" status.

I'm not sure if i'm clear.

Thanks for any suggestion.
Re: Making a function in draw() to play once
Reply #1 - Mar 24th, 2007, 3:23am
 
Basically you want to track the state of each area with a boolean value. So when you mouseover you fire the function and mark the boolean true, then don't fire the function while the boolean is true, and when you mouseout reset the boolean to false.
Re: Making a function in draw() to play once
Reply #2 - Mar 24th, 2007, 4:20am
 
Thank you,

yes that's exactly what i'm looking for  Smiley

I thought about using boolean, but can't figure out how to tell the boolean to switch to false right after it has been switched to true (when the mouse cursor was detected in the area) and ignore any further mouse presence in the area.

Quote:
if mouse cursor is inside the declared area1
then activate once myfunction1()
else if mouse cursor is inside the declared area2
then activate once myfunction2()
else if mouse cursor is inside the declared area3
then activate once myfunction3()
else if mouse cursor is inside the declared area4
then activate once myfunction4()


Re: Making a function in draw() to play once
Reply #3 - Mar 24th, 2007, 9:36am
 
This may help you:

Code:
//before draw:
boolean[] areas;
//set all to false.

//in draw
if(mouse inside area1 && !area[0])
area[0]=true;
function(1);
else
area[0]=false;

if(mouse inside area2 && !area[1])
//etc etc etc
Re: Making a function in draw() to play once
Reply #4 - Mar 24th, 2007, 2:15pm
 
Thanks JohnG for the sample code.

I've followed it and now a get a NullPointerException

here is the code :

Quote:
boolean[] area;

void setup(){
size(1024,768, P3D);
frameRate(30);
}

void draw(){
background(20);
 if(mouseX <= 512 && mouseY <= 384 && !area[0]){
 area[0] = true;
 myFunction1();}
 else{
 area[0] = false;}
 
 if(mouseX <= 1023 && mouseY <= 384 && !area[1]){
 area[1] = true;
 myFunction2();}
 else{
 area[1] = false;}
 
 if(mouseX <= 512 && mouseY <= 767 && !area[2]){
 area[2] = true;
 myFunction3();}
 else{
 area[2] = false;}
 
 if(mouseX <= 1023 && mouseY <= 767 && !area[3]){
 area[3] = true;
 myFunction4();}
 else{
 area[3] = false;}
 }


void myFunction1(){
fill(255,0,0);
rect(0,0,512,384);}

void myFunction2(){
fill(0,255,0);
rect(512,0,512,384);}

void myFunction3(){
fill(0,0,255);
rect(0,384,512,384);}

void myFunction4(){
fill(255,255,255);
rect(512,384,512,384);}


what's wrong with this code ?

Also, i can't really find out in this code when the myFunction is shown only one time until the mouse leaves the area.

Because it is in draw(), it will be repeated 30 times per second.

My goal is to get the rectangle (where the mouse is detected) to be displayed only one time and disappear, even if the mouse remains and moves in its area .

Thanks for your help.
Re: Making a function in draw() to play once
Reply #5 - Mar 24th, 2007, 2:30pm
 
You missed the "set these all to false" part:

Code:
//in draw:
areas=new boolean[4];
for(int i=0;i<areas.length;i++)
areas[i]=false;


As to how it works, if area[0] is false, and the mouse is in the first area, the functon will run, but area[0] will get set to true. Next time around draw, the mouse is still in the area, however area[0] is true, so it won't get run.

However in typing that I've seen an error in my pseudo code, you need to make it:

Code:
  if(mouseX <= 512 && mouseY <= 384){
if(area[0]==false)
{
myFunction1();
area[0]==true;
}
else{
area[0] = false;}


The previous version would have made the function get run every alternating frame unfortunately.


Re: Making a function in draw() to play once
Reply #6 - Mar 24th, 2007, 3:17pm
 
John,

I've just included your corrected code in my code.

Quote:
boolean[] areas;

void setup(){
size(1024,768, P3D);
frameRate(30);
}

void draw(){
background(20);

areas=new boolean[4];
for(int i=0;i<areas.length;i++)
 areas[i]=false;

if(mouseX >=2 && mouseX <=512 && mouseY >=2 && mouseY <=384){
 if(areas[0] == false)
 {
 myFunction1();
 areas[0] = true;
 }
 else{
 areas[0] = false;}}
 
if(mouseX >=512 && mouseX <=1024 && mouseY >=2 && mouseY <=384){
 if(areas[1] == false)
 {
 myFunction2();
 areas[1] =  true;
 }
 else{
 areas[1] = false;}}
 
if(mouseX >=2 && mouseX <=512 && mouseY >=384 && mouseY <=768){
 if(areas[2] == false)
 {
 myFunction3();
 areas[2] = true;
 }
 else{
 areas[2] = false;}}
 
if(mouseX >=512 && mouseX <=1024 && mouseY >=384 && mouseY <=768){
if(areas[3] == false)
 {
 myFunction4();
 areas[3] = true;
 }
 else{
 areas[3] = false;}}
}


void myFunction1(){
fill(255,0,0);
rect(0,0,512,384);}

void myFunction2(){
fill(0,255,0);
rect(512,0,512,384);}

void myFunction3(){
fill(0,0,255);
rect(0,384,512,384);}

void myFunction4(){
fill(255,255,255);
rect(512,384,512,384);}


I don't understand why the result is the same.

The rectangle where the mouse is located is showing up, but it still stays.

Thanks.
Re: Making a function in draw() to play once
Reply #7 - Mar 24th, 2007, 4:30pm
 
Sorry, my fault again, I shoudl stop trying to answer things whilst watching films, I'e tested it this time:

Code:
boolean[] areas;

void setup(){
size(1024,768, P3D);
areas=new boolean[4];
for(int i=0;i<areas.length;i++)
areas[i]=false;
frameRate(30);
}

void draw(){
background(20);


if(mouseX >=2 && mouseX <=512 && mouseY >=2 && mouseY <=384){
if(areas[0] == false)
{
myFunction1();
areas[0] = true;
}
}
else{
areas[0] = false;
}

if(mouseX >=512 && mouseX <=1024 && mouseY >=2 && mouseY <=384){
if(areas[1] == false)
{
myFunction2();
areas[1] = true;
}
}
else{
areas[1] = false;
}

if(mouseX >=2 && mouseX <=512 && mouseY >=384 && mouseY <=768){
if(areas[2] == false)
{
myFunction3();
areas[2] = true;
}
}
else{
areas[2] = false;
}

if(mouseX >=512 && mouseX <=1024 && mouseY >=384 && mouseY <=768){
if(areas[3] == false)
{
myFunction4();
areas[3] = true;
}
}
else{
areas[3] = false;
}
}


void myFunction1()
{
fill(255,0,0);
rect(0,0,512,384);
}

void myFunction2(){
fill(0,255,0);
rect(512,0,512,384);
}

void myFunction3(){
fill(0,0,255);
rect(0,384,512,384);
}

void myFunction4(){
fill(255,255,255);
rect(512,384,512,384);
}

Re: Making a function in draw() to play once
Reply #8 - Mar 24th, 2007, 5:03pm
 
Thank you so much, this is exactly what i was trying to get for days.

The active community here is not very big, but it is really nice and helpful ! Smiley
Page Index Toggle Pages: 1