This site requires JavaScript, please enable it in your browser!
Greenfoot back
ChuckTenton
ChuckTenton wrote ...

2014/1/12

AI Movement

ChuckTenton ChuckTenton

2014/1/12

#
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:
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;
        }
    }    
}
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 ;')
Gevater_Tod4711 Gevater_Tod4711

2014/1/12

#
I think the problem is that when there is an object abouve your AI object the else part from line 33 to line 37 is executed. So the object moves down and sets the variable tried to true. The next time the code is executed the if clause in line 27 is executed because the AI object moved down and so there is no object abouve it any longer. But because the variable Tried is true the next if clause is not executed and so nothing happens anymore. I think adding an else part to the if clause in line 28 that makes the AI object move down would fix this problem.
danpost danpost

2014/1/12

#
Or combining the two if conditions of lines 27 and 28 using '&&' and still use the 'else' part of the second 'if' on that. As long as there is always a way to go left by going down when up is blocked, this should work.
ChuckTenton ChuckTenton

2014/1/12

#
Alright I gave this code a spin
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;
            AIX = getX();
            AIY = getY();
        }
        else{
            if (getOneObjectAtOffset(0, -(getImage().getHeight()/2)-4, Obstacle.class) == null && Tried != true){
                    setLocation(AIX, AIY-4);
                    dir = 'U';
                    AIX = getX();
                    AIY = getY();
            }
            else{
                setLocation(AIX, AIY+4);
                dir = 'D';
                Tried = true;
                AIX = getX();
                AIY = getY();
            }
        }
    }
And it works! Thanks alot guys :] And I will continue posting on this thread if I come across any more problems I'm stumped with, instead of starting a new one everytime.
You need to login to post a reply.