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 › Issue with MouseX and MouseY
Pages: 1 2 
Issue with MouseX and MouseY (Read 1393 times)
Issue with MouseX and MouseY
Dec 21st, 2009, 2:48pm
 
Hi everyone!

I'm having an issue with MouseX & MouseY, that I'm unable to solve...

When I use them directly inside draw{}, they work perfectly, I mean, they hold the current mouse position. However, I'm trying to use them inside a couple of functions, and in this case, they both contain a 0, and do not update if I move the mouse accross the screen.

To be more precise, I am using these variables inside an infinite for(;Wink, which is at the same time inside a function called by another function, called inside draw{}  Cheesy

Hope you can help me! I'm lost with this one

Re: Issue with MouseX and MouseY
Reply #1 - Dec 21st, 2009, 2:53pm
 
An infinite loop inside the draw function?

That's not good ..
Re: Issue with MouseX and MouseY
Reply #2 - Dec 21st, 2009, 3:00pm
 
i guess you are stuck in your infinite loop. so as it never ends, the coordinates do not update. But there is probably another way to achieve what you want if you can describe it a bit.
Re: Issue with MouseX and MouseY
Reply #3 - Dec 21st, 2009, 3:24pm
 
It's an infinite loop with a break inside it, of course, but to get to that break it has a condition inside it that uses MouseX and MouseY... So it never ends.

By the way, I've made sure that the loop does indeed work, and is repeated unlimited times, and that at the same time MouseX and MouseY don't change from 0... So the problem must be related with mouseX and mouseY... Isn't there any possibility that they show 0 instead of the mouse's coordinates under some circumstance??


Re: Issue with MouseX and MouseY
Reply #4 - Dec 21st, 2009, 3:28pm
 
without knowing your programm i just can asume, but isntead of using this for loop put what you do inside your draw loop. it should work then.
maybe try to use a while loop. if your code is not to long you can also post it.
Re: Issue with MouseX and MouseY
Reply #5 - Dec 21st, 2009, 3:30pm
 
The values of mouseX and mouseY won't change while you are in the draw method.
Re: Issue with MouseX and MouseY
Reply #6 - Dec 21st, 2009, 3:39pm
 
It sounds to me like there might be a much better way to achieve the result you want - and as JR just pointed out mouseX/Y only update at the end of the draw loop, so if you do anything to interrupt that they won't get updated.  And if that creates a situation where your 'infinite loop' can't be exited... well that's not good at all.

Post some code and an explanation of what you're trying to achieve...
Re: Issue with MouseX and MouseY
Reply #7 - Dec 21st, 2009, 3:45pm
 
yes what i thought, so probably a "while" could work. but we have to take a look at the code i guess.
Re: Issue with MouseX and MouseY
Reply #8 - Dec 21st, 2009, 3:57pm
 
It's the fact that mouseX and mouseY don't update until a new draw the thing that is causing the problem... I didn't know that!

Now, my problem is how to achieve the same thing I was doing, without using mouseX and mouseY...

I'll post the code, but it is not quite readable, as it's long, messy and variable and function names are in spansih:

Code:

...

void crearBarcosPlayer()
{
 int[] temp = new int[3];
 
 System.out.println("Portaaviones");
 temp = leerCord(1,0);
 barcosPlayer[0] = new Barco(5,temp[0],temp[1],temp[2]); //Portaaviones
 numBarcosPlayer++;
 System.out.println("Acorazado");
 
...
}



int[] leerCord(int selector, int barcoID) //Terminado. Puede tener bugs
{
 int[] coordenadas = new int[3]; //coordenadas[0] es la coordenada X, [1] es la Y y [2] es la dirección
 int x_inic=0, y_inic=0;

 ...

 for (;; )
 {
// ***The following is what doesnt work***

   if (mousePressed && mouseX>=x_inic && mouseY>=y_inic && mouseX<width-res && mouseY<y_inic+10*res )
   {
coordenadas[0] = (int)((mouseX-x_inic)/res);
coordenadas[1] = (int)((mouseY-y_inic)/res);

break;
   }  
   

   
 }
 
...
 
 return coordenadas;
}



void setup()
{
 size($tam,2*$tam);
 
 tableroPlayer = crearTablero(tableroPlayer);
 tableroCPU = crearTablero(tableroCPU);
 
 dispTableroPlayer();
 dispTableroCPU();
 
}




void draw()
{
 if (!barcosCreados)
 {
   crearBarcosPlayer();
   crearBarcosCPU();
   barcosCreados = true;
 }
 

 ...
 
 
}

   
Re: Issue with MouseX and MouseY
Reply #9 - Dec 21st, 2009, 4:02pm
 
ok Smiley i have to admit that i dont get your code. but did you try to use void mousePressed instead of if(mousePressed) ?

file:///D:/07_Processing/processing-1.0.5/reference/mousePressed_.html
Re: Issue with MouseX and MouseY
Reply #10 - Dec 21st, 2009, 4:02pm
 
if you edit it, remove the smileys please.  use " Check this if you'll be adding code (or don't like smilies)."
Re: Issue with MouseX and MouseY
Reply #11 - Dec 21st, 2009, 4:22pm
 
The code is really messy , but I'll try to explain more or less the part we're discussing:

1st, the draw function calls "crearBarcosPlayer()", which creates an array of objects "barco. What we're creating is boats for a 'battleships' game, so we need to click in the squares where the boats will be placed.

To do so, I call the "leerCord()" function, which is the one that I've been having probles with. Inside this function, and inside the for( ; ; ) loop, I wanted the program to continue ONLY if the mouse is pressed inside certain coordinates. If it is, then those coordinates are manipulated and stores inside an array for afterwards declaring the "barco" objects.

Therefore, I need to have the mouseX and mouseY coordinates, however, I do not want the draw function to continue until I have those coordinates, because I need to declare the boats first.

To solve this, I have just tried "declaring" the boats inside the Setup function, but that doesn't work because the background isn't printed until the end of Setup, so I can't click on it...

Hope I've clarified things! And thanks in advance!!!



EDIT:

Also, I believe the mousePressed() function won't be useful here, either, because what I want is to place the boats just at the beginning, and not each time that the mouse is pressed...

Re: Issue with MouseX and MouseY
Reply #12 - Dec 22nd, 2009, 12:45am
 
Well you clearly have a fundamental problem with your approach as the moment you start your 'infinite loop' in draw mouseX/Y will never update.  Some general comments:

Something that happens once should be put in setup.  If you need to repeat the action - for instance when restarting a game - put it into a function which can be called from setup and when a new game starts.

Don't fight with the draw loop.  There are plenty of alternative options that don't involve 'infinite loops' (which are just plain bad) and don't involve using things like noLoop() which again will present you with the problems you're having.  One possibility is to have a 'gameState' variable that defines the current point in the game (e.g. setting up; player turn; computer turn etc.).  I'd be tempted to use an int and define constants for clarity.  So for example:

Code:
int gameState;
int GAMESETUP = 0;
int PLAYERTURN = 1;
int COMPUTERTURN = 2;
// etc...

void setup() {
 size(100,100);
 //...
 gameState = GAMESETUP;
}


Then in draw you simply test the value of gameState to determine what code gets run:

Code:
void draw() {
 switch(gameState) {
   case GAMESETUP:
     // set up game...

     // once the game is set up go to the next state
     if(someCondition){
       gameState = PLAYERTURN;
     }
   break;

   case PLAYERTURN:
     // handle player turn

     // test to see if it's complete and move to the next state
     if(someCondition){
       gameState = COMPUTERTURN;
     }
   break;


   case COMPUTERTURN:

     if(someCondition){
       gameState = PLAYERTURN;
     }
   break;
 }
}


This way draw is running continuously so you'll be able to register mouse events properly...
Re: Issue with MouseX and MouseY
Reply #13 - Dec 22nd, 2009, 10:11am
 
Hi, blindfish, I'm interested in the solution you propose, but there's something I don't understand:

If the draw function just contains the switch for the gameState, where do I run the rest of the application? Maybe it is a stupid question, but for the little I know about Processing, I don't know how to run functions without calling them inside setup() or draw().
Re: Issue with MouseX and MouseY
Reply #14 - Dec 22nd, 2009, 10:42am
 
you do it right there... where he wrote  "// set up game..." or "// handle player turn" you do whatever you want.
Pages: 1 2