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

2020/3/22

Need help with my java code

1
2
3
Ac951 Ac951

2020/3/22

#
oh sorry haha it says cannot find method addScore(int)
danpost danpost

2020/3/22

#
Ac951 wrote...
oh sorry haha it says cannot find method addScore(int)
That is because the method is named incScore.
Ac951 Ac951

2020/3/22

#
ok i did that and it compiles but the score doesnt go up lobster class public class Lobstrosity extends Actor { private int touching; private int count=1; /** * Act - do whatever the Lostrosity wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setRotation(90); move(1); if (isAtEdge()) { getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40); ((MyWorld)getWorld()).getScoreBoard().incScore(1); } if (isTouching(Shelter.class)==true) { ((Shelter)getOneIntersectingObject(Shelter.class)).reduceStrength(); getWorld().removeObject(this); } } } world public class MyWorld extends World { // YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!! int sheltersLeft = 3; /** * Constructor for objects of class MyWorld. */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); act(); } private void prepare() { Player player = new Player(); addObject(player, 400, 575); ScoreBoard scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40); } /** * getScoreBoard gives us the Scoreboard found in the world * NOTE ---> YOU ***MUST*** CODE THIS !!! * * @return the ScoreBoard object seen in the world */ public ScoreBoard getScoreBoard() { ScoreBoard scoreboard = new ScoreBoard(); return scoreboard; } public void act() { if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty()) { Player player = new Player(); addObject(player, 400, 575); ScoreBoard scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); Lobstrosity lobstrosity = new Lobstrosity(); addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40); } } /** * containsShelter tells us whether or not a shelter * is already in the world. * @return true if there is a shelter, false if not */ //public boolean containsShelter() //{ //} /** * containsBomb tells us whether or not a bomb * is already in the world. * @return true if there is a bomb, false if not */ public boolean containsBomb() { // fairly inelegant and inefficient way to code this // but avoids the need for code elsewhere. return (!getObjects(PoisonBomb.class).isEmpty()); } /** * lobstrosityCount tells us how man lobstrosities are * in the World. * @return the number of lobstrosities in the world */ public int lobstrosityCount() { return getObjects(Lobstrosity.class).size(); } }
danpost danpost

2020/3/23

#
danpost wrote...
the third line in the prepare method -- this one:
ScoreBoard scoreboard = new ScoreBoard();
needs to be moved to outside the method.
Also, in act, remove the first word i( ScoreBoard ) n the line creating a new score board.
Ac951 Ac951

2020/3/23

#
ok here is the edited version of my world public class MyWorld extends World { // YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!! int sheltersLeft = 3; /** * Constructor for objects of class MyWorld. */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); act(); ScoreBoard scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); } private void prepare() { Player player = new Player(); addObject(player, 400, 575); addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40); } /** * getScoreBoard gives us the Scoreboard found in the world * NOTE ---> YOU ***MUST*** CODE THIS !!! * * @return the ScoreBoard object seen in the world */ public ScoreBoard getScoreBoard() { ScoreBoard scoreboard = new ScoreBoard(); return scoreboard; } public void act() { if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty()) { Player player = new Player(); addObject(player, 400, 575); scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); Lobstrosity lobstrosity = new Lobstrosity(); addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40); } } /** * containsShelter tells us whether or not a shelter * is already in the world. * @return true if there is a shelter, false if not */ //public boolean containsShelter() //{ //} /** * containsBomb tells us whether or not a bomb * is already in the world. * @return true if there is a bomb, false if not */ public boolean containsBomb() { // fairly inelegant and inefficient way to code this // but avoids the need for code elsewhere. return (!getObjects(PoisonBomb.class).isEmpty()); } /** * lobstrosityCount tells us how man lobstrosities are * in the World. * @return the number of lobstrosities in the world */ public int lobstrosityCount() { return getObjects(Lobstrosity.class).size(); } }
Ac951 Ac951

2020/3/23

#
public void act() { MyWorld myWorld = (MyWorld) getWorld(); ScoreBoard score = myWorld.getScoreBoard(); if (Greenfoot.isKeyDown("left")) { move(-1); } if (Greenfoot.isKeyDown("right")) { move(1); } if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty()) { getWorld().removeObjects(getWorld().getObjects(null)); } if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null && placeShelt.hasHealth()) { placeShelt(); } /** * fire the PoisonBomb */ if ("b".equals(Greenfoot.getKey())) { if(Ammo>0) { PoisonBomb poisonbomb = new PoisonBomb(); getWorld().addObject(poisonbomb, getX(), getY()); Ammo--; score.decBombCount(); } else { } } } public void placeShelt() { getWorld().addObject(placeShelt, getX(), getY()-50); score.decShelterCount(); } } here is also my player class i made some edits to that too
danpost danpost

