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

2017/10/13

Help with collision detection.

Beamo Beamo

2017/10/13

#
So I have two yes inside a character that follow the enemy below them, but they would fly outside of the face if the enemy was outside of the face, so I added barriers to stop that from happening. However, it doesn't work and I can't figure out why. Here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class eyes here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class eyes extends TheFace
{
    /**
     * Act - do whatever the eyes wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        follow();
        checkForBarrierOne();
    }    
    public void follow(){
        startenemy startenemy = new startenemy();
        List objectslookingfor = getWorld().getObjects(startenemy.class);
        if (objectslookingfor.size() > 0){
            int enemyx = ((startenemy) getWorld().getObjects(startenemy.class).get(0)).getX();
            if(checkForBarrierOne()==false||checkForBarrierTwo()==false||checkForBarrierThree()==false||checkForBarrierFour()==false){
                if(getX()<enemyx){
                    setLocation(getX()+1,getY());
                }
                if(getX()>enemyx){
                    setLocation(getX()-1,getY());
                }
            }
            else if(checkForBarrierOne()==true&&getX()<enemyx){
                if(getX()<enemyx){
                    setLocation(getX()+1,getY());
                }
            }
            else if(checkForBarrierTwo()==true&&getX()>enemyx){
                if(getX()<enemyx){
                    setLocation(getX()-1,getY());
                }
            }
            else if(checkForBarrierThree()==true&&getX()<enemyx){
                if(getX()<enemyx){
                    setLocation(getX()+1,getY());
                }
            }
            else if(checkForBarrierFour()==true&&getX()>enemyx){
                if(getX()<enemyx){
                    setLocation(getX()-1,getY());
                }
            }
        }
    }
    private boolean checkForBarrierOne(){
        boolean check;
        if(getOneIntersectingObject(barrierOne.class)!=null){
            check = true;
        }
        else{check=false;}
        return check;
    }
    private boolean checkForBarrierTwo(){
        boolean check;
        if(getOneIntersectingObject(barrierTwo.class)!=null){
            check = true;
        }
        else{check=false;}
        return check;
    }
    private boolean checkForBarrierThree(){
        boolean check;
        if(getOneIntersectingObject(barrierThree.class)!=null){
            check = true;
        }
        else{check=false;}
        return check;
    }
    private boolean checkForBarrierFour(){
        boolean check;
        if(getOneIntersectingObject(barrierFour.class)!=null){
            check = true;
        }
        else{check=false;}
        return check;
    }
}
Thanks for your time! :)
Super_Hippo Super_Hippo

2017/10/13

#
I think you should control the eyes from the face. Then it wouldn't be that complicated.
danpost danpost

2017/10/13

#
The biggest problem here is that an eyes object is NOT a TheFace object. The eyes may be a part of, or belong to, a face; but, the eyes are not a face. Only subclass to give more specificity to a type of object. Do not subclass to describe only a part of an object. For example, a car, a truck and a bus are all vehicles; but headlights, brakes and an engine are not. If you want to show ownership of the eyes to a face, place the eyes class within the class of the TheFace class. All eyes objects will then belong to one specific TheFace object and the eyes can refer to its owner as 'TheFace.this'.
You need to login to post a reply.