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

2013/12/26

cannot find symbol method canSee

chrl98 chrl98

2013/12/26

#
Hi, I'm encountering an error "cannot find symbol method canSee in the shotsFired method. *Note: This is not my project, I am modifying it for school-related purposes.*
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Point;

/**
 * Write a description of class Soldier here.
 * 
 * @author Taylor Born
 * @version May 2012
 */
public class Soldier extends Unit
{
    private static final int ROTATION_SPEED = 3;
    private static final double ACCELERATION = .15;
    private static final double MAX_SPEED = 2.5;
    public int shotsFired=0;
    protected void handlePlayer()
    {
        super.handlePlayer();
        speed = MAX_SPEED;
        if (Greenfoot.isKeyDown("w"))
            move(270, false);
        else if (Greenfoot.isKeyDown("s"))
            move(90, false);
        if (Greenfoot.isKeyDown("a"))
            move(180, false);
        else if (Greenfoot.isKeyDown("d"))
            move(0, false);
        speed = 0;
        Point p = ((Terrain)getWorld()).getLastMouse();
        turnTowards((int)p.getX(), (int)p.getY());
        
        if (KeysPressed.testKey("r"))
        {
            Vehicle v = (Vehicle)getOneIntersectingObject(Vehicle.class);
            if (v != null)
                ((Terrain)getWorld()).enterVehicle(v);
        }
    }
    protected void fire()
    {
        if (isPlayer())
            getWorld().addObject(new Projectile(Greenfoot.isKeyDown("a") ? -1 : Greenfoot.isKeyDown("d") ? 1 : 0, Greenfoot.isKeyDown("w") ? -1 : Greenfoot.isKeyDown("s") ? 1 : 0, MAX_SPEED, getRotation()), getX() + (int)(Math.cos(Math.toRadians(getRotation())) * (-14 + 26) + Math.cos(Math.toRadians(getRotation() + 90)) * (-14 + 19)), getY() + (int)(Math.sin(Math.toRadians(getRotation() + 90)) * (-14 + 19) + Math.sin(Math.toRadians(getRotation())) * (-14 + 26)));
        else
            getWorld().addObject(new Projectile(getRotation(), speed, getRotation()), getX() + (int)(Math.cos(Math.toRadians(getRotation())) * (-14 + 26) + Math.cos(Math.toRadians(getRotation() + 90)) * (-14 + 19)), getY() + (int)(Math.sin(Math.toRadians(getRotation() + 90)) * (-14 + 19) + Math.sin(Math.toRadians(getRotation())) * (-14 + 26)));
    }
    protected void slowDown()
    {
        if (Math.abs(speed) < .1)
            speed = 0;
        else if (speed > 0)
            speed -= speed / 10;
        else if (speed < 0)
            speed += -speed / 10;
    }
    protected boolean canTurnWhenStill()
    {
        return true;
    }
    protected int getRotationSpeed()
    {
        return ROTATION_SPEED;
    }
    protected double getAcceleration()
    {
        return ACCELERATION;
    }
    protected double getMaxSpeed()
    {
        return MAX_SPEED;
    }
    protected boolean hitsSomething()
    {
        return super.hitsSomething() || hitsFence() || hitsTree() || hitsLandUnits();
    }
    public void shotsFired()
    {
        if (canSee (Projectile.class) )
        {
            shotsFired++;
            System.out.println (shotsFired);
        }
    }
}
chrl98 chrl98

2013/12/26

#
Line 77 is where i'm getting an error, and I'm learning to create a scoreboard where it will detect the number of shots fired and decrease the scoreboard by a value of 1 accordingly.
gnvrajareddy gnvrajareddy

2013/12/26

#
canSee method is not present Soldier and Unit class. You can call this method provided you extend your class to the class that posses canSee() method.
chrl98 chrl98

2013/12/26

#
@gnvrajareddy how should I incorporate the method in my Soldier and Unit class?
gnvrajareddy gnvrajareddy

2013/12/26

#
Import the Animal class that has canSee method from edit menu. Extend the Unit class to Animal so that Unit class becomes the Subclass. Hence the Unit class and it's subclasses can use the canSee method present in the Animal class.
chrl98 chrl98

2013/12/27

#
Thanks :)
chrl98 chrl98

2013/12/27

#
Sorry but can you show me how to do that, because I am having trouble with the process of extending the unit class to animal.
danpost danpost

2013/12/27

#
It probably would be easiest for you to just copy/paste the method from the Animal class into your Soldier or Unit class.
gnvrajareddy gnvrajareddy

2013/12/28

#
Open the Unit class code editor and just modify the starting line of the class as public class Unit extends Animal
You need to login to post a reply.