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 › I am new to this...this is driving me crazy...
Page Index Toggle Pages: 1
I am new to this...this is driving me crazy... (Read 1144 times)
I am new to this...this is driving me crazy...
Oct 18th, 2009, 7:06am
 


int x = 0;  // x location of square
int y = 0;  // y location of square

int speed = 5;  // speed of square

int state = 0;  // "state" of square, it's state will determine how it moves

void setup() {
 size(200,200);
}

void draw() {
 framerate(30);
 background(0);
 noStroke();
 fill(255);

 // display the square
 rect(x,y,10,10);

 // Depending on the state, change location of the square
 // Depending on the location, change the state of square
 if (state == 0) {
   x+=speed;
   if (x > width-10) {
     x = width-10;
     state++;
   }
 }
 else if (state == 1) {
   y+= speed;
   if (y > height-10) {
     y = height-10;
     state++;
   }
 }
 else if (state == 2) {
   x-=speed;
   if (x < 0) {
     x = 0;
     state++;
   }
 }
 else if (state == 3) {
   y-=speed;
   if (y < 0) {
     y = 0;
     state=0;
   }
 }


}


Why does encapsulating the if statements into a
for(;x<width-10;x+=speed)
{
 rect(x,y,10,10);
delay(100);
background(0);
}
(other for loops omitted)

not work as expected?
Re: I am new to this...this is driving me crazy...
Reply #1 - Oct 18th, 2009, 8:06am
 
Did you copy the code or do you work with some really old processing versions? Cause you still use framerate not frameRate.
And helping you is a bit hard, cause what is "as expected" ?

Re: I am new to this...this is driving me crazy...
Reply #2 - Oct 18th, 2009, 12:13pm
 
The only reason I could see for putting that into a for loop is if you wanted to have multiple squares all moving independently...  In that case the problem is likely to be that you'd need multiple 'state' variables - one for each square.  If that's the problem then the solution is to create a square object with a state property and associated movement methods...

(But as the others pointed out - you haven't expressed your problem particularly well.)
Re: I am new to this...this is driving me crazy...
Reply #3 - Oct 18th, 2009, 12:13pm
 
Like Cedric (good catch, the framerate!), I am perplex.
I don't see such for loop in your code, and we don't know what isn't working as expected.
Beside, you shouldn't use delay(), this is pretty sure not to work "as you expect".
Re: I am new to this...this is driving me crazy...
Reply #4 - Oct 19th, 2009, 5:16am
 
This is code from Daniel Shiffman's website. The framerate was the only thing (I think) that was outdated and is easily fixed.

The code makes a square move around the border. What I want to do is animate the whole "move around the border" in one draw loop. But when I try, it breaks.

int x = 0;  // x location of square
int y = 0;  // y location of square

int speed = 5;  // speed of square

int state = 0;  // "state" of square, it's state will determine how it moves

void setup() {
 size(200,200);
}

void draw() {
 frameRate(30);
 background(0);
 noStroke();
 fill(255);

 // display the square
 //rect(x,y,10,10);

 // Depending on the state, change location of the square
 // Depending on the location, change the state of square
 for(;x<width-10;x+=speed)
   rect(x,y,10,10);
   delay(100);
   background(0);
 x = width-10;
 for(;y<height-10;y+=speed)
   rect(x,y,10,10);
   delay(100);
   background(0);
 y = height-10;
 for(;x>0;x-=speed)
   rect(x,y,10,10);
   delay(100);
   background(0);
 x = 0;
 for(;y>0;y-=speed)
   rect(x,y,10,10);
   delay(100);
   background(0);
 y = 0;
}
Re: I am new to this...this is driving me crazy...
Reply #5 - Oct 19th, 2009, 5:48am
 
wozub wrote on Oct 19th, 2009, 5:16am:
What I want to do is animate the whole "move around the border" in one draw loop.

So you want to see the rectangle to go around the border 30 times per second
As I wrote, delay() won't work as you expect. Neither draw().
All drawing done in draw() are displayed only when draw() is finished. In other words, the screen is updated only at the end of the draw() function.
Page Index Toggle Pages: 1