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

2019/4/12

Powerup Problem

1
2
3
danpost danpost

2019/4/13

#
AdiBak wrote...
I understand, but how would I ask the question if it's null?
I am not sure what you mean. Maybe you do not understand. The if statement I gave should properly restrict the asking of the question to (1) not yet asked; and (2) points greater than 6000.
danpost danpost

2019/4/14

#
Another thing is this -- you cannot use '==' to compare contents of String objects. That will only check to see if they are one in the same object. To compare the contents, use the equals method:
if ("normal".equals(question))
AdiBak AdiBak

2019/4/14

#
Can you please provide an example block of code?
Super_Hippo Super_Hippo

2019/4/14

#
He provided code for everything he mentioned.
AdiBak AdiBak

2019/4/14

#
I meant if you could please show how I can use this in my code, since I'm not sure how...
Super_Hippo Super_Hippo

2019/4/14

#
I don't know what was unclear…
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);
    }
}
AdiBak AdiBak

2019/4/14

#
I tried the above code, but upon clicking "ok", the prompt still wouldn't go away.
Super_Hippo Super_Hippo

2019/4/14

#
Show the full code of the class.
AdiBak AdiBak

2019/4/14

#
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);
        }
    }

}
Super_Hippo Super_Hippo

2019/4/15

#
i started with "private":
private String question = null;
This should tell you that you should move this line outside methods, so for example in line 12 (around there).
AdiBak AdiBak

2019/4/15

#
Here's why there are State classes (please view): https://docs.google.com/presentation/d/1kg-rlnzxauaekj9QWt39jZQDunYA0vUAcHeNeu0HN9w/edit?usp=sharing
Super_Hippo Super_Hippo

2019/4/15

#
Yeah, it is still totally useless, but if it is your task to do it like that, you don't have a choice, I guess. Does it work now?
AdiBak AdiBak

2019/4/15

#
Yes, it worked, thanks!
AdiBak AdiBak

2019/4/15

#
I have another question -- how can I make it so that whenever the user presses a certain key, say, "control", that question pops up and he/she gets to choose the powerup?
danpost danpost

2019/4/15

#
AdiBak wrote...
how can I make it so that whenever the user presses a certain key, say, "control", that question pops up and he/she gets to choose the powerup?
Use:
if ((question == null || Greenfoot.isKeyDown("control")) && getScore() > 6000)
There are more replies on the next page.
1
2
3