Systematic Instrucations in Java?

How could I make a set of systematic instrucations? like so after one thing is done the next thing works?

like in this example;


derpX += .04;
derpY -= .5;
derpX += .3;
//these variables are x and y values for a moving image

Like I want these operations to work one at a time, not like the normally would (as they normally operate all at the same time) how could i do this?

There’re lot ways to achieve sequence execution, ranging from putting while() after each statement, to create your own callback listener.

could you show me an example? sorry for my noobish java skills

They already do run one after another, not all at the same time?


float targetX = derpX + .4;
derpX += .4
while (derpX != targetX);
derpY -= .5;

The last statement will be executed after derpX meets the target.
I can’t provide callback examples, too much classes and interfaces needed. Try to google it.

They do, but the first one will still be operating while the last one is. And thats the problem. I would want these to operate one at a time

I dont see how I can use this for many sets of instruction… :frowning:

Guys just saying this is how i finally got it to work. Thanks for any tips though.


			if (move01 == true) {
				homelessX += .04f;
				if (homelessX > 700) {
					move01 = false;
					move02 = true;
				}
			}
			if (move02 == true) {
				homelessY += .04f;
				if (homelessY > 900) {
					move02 = false;
					move03 = true;
				}
			}
			if (move03 == true) {
				homelessX += .04f;
				if (homelessX > 800) {
					move03 = false;
					move04 = true;
				}
			}
			if (move04 == true) {
				homelessY += .04f;
				if (homelessY > 1000) {
					move04 = false;
					move05 = true;
				}
			}
			if (move05 == true) {
				homelessX -= .04f;
				if (homelessX < 675) {
					move05 = false;
					move06 = true;
				}
			}
			if (move06 == true) {
				homelessY += .04f;
				if (homelessY > 1100) {
					move06 = false;
					move07 = true;
				}
			}
			if (move07 == true) {
				homelessX -= .04f;
				if (homelessX < 600) {
					move07 = false;
					move08 = true;
				}
			}
			if (move08 == true) {
				homelessY -= .04f;
				if (homelessY < 743) {
					move08 = false;
					move01 = true;
				}
			}
//homelessX and homelessY are the x and y cordinates of the image

Great then.

Hot damn dude, that’s too much!

Here’s a better (and scalable) example:


class Action {
    enum Conditions {
        LESS_THAN, GREATER_THAN;
        
        public boolean test(float v1, float v2) {
            switch(this) {
                case LESS_THAN: return v1 < v2;
                case GREATER_THAN: return v1 > v2;
            }
        }
    }
    
    private boolean doneX, doneY;
    private Condition cond;
    private float condValue;
    
    private float dx, dy;
    
    public Action(float dx, float dy) {
        this.dx = dx;
        this.dy = dy;
    }
    
    public void act(Player player) {
        player.x += dx;
        player.y += dy;
    }
    
    public Action doneWhenX(Condition cond, float condValue) {
        doneX = true; doneY = false;
        this.cond = cond;
        this.condValue = condValue;
        return this;
    }
    
    public Action doneWhenY(Condition cond, float condValue) {
        doneY = true; doneX = false;
        this.cond = cond;
        this.condValue = condValue;
        return this;
    }
    
    public boolean isDone(Player player) {
        if(cond == null)
            throw new IllegalArgumentException("No condition specified.");
        
        if(doneX)
            return cond.test(player.x, condValue);
        if(doneY)
            return cond.test(player.y, condValue);
    }
}

public class MyClass {
    public static void main(String[] args) {
        PriorityQueue<Action> actions = new PriorityQueue<>();
        actions.add(new Action(0.04f,0).doneWhenX(Action.Condition.GREATER_THAN, 700));
        //...
        
        Action curAction = null;

        // gameloop
        while(true) {
            //...
            
            if(curAction == null || curAction.isDone())
                curAction = actions.poll();
            
            if(curAction != null)
                curAction.act(player);
            
            //...
        }
    }
}