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

2017/4/19

Progress with my pong game, but new question about boolean/static methods

VinRio VinRio

2017/4/19

#
So i got the physics of my game working, but now my problem is trying to make the game roll over the levels when you beat them. I have three stages that each have a faster/harder AI to play. My problem is calling the score to be able to determine when the world should switch. I have been trying to use static methods to call a boolean result that compares the current score to the endscore. I am hitting a problem with my syntax though and cant figure out the exact thing to put in.
 public void beatStageOne()
    {
        if(counterRight.checkScore() =true)
        {
            Greenfoot.setWorld(new StageTwo());
        }
    }

I have three stages so ill just post the important bits that aren't redundant. This pat of the code is supposed to check my counter to see if requirements are met.
public static boolean checkScore()
    {
        return score ==endScore;
    }
This is my method for determining the requirements. I have my Stage worlds sub classed to a world called "ComputerLevels". So I put the method to change the stage in the ComputerLevels world and then call it from the actual stage. I can post any extra code that would help, thanks for the help.
Nosson1459 Nosson1459

2017/4/19

#
first on line 3 u only have I '=' I think u shud hav 2. and cud you post the code 4 the whole class it would make things easier (check if your variables are static because you cant use a non static variable in a static method)
Yehuda Yehuda

2017/4/19

#
Nosson1459 wrote...
(check if your variables are static because you cant use a non static variable in a static method)
Basically, your checkScore() method should not be static. I'm assuming you made it static because when it wasn't, and you tried to call the method you got an error. You shouldn't be using the class name to call this method, you need an instance of it. It's hard to point you in the right direction, since all you gave is two little pieces of code, you didn't say where they're from or what's happening with those classes.
VinRio VinRio

2017/4/20

#
Here is my counter class:
{
    private int score =0;
    private static final int endScore =10;
    
    public Counter()
    {
        updateScore();
    }
    
    public void act()
    {
        
    }
    
    public void score()
    {
        score++;
        updateScore();
    }
    
    public void updateScore()
    {
        String scoreString = "" + score;
        GreenfootImage img =  new GreenfootImage(scoreString, 40, Color.WHITE, Color.BLACK);
        setImage(img);
    }
}
I have taken out most of the code that reffered to the methods i previously listed because they didnt work. This is an overview of all my classes and subclasses to give a better understanding of how my scenario is laid out, im not sure what code you would needd in order to help me. What should the method signature be for it if i need to call it from another place? And is it a good idea to use a boolean response to tell the method if it should end or should i just use a method by itself that compares the score to the endscore? just right click and open in a new tab to view the image.
danpost danpost

2017/4/20

#
VinRio wrote...
What should the method signature be for it if i need to call it from another place? And is it a good idea to use a boolean response to tell the method if it should end or should i just use a method by itself that compares the score to the endscore?
Well, either the score itself or a boolean value indicating that the score has reached the value of 'endScore' will need to be acquired somewhere outside the Counter class. With your current Counter class code, it would be impossible to get either outside the class. Somewhere you are accessing the Counter object to call the 'score' method, you can create another method in the Counter class and call it immediately after calling 'score' to 'get' the value of 'score':
public int getScore()
{
    return score;
}
Now, need to see the class where the 'score' method is called on the Counter object.
You need to login to post a reply.