Game Development Reference
In-Depth Information
Calling the move() Method Periodically
To move naturally on the map, the monster's move() method should be called periodically
(every 100ms) by a thread named AIGoThread .
Here is the run() method in AIGoThread :
//AIGoThread run() method
public void run(){
while (flag){
while (isGameOn){
for (Monster m:gv.monsterArray){
if (m.isLocked){
continue ;
}
if (checkIfCrossWay(m)){
int heroX = gv.hero.x+TILE_SIZE/2;
int heroY
= gv.hero.y+SPRITE_HEIGHT-TILE_SIZE/2;
m.direction
= searchForDirection(heroX, heroY, m);
}
if (checkIfTurnAround(m)){
m.direction
=
3-m.direction;
}
m.move();
}
try {
Thread.sleep(AI_THREAD_SLEEP_SPAN);
}
catch (Exception e){
e.printStackTrace();
}
}
try {
//thread sleeps for the given interval of time in ms
Thread.sleep(1500);
Search WWH ::




Custom Search