Adding a delay in a switch statement, help req.

?

_

Hello,
I need to make a delay in a case step on a switch statement, with a
counter that keeps track of how many times the method danceStep() is
called, and when the condition of an if statement (or other
branchig/looping instruction) is met, the case should start, meet the
condition specified, and jump normally to another case. The problem is
that I cannot make the case loop "normally", as without the counter/if
statement, because it seems to just consider the counter and its
condition instead of the full case (i.e., it checks the condition, runs
only ONE STEP of the rest of the statement, and then waits again until
the condition is met).
What I would like to do, again, is to make the case wait till the
condition is met, and then execute its content UNTIL another condition
is met, regarding that content (and not the counter!!). Yeah I am sure
its tricky to understand what I am trying to say, just give a look to
the code. I have struggled 2 days without finding a possible workable
solution... check the comments, if you want. Please, answer with a
snippet of code, if you can. It would be really useful as I am trying to
get this thing work....


import java.awt.*;
import java.applet.*;

class NinjaRect extends DancingRect {

byte state;

static final byte SE = 0;
static final byte W = 1;
static final byte NE = 2;

int right_x;
int left_x;

byte triggerCounter;
byte westCounter;

public NinjaRect(int x, int y, int w, int h, Color c) {
super(x,y,w,h,c);
right_x = locx + 21;
left_x = locx;
triggerCounter = 0; // this is the trigger counter that should keep
track of the danceStep()
westCounter = 0;
state = SE;
}

public void danceStep() { //when the SE: case is called with the
constructor, it should wait...
switch (state) { //then the thing should go smoothly to SouthEast
following locx+=3,locy+=3
case SE:
triggerCounter++; //instead, after having added this counter, it
just waits for the
if (triggerCounter >= 3) { //condition to be met, then it moves the
thing only
locx += 3; //one step x+3, y+3, and it stops, waiting for the next
cycle
locy += 3; //and when locx==right_x, it normally proceed to the
other steps
if (locx == right_x) { //but I would like to see it slide to
right_x in one movement!
triggerCounter = 0;
state = W;
}
}
break;

/* case SE: // I have tried also this one, but it updates the thing in
one stroke
triggerCounter++;
if (triggerCounter == 3) {
while (locx != right_x) {
locx += 3;
locy += 3;
}
triggerCounter = 0;
state = W;
}
break;
*/
case W:
locx -= 3;
if (locx == left_x) {
if (westCounter == 0) {
westCounter++;
state = NE;
}
else if (westCounter == 1) {
westCounter--;
state = SE;
}
}
break;
case NE:
locx += 3;
locy -= 3;
if (locx == right_x) {
state = W;
}
break;
}
}
}
 
E

Enrique

_ said:
What I would like to do, again, is to make the case wait till the
condition is met, and then execute its content UNTIL another condition
is met

Re-write this as threaded processes that interact with each other.
One sleeping until another wakes it, et cetera. If you are not
familiar with threaded programming, you've got some reading to do.

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top