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 & HelpPrograms › First attempt at Processing and struggling
Page Index Toggle Pages: 1
First attempt at Processing and struggling (Read 444 times)
First attempt at Processing and struggling
Mar 31st, 2009, 4:31pm
 
Hi,

Im new to Processing and I've been struggling to create a simple code of a rectangle moving left when mouse is left clicked and moving right when mouse is right clicked. I've looked at all the Input references (mouseButton especially), tried that, tried to edit that, looked at Input examples on the program - I just don't understand how to do this simple task. Please someone help me! I will appreciate it tremendously!

Re: First attempt at Processing and struggling
Reply #1 - Mar 31st, 2009, 5:10pm
 
Alright, I will give you a start and some hints...

this is a sketch of a moving rectangle.
As you can see i add the 1 ( the speed)  to the position as long as it is 100px away from the right side. if it is, i set the speed to zero. Now you have to add some mouseAction...
look at this reference Page:
http://processing.org/reference/mouseButton.html
think about how you can make it turn back again and how you would stop it if its on the left side of the sketch.
If you need further help, feel free to ask, but give it a try, its worth it.




float x = 100;
float speed = 1;

void setup(){
 size( 400, 400 );
 smooth();
 background(255);
 rectMode(CENTER);
}  


void draw(){
 background(255);
 rect(x,height/2,50,50);

 x=x+speed;

 if(x==width-100 ){
   speed=0;
 }
}
Page Index Toggle Pages: 1