JAVA swing. movement from random point to random point

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


JAVA swing. movement from random point to random point



i have a problem with one thing. I have a map of 10 cities and a civilian. I want the civilian to be walking from city to city randomly. But the problem is that the city is beeing chosen on and on so the civilian is changing the destination before he reachs it. This is my part of a code of a Jpanel where everything is drawn:


@Override
public void run() {
while (running) {
update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException ex) {
}
}
}

private void update() {
if (game != null && running == true) {

c.goTo(cities); // c is civilian

}
}



and this is part of code for civilian


private boolean set = true;
public void move(int x, int y) {
if (this.location.x != x || this.location.y != y) {
if (this.location.x > x) {
this.location.x -= 1;
} else {
this.location.x += 1;
}

if (this.location.y > y) {
this.location.y -= 1;
} else {
this.location.y += 1;
}
}
}

public void goTo(ArrayList<City> cities) {

City city;

if (set) {
city = cities.get(rand());

move(city.location.x, city.location.y);
set = false;
} else {
set = true;
}

}
public int rand() {

int i;
Random rand = new Random();
i = rand.nextInt(10);

return i;
}



How to solve it ?




1 Answer
1



So, your problem is here:


while (running) {
update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException ex) {
}
}



You're calling update every 17 milliseconds which in turn is causing your civilian to move to a new city every 17 milliseconds. You could make a separate statement that calls update while another boolean statement is false so that you travel only when he is in a city.



For example:


boolean travelling = //whatever you go about to configure this
while(travelling == false){
update();
}



This will cause him to only travel when he is not in a city. Here is some very rough code (you will have to configure it to your liking):



//civilian x //civilian y
if(this.location.x == //randomed city.x && this.location.y == //randomed city.y){
travelling = false;
}


//civilian x //civilian y
if(this.location.x == //randomed city.x && this.location.y == //randomed city.y){
travelling = false;
}



This will most likely need to be within the run() method in your first set of code, so it can be checked over and over. But let me explain what the above code is doing:


run()



First, you have a thread or something keeping it running checking if your civilian's x and y correspond to the most recently randomed city's x and y, obviously when they're the same, the civilian is at the city.



Second, when the x and y's are the same, the statement makes travelling false


travelling



Third, When travelling is false, your custom update method is called, picking a new city, at random and putting your civilian back on the move.


travelling


update






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results