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 › What is going on with these blocks
Page Index Toggle Pages: 1
What is going on with these blocks? (Read 444 times)
What is going on with these blocks?
Mar 10th, 2010, 7:45pm
 
The code is below. The program starts off running just fine. Yet apparently the blocks are going faster and faster, and eventually move so fast they ignore my bounce-off-sides conditional statement. I have the speed variable constrained in the code, which doesn't seem to be doing the job. I also tried constraining xpos to be between 100 and 700, but that failed as well. I'm grateful to anyone who can find the mistake in my code (which I'm sure is alarmingly obvious).

Code:
float direction=1;
float speed=4;

Enemies enemy1;
Enemies enemy2;
Enemies enemy3;

void setup() {
 size(800,600);
 smooth();
 enemy1=new Enemies(color(0,0,125),200,50);
 enemy2=new Enemies(color(125,0,0),400,50);
 enemy3=new Enemies(color(0,125,0),600,50);
}

void draw() {
 background(255);
 
 constrain(speed,0,4);
 
 enemy1.display();
 enemy2.display();
 enemy3.display();
 
 enemy1.move();
 enemy2.move();
 enemy3.move();
}

Code:
class Enemies {
 color c;
 float xpos;
 float ypos;
 
 Enemies(color tempC, float tempXpos, float tempYpos) {
   c=tempC;
   xpos=tempXpos;
   ypos=tempYpos;
 }
 
 void display() {
   rectMode(CENTER);
   fill(c);
   rect(xpos,ypos,50,50);
 }
 
 void move() {
   xpos=xpos+speed*direction;
   if (xpos>700||xpos<100) {
direction=-direction;
   }
 }
}
   


Thank you!
Re: What is going on with these blocks?
Reply #1 - Mar 10th, 2010, 8:38pm
 
Hey, cool -- progress --

I think part of what's happening is: the direction is getting reversed in the middle of your draw() cycle, say, on enemy2.  Then enemy1 has already moved one way, but the other two move the other way.  So they can shift around unpredictably, and direction can be swapped multiple times during a single frame eventually.  Once a single enemy gets off the screen, they'll effectively reverse the direction every frame.

To get around this, you could move the bounce test to a function inside your enemy class, something like this: boolean reverseCheck{ return(x<100 || x>700); }
and do a reversecheck on each enemy as the last thing in your draw() cycle.  If a single enemy returns true, just reverse the direction, and then call break;, which will exit the loop and stop checking the rest of the enemies.  Re: using a loop that can be exited, see below:

Some issues in this code:
constrain(speed,0,4); isn't code that does anything -- to set speed equal to the resulting value, you'd do this:  speed=constrain(speed,0,4); -- because "constrain()" is a function that returns a value, rather than a function that modifies one of its parameters.

It will also make your life easier to make enemy1, 2, 3 (etc) an array:
Enemies[] enemyArray = new Enemies[3];

for (int i=0;i<enemyArray.length;i++){
 enemyArray[i] = new enemy(enemy vars);
}

for (int i=0;i<enemyArray.length;i++){
 enemyArray[i].move();
 enemyArray[i].display();
}
etc.  This allows you to change the number of enemies without copypasting.  If you want to start off each enemy diifferently, you can do things like:
enemyArray[0] = new Enemies(specific vars here);

hope this helps

[edit] you could also make a global boolean named "reverseHasHappenedThisFrame" or something ;)   -- that way it could be set to false as the first act in draw(), and if the enemy class's reverse function got called, it would test that boolean before reversing the direction float, and then set it to true.
Page Index Toggle Pages: 1