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

2013/12/18

Scoreboard not updating and detecting shots fired

chrl98 chrl98

2013/12/18

#
Hi guys I'm creating a scoreboard with this code, but it is not working: (Note that this scenario is not my own, it has been downloaded from the gallery and modified as a school project) World code:

public class Terrain extends World
{
    public static Terrain main;
    
    private Point lastMouse = new Point(getWidth() / 2, getHeight() / 2);
    private GreenfootImage grass = new GreenfootImage("Grass.jpg");
    private Unit player;
    private int score = 0;
    
    //private Sector[][] sectors = new Sector[3][3];
    private Sector[] sectors = new Sector[5];
    
    private double screenX, screenY;
    private int saveIt;
    private Message saveMessage = new Message("Saving..");

    public Terrain()
    {
        //
        super(600, 400, 1, false);
        main = this;
        CodeKeyUser.enter();
        Greenfoot.start();
        addObject(new AmmoCounter(), 130, 30);
        AmmoCounter ammocounter = new AmmoCounter() ;
        addObject(new AmmoSign(), 60, 30);
        AmmoSign ammosign = new AmmoSign();
        addObject(new LiveCounter(), 120, 60);
        LiveCounter livecounter = new LiveCounter();
        addObject(new LiveSign(),60, 55);
        LiveSign livesign = new LiveSign() ;
        addObject(new CooldownClock(),175, 80);
        CooldownClock cooldownclock = new CooldownClock();
        addObject(new CDSign(), 80, 80);
        CDSign cdsign = new CDSign();
        addObject(new PlayButton(), 300, 140);
        PlayButton playbutton = new PlayButton();
        addObject(new CreditsButton(), 300, 280);
        CreditsButton creditsbutton = new CreditsButton();
        //addObject(new InstructionsButton(), 300, 210);
        //InstructionsButton instructionsbutton = new InstructionsButton();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getHeight()/2),Greenfoot.getRandomNumber(getWidth()/2));
        EnemyTruck enemytruck = new EnemyTruck();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getHeight()/3),Greenfoot.getRandomNumber(getWidth()/3));
        EnemyTruck enemytruck2 = new EnemyTruck();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getHeight()),Greenfoot.getRandomNumber(getWidth()));
        EnemyTruck enemytruck3 = new EnemyTruck();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getHeight()/2),Greenfoot.getRandomNumber(getWidth()/3));
        EnemyTruck enemytruck4 = new EnemyTruck();
        addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getHeight()),Greenfoot.getRandomNumber(getWidth()));

        //if (UserInfo.isStorageAvailable() && UserInfo.getMyInfo().getUserName().equals("bourne"))
        newGame();
        setBackground(pic);
    }
    private void prepare()
    {
    }
    private int spawnCounter = 0;
    {
        if (spawnCounter > 100)
        {
            spawnCounter = 0;
            addObject(new EnemyTruck(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        }
        spawnCounter++;
    }
    public void started()
    {
       //Greenfoot.playSound("Kryptic Minds - Six Degrees (Elysium Soundtrack) [Dubstep] [HD].mp3");
    }
    public AmmoCounter getAmmoCounter()
    {
        return AmmoCounter;
    }

AmmoCounter Code:

public class AmmoCounter extends Counter
{
    /**
     * Act - do whatever the AmmoCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public static final float FONT_SIZE = 30.0f;
    public static final int WIDTH = 200;
    public static final int HEIGHT = 300;
    private int totalCount = 150;    
    public AmmoCounter()
    {
        setImage(new GreenfootImage("150",30, Color.BLUE, Color.WHITE));
    }
    /**
     * Decrease the total amount displayed on the counter, by a given amount. 
     */
    public void dropCount (int amount)
    {
        totalCount -= amount;
        setImage(new GreenfootImage(""+ totalCount, 20, Color.WHITE, Color.BLACK));
    }
}

Projectile Code:

public class Projectile extends GameObject
{
    private int origX, origY;
    private double sX, sY;
    private final int speed = 11;
   
    private Projectile(int r)
    {
        setRotation(r);
        GreenfootImage pic = new GreenfootImage(5, 3);
        pic.fill();
        pic.setColor(Color.WHITE);
        pic.setColorAt(2, 1, Color.WHITE);
        setImage(pic);
    }
    public void started()
    {
        Greenfoot.playSound("machine-gun-01.mp3");
    }
    public Projectile(int r, double s, int r2)
    {
        this(r2);
        sX = Math.cos(Math.toRadians(r)) * s + Math.cos(Math.toRadians(r2)) * speed;
        sY = Math.sin(Math.toRadians(r)) * s + Math.sin(Math.toRadians(r2)) * speed;
         setRotation(r2);
         GreenfootImage pic = new GreenfootImage(5, 3);
         pic.fill();
         pic.setColor(Color.WHITE);
         pic.setColorAt(2, 1, Color.WHITE);
         setImage(pic);
    }
    public Projectile(int xM, int yM, double s, int r)
    {
        this(r);
        sX = xM * s + Math.cos(Math.toRadians(r)) * speed;
        sY = yM * s + Math.sin(Math.toRadians(r)) * speed;
    }
    public void act()
    {
        translate(sX, sY);
        if (!withinDistance(origX, origY, 1000))
            getWorld().removeObject(this);
        Actor projectile;
        projectile = getOneObjectAtOffset(0, 0, Soldier.class);
        if (projectile != null)
        {
            hitEdgeOfWorld();
            getWorld().removeObject(this);
        }
        else{
            getWorld().removeObject(this);
        }
    }
    private void hitEdgeOfWorld()
    {
        Projectile projectile = (Projectile) getOneIntersectingObject(Projectile.class);
        if (projectile != null){
            getWorld().removeObject(this);
    }
        Terrain terrainWorld = (Terrain) getWorld();   
        AmmoCounter counter = terrainWorld.getAmmoCounter();
        counter.dropCount(1);
    }
 
    public void EdgeOfWorld()
    {
        translate(sX, sY);
        if (!withinDistance(origX, origY, 1000))
            getWorld().removeObject(this);
        
    }
    public void addedToWorld(World world)
    {
        super.addedToWorld(world);
        origX = getX();
        origY = getY();
    }
}

chrl98 chrl98

2013/12/18

#
Line 74 of the world code is where I am getting the error. It says "cannot find symbol- variable AmmoCounter". I am trying to link the scoreboard to the Projectile class and the world so that it will update itself automatically and decrease by 1 each time a shot is fired, but I do not know how to fix the broken code. Thanks guys.
chrl98 chrl98

2013/12/18

#
Please reply ASAP, as time is of the essence.
bourne bourne

2013/12/18

#
There is no variable that exists with name AmmoCounter. public AmmoCounter getAmmoCounter() is expected to return an AmmoCounter Object, you say return AmmoCounter which is just a class name
chrl98 chrl98

2013/12/26

#
so how should i program it to return an AmmoCounter object?
danpost danpost

2013/12/26

#
You do not want just any AmmoCounter object; you want the one that you already have in your world. Therefore, you would need to use the World class method 'getObjects' to return a list of AmmoCounter objects in the world. Then check that list to make sure that it contains at least one element; and if so, get that element from the list. That would be the AmmoCounter object that needs to be returned in the 'getAmmoCounter' method.
chrl98 chrl98

2013/12/27

#
Understood. Thanks guys! :)
You need to login to post a reply.