Hello, it is me again. This time I have got a problem in the same game, but this time with the movement of another character.
Basically what it is supposed to do is check if the area to the left of it is free, and if it is not move in that direction.
if the left area is not free, it does the same action for the area directly above the AI. If the area above the AI is not free, it sets a boolean to true which is supposed to stop it from trying to go north (above) and make it go down instead, until the area left to it is collision free. This will of course set the boolean back to false.
The AI does its job perfectly, until it collides with an object directly above it. It will face the right way, but stops moving. Here is the code in question:
The boolean that is supposed to check if the Actor has already Tried to go north is called Tried, logically.
Any help would be much appreciated ;')
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Thief extends Actor
{
private char dir = 'D';
private static int imageCounter;
private boolean Tried = false;
GreenfootImage[] imagesL = new GreenfootImage[7];
GreenfootImage[] imagesR = new GreenfootImage[7];
GreenfootImage[] imagesU = new GreenfootImage[7];
GreenfootImage[] imagesD = new GreenfootImage[7];
public void act()
{
animate();
animation();
AIMovement();
}
public void AIMovement(){
int AIX = getX();
int AIY = getY();
if (getOneObjectAtOffset(-(getImage().getWidth()/2)-4, 0, Obstacle.class) == null){
setLocation(AIX-4, AIY);
dir = 'L';
Tried = false;
}
else{
if (getOneObjectAtOffset(0, -(getImage().getHeight()/2)-4, Obstacle.class) == null){
if (Tried != true){
setLocation(AIX, AIY-4);
dir = 'U';
}
}
else{
setLocation(AIX, AIY+4);
dir = 'D';
Tried = true;
}
}
}
public void animate(){
for( int i=0; i<imagesD.length; i++ ) {
switch (dir){
case 'D':
imagesD[i] = new GreenfootImage( "T_D_" + i + ".png" );
setImage( imagesD[imageCounter] );
break;
case 'U':
imagesU[i] = new GreenfootImage( "T_U_" + i + ".png" );
setImage( imagesU[imageCounter] );
break;
case 'L':
imagesL[i] = new GreenfootImage( "T_L_" + i + ".png" );
setImage( imagesL[imageCounter] );
break;
case 'R':
imagesR[i] = new GreenfootImage( "T_R_" + i + ".png" );
setImage( imagesR[imageCounter] );
break;
}
}
}
public void animation()
{
switch (dir){
case 'D':
imageCounter = ( imageCounter + 1 ) % imagesD.length;
setImage( imagesD[imageCounter] );
break;
case 'U':
imageCounter = ( imageCounter + 1 ) % imagesU.length;
setImage( imagesU[imageCounter] );
break;
case 'L':
imageCounter = ( imageCounter + 1 ) % imagesL.length;
setImage( imagesL[imageCounter] );
break;
case 'R':
imageCounter = ( imageCounter + 1 ) % imagesR.length;
setImage( imagesR[imageCounter] );
break;
}
}
}


