AdiBak wrote...
I understand, but how would I ask the question if it's null?if ("normal".equals(question))
private String question = null; //... if (question == null && w.getScore() > 6000) { w.setPower(false); question = Greenfoot.ask("What powerup would you like (normal, points, power)?"); if ("normal".equals(question)) { w.setNormal(true); } else if ("points".equals(question)) { w.setPointPowerup(true); } else if ("power".equals(question)) { w.setPower(true); } }
import greenfoot.*; import java.util.List; /** * Write a description of class GameState here. * * @author (your name) * @version (a version number or a date) */ public class GameState extends State { MyWorld w = ((MyWorld)getWorld()); public int yPosition = getWorld().getHeight() * 4/5; Score j = new Score(); GreenfootSound backMusic = new GreenfootSound("YouAreCreed.wav"); private int numSegments = 10; private int numToDestroyForFlea = 10; private int numToDestroyForScorpion = 15; public GameState(MyWorld w){ super(w); backMusic.playLoop(); } public void onSet(){ MyWorld q = ((MyWorld)getWorld()); q.setLvl(1); w.setGameOver(false); Player player = new Player(); getWorld().addObject(player, getWorld().getWidth()/2, getWorld().getHeight() - player.getImage().getHeight()/2); Spider s = new Spider(); getWorld().addObject(s, s.getImage().getWidth()/2, getWorld().getHeight() - s.getImage().getHeight()/2); for (int i = 0; i < 10; i++){ Centipede centipede = new Centipede(); getWorld().addObject(centipede, getWorld().getWidth()/2 - centipede.getImage().getWidth() * i, centipede.getImage().getHeight()/2); } for (int i = 0; i < 42; i++){ int x = Greenfoot.getRandomNumber(getWorld().getWidth()); int y = Greenfoot.getRandomNumber(getWorld().getHeight()); Mushroom mushroom = new Mushroom(); if (getWorld().getObjectsAt(x, y, Mushroom.class).size() == 0 && (y >= 20 && y <= getWorld().getHeight() - player.getImage().getHeight())){ getWorld().addObject(mushroom, x, y); mushroom.goToGrid(); } } j.setText(w.getScore()); j.setSize(MyWorld.GRIDSIZE); j.setOutLColor(null); getWorld().addObject(j, j.getImage().getWidth(), j.getImage().getHeight()/2); } public void addFlea(){ for (int i = 0; i < 1; i++){ Flea f = new Flea(); getWorld().addObject(f, Greenfoot.getRandomNumber(getWorld().getWidth()), -f.getImage().getHeight()); } } public void addScorpion(){ for (int i = 0; i < 1; i++){ //if (w.getScore() > 0 && w.getScore() <= ){ Scorpion sco = new Scorpion(); getWorld().addObject(sco, 0, Greenfoot.getRandomNumber(getWorld().getHeight()/3)); //} /*if (w.getLvl() > 3 && w.getLvl() <= 5){ Arachnoid a = new Arachnoid(); getWorld().addObject(a, 0, Greenfoot.getRandomNumber(getWorld().getHeight()/4)); }*/ } } public void snapAllToGrid(){ List<Mushroom> plant = getWorld().getObjects(Mushroom.class); for (int i = 0; i < plant.size(); i++){ Mushroom fungi = plant.get(i); fungi.goToGrid(); } } public void onRemove(){ // Remove anything when state is changed List <Actor> all = getWorld().getObjects(Actor.class); getWorld().removeObjects(all); } public void checkFleas(){ if (w.getDestroyed() == numToDestroyForFlea){ numToDestroyForFlea += 10; addFlea(); } } public void checkScorpions(){ if (w.getDestroyed() == numToDestroyForScorpion){ numToDestroyForScorpion += 15; addScorpion(); } } public void onAct(){ // Do what happpens in the state. Check if needs to be changed if (getWorld() != null){ checkFleas(); checkScorpions(); } if (getWorld().getObjects(Centipede.class).isEmpty()){ Greenfoot.delay(15); w.setLvl(w.getLvl() + 1); for (int i = 0; i < 10; i++){ Centipede centipede = new Centipede(); //centipede.setDx(centipede.getDx() + 1); getWorld().addObject(centipede, getWorld().getWidth()/2 - centipede.getImage().getWidth() * i, centipede.getImage().getHeight()/2); } Centipede c = new Centipede(); getWorld().addObject(c, getWorld().getWidth() - c.getImage().getWidth() * 2, c.getImage().getHeight()/2); List<Mushroom> cacti = getWorld().getObjects(Mushroom.class); for (int i = 0; i < cacti.size(); i++){ Mushroom food = cacti.get(i); food.getImage().setTransparency(255); food.goToGrid(); } Spider s = new Spider(); getWorld().addObject(s, s.getImage().getWidth()/2, getWorld().getHeight() - s.getImage().getHeight()/2); if (getWorld() != null){ checkFleas(); checkScorpions(); } } if (w.getScore() > 0 && w.getScore() <= 2000){ w.setNormal(true); } if (w.getScore() > 2000 && w.getScore() <= 4000){ w.setNormal(false); w.setPointPowerup(true); } if (w.getScore() > 4000 && w.getScore() <= 6000){ w.setPointPowerup(false); w.setPower(true); } String question = null; //... if (question == null && w.getScore() > 6000) { w.setPower(false); question = Greenfoot.ask("What powerup would you like (normal, points, power)?"); if ("normal".equals(question)) { w.setNormal(true); } else if ("points".equals(question)) { w.setPointPowerup(true); } else if ("power".equals(question)) { w.setPower(true); } } if (getWorld().getGameOver() == true){ if (backMusic.isPlaying()){ backMusic.stop(); } Score txt = w.getObjects(Score.class).get(0); w.setScore(w.getScore() - w.getScore()); txt.setText("Score: " + w.getScore()); getWorld().setState(new GameOverState(getWorld())); w.setLvl(1); } } }
private String question = null;