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

2013/5/11

Collision Detection

lgbrf lgbrf

2013/5/11

#
I'm having an issue with getting my zombie class to follow the character when he sees him. The zombie isn't registering either of the variable as true, even when it seems he is within the correct distance.
public void moveTowardsCharacter(){
        if(getOneObjectAtOffset(50, 0, Platform.class) != null){
            foundCharacterR = true;
        }else{
            foundCharacterR = false;
        }
        if(getObjectsAtOffset(-50, 0, Character.class) != null){
            foundCharacterL = true;
        }else{
            foundCharacterL = false;
        }
    }
In a different method-
if(foundCharacterL || foundCharacterR){
            if(foundCharacterL){
                setLocation (getX() - 4, getY());
            }
            if(foundCharacterR){
                setLocation (getX() + 4, getY());
            }
danpost danpost

2013/5/11

#
Looks like you are looking for a Platform object on the right and looking for a Character object on the left. I may be mistaken, but you may not want to be looking for a Platform object at all, there. In your second block of code, you can remove the outer 'if' condition, which is individually taken care of from within the block.
lgbrf lgbrf

2013/5/11

#
You're right - I changed the Platform.class to Character.class, but the zombie still didn't detect the character. The reason for the external if statement is that there is also an else statement which defines the movement of the zombie when it is not in range of the character.
danpost danpost

2013/5/11

#
lgbrf wrote...
The reason for the external if statement is that there is also an else statement which defines the movement of the zombie when it is not in range of the character.
If you character is not in range then the conditions for each inner 'if' statement will be both false.
danpost danpost

2013/5/11

#
Maybe you are looking to close to your character (50 pixels from the center of your character minus half the width of your character would be very close to intersecting). Widen the range some to see if it is actually detecting or not. I would code it more like the following:
public void moveTowardsCharacter() {
    if (!getObjectsInRange(150, Character.class).isEmpty()) {
        Actor actor = getObjectsInRange(150, Character.class).get(0);
        foundCharacterR = actor.getX()-getX() > 0;
        foundCharacterL = actor.getX()-getX() < 0;
    }
}
lgbrf lgbrf

2013/5/11

#
Still isn't working, maybe i copied over the code wrong. My entire zombie class, maybe something somewhere else is messing with it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    boolean foundCharacterL = false;
    boolean foundCharacterR = false;
    boolean hitCharacter = false;
    int i = 0;
    int j = 0;
    int randomI = Greenfoot.getRandomNumber(50) + 10;
    int randomJ = Greenfoot.getRandomNumber(50) + 10;
    boolean toggle = true;
    GreenfootImage right = new GreenfootImage("zsr.png");
    GreenfootImage left =  new GreenfootImage("zsl.png");
    GreenfootImage mRight = new GreenfootImage("zmr.png");
    GreenfootImage mLeft = new GreenfootImage("zml.png");
    int imCounter = 0;
    public void act() 
    {
        move();
        attackCharacter();
        hitCharacter();
    }    

    public boolean hitCharacter()
    {
        if(hitCharacter){
            return true;
        }
        else{
            return false;
        }
    }

    public void move(){
        imCounter++;
        if (imCounter==17)
        {
            imCounter = 0;
        }
        if(foundCharacterL || foundCharacterR){
            if(foundCharacterL){
                setLocation (getX() - 4, getY());
            }
            if(foundCharacterR){
                setLocation (getX() + 4, getY());
            }
        }else{
            if (toggle){
                if(i < randomI){
                    setLocation(getX() - 4, getY());
                    i++;

                }
                else{
                    i=0;
                    j=0;
                    randomI = Greenfoot.getRandomNumber(50) + 10;
                    toggle = false;
                }
                if (imCounter <=8){
                    setImage(mLeft);
                }
                else{
                    setImage(left);
                }
            }
            if(!toggle){
                if(j < randomJ){
                    setLocation(getX() + 4, getY());
                    j++;
                }
                else{
                    i=0;
                    j=0;
                    randomJ = Greenfoot.getRandomNumber(50) + 10;
                    toggle = true;
                }
                if (imCounter <=8){
                    setImage(mRight);
                }
                else{
                    setImage(right);
                }
            }
        }
        // The issue below was that it was not moving 1 pixel per act sequence,
        // but the whole movement in one sequence (very erratic).
        /*     while(j < randomJ){
        setLocation(getX() + 1, getY());
        j++;
        }*/
        if(!onGround()){
            setLocation(getX(), getY() + 10);
        }
    }

    public boolean onGround(){
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class); 
        if(under != null){
            setLocation(getX(), under.getY() - under.getImage().getHeight()/2 - getImage().getHeight()/2);
        }
        return under != null;
    }

    public void moveTowardsCharacter(){
        //         if(getOneObjectAtOffset(50, 0, Character.class) != null){
        //             foundCharacterR = true;
        //         }else{
        //             foundCharacterR = false;
        //         }
        //         if(getObjectsAtOffset(-50, 0, Character.class) != null){
        //             foundCharacterL = true;
        //         }else{
        //             foundCharacterL = false;
        //         }
        if (!getObjectsInRange(150, Character.class).isEmpty()) {  
            Actor actor = (Character)getObjectsInRange(150, Character.class).get(0);  
            foundCharacterR = actor.getX()-getX() > 0;  
            foundCharacterL = actor.getX()-getX() < 0;  
        }  
    }

    public void attackCharacter(){

    }
}
danpost danpost

2013/5/12

#
Just quickly looking over the code, the only thing I saw was that line 74 should probably be 'else {'.
lgbrf lgbrf

2013/5/12

#
I changed that, but it still doesn't work. the booleans stay at false the entire time.
danpost danpost

2013/5/12

#
lgbrf wrote...
I changed that, but it still doesn't work. the booleans stay at false the entire time.
I did not say it would fix it; I just noticed that there was a bug there.
danpost danpost

2013/5/12

#
Here is what I see: your act method has three method calls in it. First a call to 'move' (all well and good). Then, a call to attackCharacter, which is an empty method. Finally, a call to 'hitCharacter', which returns the value of a boolean field. This boolean value is not used in any way and the 'moveTowardCharacter' is never called (anywhere).
You need to login to post a reply.