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

2017/5/12

Moving a Counter to a different spot on screen

WhiteRoseAshes WhiteRoseAshes

2017/5/12

#
In my game I'm trying to make it where the counter will move to the center of the screen once the game is over so it is next to the "Score:" text on screen. Every time I try to use .setLocation on the Counter the program stops running. It is also doing the same thing if I try to remove the Counter from the world. These are the two codes that I am working with. I am going to add a link to my scenario soon. EDIT:here is the link
    public void act() 
    {

        int xpos = getX() - 5;
        if (xpos <= 0) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            setLocation(xpos, getY());
            Actor player = getOneObjectAtOffset(0,0,RocketPlayer.class);
            if (player != null)
            {
                getWorld().removeObjects(getWorld().getObjects(RocketPlayer.class));
                getWorld().removeObjects(getWorld().getObjects(Fuel.class));
                getWorld().setBackground(new GreenfootImage("gameOver.jpg"));
                getWorld().removeObjects(getWorld().getObjects(Astroid.class));
                getWorld().removeObjects(getWorld().getObjects(Counter.class));
                Greenfoot.delay(250);
                Greenfoot.setWorld(new MainMenu());                
            }
        }

    }
    public AstroidWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        player = new RocketPlayer();
        gameTime = new Timer();
        counter = new Counter();
        addObject(player,30,200);
        addObject(gameTime,550,20);
        addObject(counter,20,350);
    }
    public void act()
    {
        if (Greenfoot.getRandomNumber(200)<5)
        {
            addObject (new Astroid(), 560, Greenfoot.getRandomNumber(400));
        }
        if (player.getWorld() != null)
        {
            gameTimer = (gameTimer+1)%60;
            if (gameTimer == 0)
                gameTime.increaseTime();
        }
        if (Greenfoot.getRandomNumber(200)<5)
        {
            addObject (new Fuel(), 560, Greenfoot.getRandomNumber(400));
        }
        if ( player.getWorld() == null)
        {
            counter.setLocation(100,200);
        }
    }
    public Counter getCounter()
    {
        return counter;
    }
    public void moveCounter()
    {
        counter.setLocation(200,100);
    }
}
danpost danpost

2017/5/12

#
In the Astroid class, put this line:
World world = getWorld();
as the first line in the
if (player != null)
block. Then, change all subsequent 'getWorld()' to 'world'. Problem was after removing all astroids from world (which includes the one that hits the RocketPlayer object), 'this' no longer has a world to get.
WhiteRoseAshes WhiteRoseAshes

2017/5/16

#
Once I entered that code I now get a NullPointerException every time it gets to this segment of code:
world.removeObjects(getWorld().getObjects(Counter.class));
danpost danpost

2017/5/16

#
Change the line to:
world.removeObjects(world.getObjects(Counter.class));
You need to login to post a reply.