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

2018/5/16

Need Help with Collision

stewe951 stewe951

2018/5/16

#
I am in the process of writing a game where you must avoid the obstacles and am having problems with actor collision working through my Hero class. I think that the problem is with the isTouchingEnemy method but do now know how to fix it. Please help!!!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hero here.
 * 
 * @author Andrew Fantino 
 * @version 5/6/18
 */
public class Hero extends SmoothMover implements Killable
{
    private int health;
    private  double speed;
    private World world;
    
    /**
     * this is the constructor for hero
     * 
     * @param world
     */
    public Hero (World world) {
        this.world = world;
        
        health = 3;
        speed = 5.0;
    }
    /**
     * act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        controlledMovement();
    }
    /**
     * controlledMovement - Allows the hero to be controlled and for next move to be 
     * qued with either arrows or wasd depending on what was used previously. This is 
     * a feature that will allow advance players to have greater control of the hero
     * and increases the skill cap.
     */
    private void controlledMovement(){
        if((Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) && 
               this.getExactX() >= 0){
            this.move(-speed);
        } else if ((Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) && 
               this.getExactX() <= 800) {
            this.move(speed);
        } else if ((Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) && 
               this.getExactY() >= 0) {
            this.setLocation(this.getExactX(), this.getExactY()-speed);
        } else if ((Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")) && 
               this.getExactY() <= 450) {
            this.setLocation(this.getExactX(), this.getExactY()+speed);
        }
    }
    /**
     * getHealth - gets the health of the instance of hero
     * 
     * @return health
     */
    public int getHealth() {
        return this.health;
    }
    /**
     * getSpeed - gets the speed of the instance of the hero
     * 
     * @return speed
     */
    public double getSpeed() {
        return this.speed;
    }
    /**
     * setSpeed - sets the speed of the instance of the hero
     * 
     * @param speed
     */
    public void setSpeed(double speed) {
        this.speed = speed;
    }
    /**
     * decreaseHealth - decreases health by param amount
     * 
     * @param amount
     */
    public int decreaseHealth(int amount) {
        return health-amount;
    }
    /**
     * increaseHealth - increases health by 1
     * 
     * @return health
     */
    public int increaseHealth() {
        return health+1;
    }
    /**
     * die - sets the world to the game over screen
     */
    public void die() {
        if (isDead()) {
            System.out.println("DEAD #2");
            Greenfoot.setWorld(new GameOver());
        }
    }
    /**
     * isDead() - checks if health is 0 or below
     * 
     * @return isDead
     */
    public boolean isDead() {
        if(this.getHealth() <= 0) {
            return true;
        } else {
            return false;
        }
    }
    public boolean isTouchingEnemy() {
        Actor e = getOneIntersectingObject(Enemy.class);
        
        if (e != null) {
            return true;
        } else {
            return false;
        }
    }
    public void takeDamage() {
        if (isTouchingEnemy()) {
            this.getWorld().getHealthBar().loseHealth(this);
            die();
        }
    }
    /**
     * getWorld - gets the world that the Hero is instanciated
     * in.
     * 
     * @return world
     */
    public MyWorld getWorld() {
        return (MyWorld)world;
    }
}
danpost danpost

2018/5/16

#
Maybe you should explain exactly how it is not working. Just saying it does not work is not much to go on. Describe the behavior you are getting and how it differs from what you want. As a side-note, I have looked over the code given above and do not see anything that strikes me as being "off" (not saying I particular like the way its coded, however). The only thing that might be an issue is continuous touching of an enemy may cause the health to plummet quickly.
stewe951 stewe951

2018/5/17

#
My problem was originally that I was not calling my takeDamage method in act() and it would not be checked every loop. Thank you for all the help though.
You need to login to post a reply.