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

2014/10/10

World constructor

wslade wslade

2014/10/10

#
I am trying to use a score in this game. Why is it when my world code is this (see below) it doesn't work but if I change it to this code (showing after bold heading) it does?
public class Space extends World
{
    public Space()
    {

        super(800, 600, 1);
        Color black = new Color(0,0,0);
        GreenfootImage background = getBackground();
        background.setColor(black);
        background.fill();
        createStars(300);
        setPaintOrder(Player.class, Missile.class, GameOver.class, Explosion.class);
        spawnPlayer();
        spawnGunner();
        spawnGunner();
        spawnKamikaze();
        spawnScore();
    }

    private void createStars(int number)
    /**
     * adds stars randomly to world
     */
    {
        {
            for (int i = 0; i < number; i++)
            {
                int x = Greenfoot.getRandomNumber( getWidth() );
                int y = Greenfoot.getRandomNumber( getHeight() );
                addObject(new Star(), x, y);
            }
        } 
    }

    public void spawnPlayer()
    /**
     * sets position of spawn for player
     */
    {
        addObject(new Player(), 50, 300); 
    }

    public void spawnGunner()

    {
        /**
         * spawn Gunner class on right side of screen at random y-value
         */               
        addObject(new Gunner(score_field), 700, Greenfoot.getRandomNumber(getHeight()));     
    }

    public void spawnKamikaze()

    {
        /**
         * spawn Kamikaze class on right side of screen at random y-value
         */
        addObject(new Kamikaze(score_field), 700, Greenfoot.getRandomNumber(getHeight()));
    }
    
    public void spawnScore()
    {        
        /**
         * spawn counter in to-left corner, sets reference for counter
         */
        Score score_field = new Score();
      	addObject(score_field, 35, 20);  
    }
     
      
}
New code
public class Space extends World
{
    public Space()
    /**
     * sets background to solid black color, has 300 stars on screen at once, spawns ship and enemies,
     * keeps missiles under ship and explosions under everything except enemies, spawn counter
     */
    {

        super(800, 600, 1);
        Color black = new Color(0,0,0);
        GreenfootImage background = getBackground();
        background.setColor(black);
        background.fill();
        createStars(300);
        setPaintOrder(Player.class, Missile.class, GameOver.class, Explosion.class);
        Score score_field = new Score();
      	addObject(score_field, 35, 20);  
        addObject(new Player(), 50, 300); 
      addObject(new Gunner(score_field), 700, Greenfoot.getRandomNumber(getHeight()));  
      addObject(new Gunner(score_field), 700, Greenfoot.getRandomNumber(getHeight())); 
               addObject(new Kamikaze(score_field), 700, Greenfoot.getRandomNumber(getHeight()));

    }

    private void createStars(int number)
    /**
     * adds stars randomly to world
     */
    {
        {
            for (int i = 0; i < number; i++)
            {
                int x = Greenfoot.getRandomNumber( getWidth() );
                int y = Greenfoot.getRandomNumber( getHeight() );
                addObject(new Star(), x, y);
            }
        } 
    }
}
davmac davmac

2014/10/10

#
By "doesn't work" you actually mean "doesn't compile", right? One problem is that you declare score_field as a method-local variable (in the "spawnScore" method, on line 66), but you also try to access it in other methods (such as "spawnGunner"). You can't access variables that are local to one method from another method.
wslade wslade

2014/10/10

#
Oh, okay. Thanks. That makes sense :) It does compile but when I try to run the scenario then I get error messages and the score won't increase.
danpost danpost

2014/10/10

#
What error message are you getting and what does your revised code look like?
davmac davmac

2014/10/10

#
It does compile
It couldn't possibly compile, for the reasons I gave above - the score_field variable is not accessible from the method in which you use it. Are you now talking about different code (perhaps the second variant that you posted originally)?
You need to login to post a reply.