ben_hem
Senior Member
Offline
Posts: 399
CA, US
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.