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.
IndexDiscussionExhibition › Player class
Page Index Toggle Pages: 1
Player class (Read 1152 times)
Player class
Nov 1st, 2009, 5:50pm
 
With so many topics with promissing titles, I'm kind of embarassed to share my problem with you guys… But anyway, I’m learning programming logic in my university through processing and I just learned about classes. Still, I haven’t managed to actually use one…

I’m trying through this piece of code make an ellipse follow the mouse cursor:

int tamanho;

AVATAR player;
void setup()
{
 tamanho = 400;
 size(tamanho,tamanho);
 player = new AVATAR (color(31,155,16), CENTER, CENTER, 15);
}

void draw()
{
   background(color(#141B5D));
   player.update();
   player.move();
}

//Then, in a new tab

public class AVATAR
{
 color c; float xpos; float ypos; float speed;
   
   AVATAR(color writeColor, float writeXpos, foat writeYpos, float writeSpeed)
   {
     c = writeColor;
     xpos = writeXpos;
     ypos = writeYpos;
     speed = writeSpeed;
   }
   
   void update()
   {
         noFill();
         strokeWeight(5);
         stroke(c);
         ellipseMode(CENTER);
         ellipse(xpos, ypos, (height/15), (width/15));
   }
   
   void move()
   {  
     if(xpos < mouseX)
     {
       xpos = xpos + speed;
     }
     
     if(xpos > mouseX)
     {
       xpox = xpos - speed;
     }
     
     if(xpos = mouseX)
     {
       xpos = mouseX;
     }
     
     if(ypos < mouseY)
     {
       ypos = ypos + speed;
     }
     
     if(ypos > mouseY)
     {
       ypox = ypos - speed;
     }
     
     if(ypos = mouseY)
     {
       ypos = mouseY;
     }
   }
}
Re: Player class
Reply #1 - Nov 1st, 2009, 7:06pm
 
Eh... Problem solved.
Re: Player class
Reply #2 - Nov 2nd, 2009, 7:19am
 
Ok, the player Avatar is following the mouse cursor smoothly and his initial position is placed at the center of the screen. However, due to its mouse following proprieties, it immediately goes to position (0,0) until the mouse is placed over the screen. I’ve tried to place a void mouseReleased () inside the AVATAR class in order to lock it in place until the player clicks over the screen but, it’s not working…
Here’s the code:
int tamanho;
AVATAR player;

void setup()
{
 tamanho = 400;
 size(tamanho,tamanho);
 
 player = new AVATAR (color(31,155,16), (tamanho/2), (tamanho/2), 5);
}

void draw()
{
   background(color(#141B5D));
   player.display();
   player.mouseReleased();
}

//Then the AVATAR class:

public class AVATAR
{
 color c;
 int xpos;
 int ypos;
 int speed;

 AVATAR(color writeColor, int writeXpos, int writeYpos, int writeSpeed)
 {
   c = writeColor;
   xpos = writeXpos;
   ypos = writeYpos;
   speed = writeSpeed;
 }

 void display()
 {
   noFill();
   strokeWeight(5);
   stroke(c);
   ellipseMode(CENTER);
   ellipse(xpos, ypos, (height/15), (width/15));
 }

 void mouseReleased ()
 {
   void move()
   {
     if(xpos < mouseX)
     {
       xpos = xpos + speed;
       if(xpos > mouseX)
       {
         xpos = mouseX;
       }
     }

     if(xpos > mouseX)
     {
       xpos = xpos - speed;
       if(xpos < mouseX)
       {
         xpos = mouseX;
       }
     }

     if(ypos < mouseY)
     {
       ypos = ypos + speed;
       if(ypos > mouseY)
       {
         ypos = mouseY;
       }
     }

     if(ypos > mouseY)
     {
       ypos = ypos - speed;
       if(ypos < mouseY)
       {
         ypos = mouseY;
       }
     }
   }
 }
}
Re: Player class
Reply #3 - Nov 2nd, 2009, 8:35am
 
To save yourself further embarrassment try and post in the right forum in future: Exhibition is for finished projects; not asking for help Wink

As for your problem.  You'll need to rely on Java's MouseEvent class to determine if the mouse is over the screen and only move the circle if it is (a suitable global boolean variable might be useful here).  I remember posting an example of this Class in action a while ago.  However if you want to implement mouse events within a Class you may want to go one step further and follow the method suggested here by Quark.

Note also that you can't declare a function within a function:

Code:
void mouseReleased() {
 void move() {
   //...
 }
}


... throws an error.
Re: Player class
Reply #4 - Nov 2nd, 2009, 11:08am
 
Thank you. Eh... If I have any more questions, where should I post then?
Re: Player class
Reply #5 - Nov 2nd, 2009, 12:17pm
 
The forums are described quite clearly here.  This question would have been better placed under 'Programs' Wink
Page Index Toggle Pages: 1