2020/3/23

#
danpost wrote...
the third line in the prepare method -- this one:
ScoreBoard scoreboard = new ScoreBoard();
needs to be moved to outside the method.
"outside the method" did not mean in some other block of code. That is, you now need to move it outside your constructor block.
Ac951 Ac951

2020/3/23

#
ok i think i did it this time sorry very new to greenfoot public class MyWorld extends World { // YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!! ScoreBoard scoreboard = new ScoreBoard(); int sheltersLeft = 3; /** * Constructor for objects of class MyWorld. */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); act(); } private void prepare() { Player player = new Player(); addObject(player, 400, 575); addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40); } /** * getScoreBoard gives us the Scoreboard found in the world * NOTE ---> YOU ***MUST*** CODE THIS !!! * * @return the ScoreBoard object seen in the world */ public ScoreBoard getScoreBoard() { ScoreBoard scoreboard = new ScoreBoard(); return scoreboard; } public void act() { if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty()) { Player player = new Player(); addObject(player, 400, 575); scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); Lobstrosity lobstrosity = new Lobstrosity(); addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40); } } /** * containsShelter tells us whether or not a shelter * is already in the world. * @return true if there is a shelter, false if not */ //public boolean containsShelter() //{ //} /** * containsBomb tells us whether or not a bomb * is already in the world. * @return true if there is a bomb, false if not */ public boolean containsBomb() { // fairly inelegant and inefficient way to code this // but avoids the need for code elsewhere. return (!getObjects(PoisonBomb.class).isEmpty()); } /** * lobstrosityCount tells us how man lobstrosities are * in the World. * @return the number of lobstrosities in the world */ public int lobstrosityCount() { return getObjects(Lobstrosity.class).size(); } }
danpost danpost

2020/3/23

#
Also:
danpost wrote...
the method in your MyWorld class should be just:
public ScoreBoard getScoreBoard()
{
    return scoreboard;
}
Ac951 Ac951

2020/3/23

#
ok i did that and the scoreboard only works when i press n to restart the game
danpost danpost

2020/3/23

#
Ac951 wrote...
ok i did that and the scoreboard only works when i press n to restart the game
You had a line to add the initial score board into the world previously (at one point, in your prepare method; and at another, in your constructor block). I can only believe that you removed the line (for some unknown reason).
Ac951 Ac951

2020/3/23

#
ok thanks i got that to work but when i press space for my shelters it gives me many errors it says java lang null pointer exceptions Player class public class Player extends Actor { private Shelter placeShelt= new Shelter(10); private int speedX; private int speedY; int Ammo = 10; int sheltersLeft = 3; int initialStrength=10; private ScoreBoard score = new ScoreBoard(); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { MyWorld myWorld = (MyWorld) getWorld(); ScoreBoard score = myWorld.getScoreBoard(); if (Greenfoot.isKeyDown("left")) { move(-1); } if (Greenfoot.isKeyDown("right")) { move(1); } if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty()) { getWorld().removeObjects(getWorld().getObjects(null)); } if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null && placeShelt.hasHealth()) { placeShelt(); } /** * fire the PoisonBomb */ if ("b".equals(Greenfoot.getKey())) { if(Ammo>0) { PoisonBomb poisonbomb = new PoisonBomb(); getWorld().addObject(poisonbomb, getX(), getY()); Ammo--; score.decBombCount(); } else { } } } public void placeShelt() { getWorld().addObject(placeShelt, getX(), getY()-50); score.decShelterCount(); } } Shelter class public class Shelter extends Actor { private int strengthLeft; // remaining strength private int strength = 10; // dimensions of the shelter object. Note that these are // private as there is no reason to utiize these outside // of this class. private static final int SHELTER_WIDTH = 225; private static final int SHELTER_HEIGHT = 25; public void act() { /** * Constructor for a shelter. * * @param initialStrength is the starting strength of * this shelter */ } public boolean hasHealth() { return strengthLeft > 0; } public Shelter(int initialStrength) { strengthLeft = initialStrength; // remember initial strength redraw(); // redraw the shelter } // redraw simply rebuilds the image for the shelter. // note that methods outside this class should not call // redraw, so it is private. private void redraw() { // (re)build image if necessary GreenfootImage img = getImage(); if (img==null || img.getWidth()!=SHELTER_WIDTH || img.getHeight()!=SHELTER_HEIGHT) { img = new GreenfootImage(SHELTER_WIDTH, SHELTER_HEIGHT); setImage(img); } // set background to light gray img.setColor(Color.LIGHT_GRAY); img.fill(); // add text repressenting the strength left in black img.setColor(Color.BLACK); img.drawString(""+ strengthLeft, SHELTER_WIDTH/2-10, SHELTER_HEIGHT-8); } /** * reduceStrngth reduces this shelter's strength by 1 unit */ public void reduceStrength() { reduceStrength(1); // use the other reduceStrength method } /** * reduceStrength reduces the strength of the shelter by a * requested amount. * @param byVal is the amount to reduce this shelter's strength */ public void reduceStrength(int byVal) { strengthLeft-=byVal; // reduce the strength // if shelter is still strong enough to function ... if (strengthLeft>0) redraw(); // ... redisplay the shelter else // shelter not strong enough to function getWorld().removeObject(this); // remove this shelter } } world public class MyWorld extends World { // YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!! ScoreBoard scoreboard = new ScoreBoard(); int sheltersLeft = 3; /** * Constructor for objects of class MyWorld. */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); act(); } private void prepare() { Player player = new Player(); addObject(player, 400, 575); addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40); } /** * getScoreBoard gives us the Scoreboard found in the world * NOTE ---> YOU ***MUST*** CODE THIS !!! * * @return the ScoreBoard object seen in the world */ public ScoreBoard getScoreBoard() { addObject(scoreboard,400, 10); return scoreboard; } public void act() { if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty()) { Player player = new Player(); addObject(player, 400, 575); scoreboard = new ScoreBoard(); addObject(scoreboard,400, 10); Lobstrosity lobstrosity = new Lobstrosity(); addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40); } } /** * containsShelter tells us whether or not a shelter * is already in the world. * @return true if there is a shelter, false if not */ //public boolean containsShelter() //{ //} /** * containsBomb tells us whether or not a bomb * is already in the world. * @return true if there is a bomb, false if not */ public boolean containsBomb() { // fairly inelegant and inefficient way to code this // but avoids the need for code elsewhere. return (!getObjects(PoisonBomb.class).isEmpty()); } /** * lobstrosityCount tells us how man lobstrosities are * in the World. * @return the number of lobstrosities in the world */ public int lobstrosityCount() { return getObjects(Lobstrosity.class).size(); } }
danpost danpost

2020/3/23

#
In Player class act, method insert line 2 after line 1 below:
getWorld().removeObjects(getWorld().getObjects(null));
return;
Ac951 Ac951

2020/3/23

#
ok i did that and i still get that error ver weird
danpost danpost

2020/3/23

#
Ac951 wrote...
ok i did that and i still get that error ver weird
Copy/paste complete error output here.
There are more replies on the next page.
1
2
3