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

2014/5/9

Score counter not working.

1
2
Bntyhntr501 Bntyhntr501

2014/5/9

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MetalWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
    
    
    
    /**
     * Constructor for objects of class MetalWorld.
     * 
     */
    public SpaceWorld()
    {    
        
        super(800, 800, 1); 
        
        populateWorld();
        
        
    }//end SpaceWorld
    
    
    public void populateWorld()
    {
        Counter counter = new Counter("Score: ");
        addObject(counter, 50, 790);
        
        
        
        
        Redship redship = new Redship(counter);
        addObject(redship, 400, 400);
        
        
        
        
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
       
        
        addObject(new Goldasteroid(),Greenfoot.getRandomNumber(650), Greenfoot.getRandomNumber(650));
        


        
    }//end populateWorld
    
    
    public Counter getCounter()  
    {  
        return (Counter)geObjects(Counter.class).get(0);  
    } 
    



}//end World
danpost danpost

2014/5/9

#
Ok. It looks like you placed it properly in the world class. Now show the Alienship class again (as it is now).
Bntyhntr501 Bntyhntr501

2014/5/9

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Alienship here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alienship extends Aliens
{
    private int health;
    private int stability;
    private int spawnTimer = 10;
    

    

    /**
     * Act - do whatever the Alienship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turnAtEdge();
        randomTurn();
        move(2);
        lookForRedship();
        
        runSpawnTimer();
        
    }//end act
    
    public void healthLevel()
    {
        health = 1;
        stability = health;
        this.health = health;
    }//end health
    
    private void runSpawnTimer()
    {
        spawnTimer = (spawnTimer+1)%400;
        if(spawnTimer == 0) spawnAlienship();
    }//end runSpawnTimer
    
    private void spawnAlienship()
    {
        getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
    }//end spawnAlienship
    
    
    public void turnAtEdge()
    {
        if(atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(10));
        }//end if
    }//end turnAtEdge
    
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100) > 90)
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }//end if
    }//end randomTurn
    
    
    
    public void lookForRedship()
    {
        if(canSee(Redship.class))
        {
            destroy(Redship.class);
            getWorld().addObject(new ScoreBoard(((SpaceWorld)getWorld().getCounter().getValue()), 400, 400));
            Greenfoot.playSound("Blast.wav");
            Greenfoot.stop();
        }//end if
    }//end lookForRedship
    
        public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
            
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
        //end if/else
    }//end atWorldEdge
    
    public int getStability() 
    {
        return stability;
    }
    
    public void hit(int damage)
    {
        stability = stability - damage;
        if(stability <= 0) 
            destroy ();         
    }//end hit
    
    public void destroy()
    {
        if(health <= 0)
       {
         ((SpaceWorld)getWorld()).getCounter().add(10);
         getWorld().removeObject(this);
        
       }//end if

    }//end destory
    

    

}//end class
danpost danpost

2014/5/9

#
Replace line 75 with this:
getWorld().addObject(new ScoreBoard(((SpaceWorld)getWorld()).getCounter().getValue()), 400, 400));
there was a closing parenthesis missing after '((SpaceWorld)getWorld()'.
Bntyhntr501 Bntyhntr501

2014/5/9

#
Awesome it works great now, thank you so much for your time and help.
danpost danpost

2014/5/9

#
Best regards.
You need to login to post a reply.
1
2