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

2021/11/6

Implement 3 timers for one world controlled from buttons

1
2
nafridi217 nafridi217

2021/11/6

#
I wanted to implement a timer for my project that is changed based of which mode the player selects. I want the timer to be passed to the world from buttons like easy, medium and hard and the buttons are subclasses of the actor. is there something in the project setup that I need to change or do I have to change the code for the timer. The timer code is in my world class. I hope someone can help me today as I need to finish this in the next 24 hours. Thank you in advance.
danpost danpost

2021/11/6

#
In world:
private int timeRemaining;
private boolean gameOver;

public void setTimeRemaining(int duration)
{
    timeRemaining = duration;
    gameOver = false;
}

public void act()
{
    if (gameOver) return;
    //everything else world does
    if (timeRemaining > 0 && --timeRemaining == 0) gameOver();
}

private void gameOver()
{
    gameOver = true;
    // whatever else you want to do when ending a game
}
To set a one minute game upon a button click, for example (somewhere in button class codes):
((MyWorld)getWorld()).setTimeRemaining(3600);
danpost danpost

2021/11/6

#
You can display the time remaining in a simple actor. A simple actor could have a complete class code as follows:
public class SimpleActor extends greenfoot.Actor {}
Then, in world, add the following:
private Actor timerDisplay = new SimpleActor();

private void updateTimerDisplay()
{
    GreenfootImage img = new GreenfootImage("Time left: "+timeRemaining, 32, Color.BLACK, new Color(0, 0, 0, 0));
    timerDisplay.setImage(img);
}
In constructor, add the following:
updateTimerDisplay();
addObject(timerDisplay, 150, 15);
Also, as last line in act method, add:
updateTimerDisplay();
nafridi217 nafridi217

2021/11/6

#
The button classes are subclasses of the Actor class. does that matter or it should still work after I instantiate the world class in the button classes?
nafridi217 nafridi217

2021/11/6

#
My timer code is in the world class where I also have code to display the timer on the main display. Should I get rid of that code?
nafridi217 nafridi217

2021/11/7

#
java.lang.ClassCastException: class info cannot be cast to class MyWorld (info and MyWorld are in unnamed module of loader java.net.URLClassLoader @190b5f40) I get this error when setting the timer from the button. ((MyWorld)getWorld()).setTimeRemaining(3600); this line of code is in the button act method where I have code to instantiate the world class after the button has been clicked. What am I doing wrong? thank you
danpost danpost

2021/11/7

#
nafridi217 wrote...
instantiate the world class in the button classes?
That does not sound right. Show codes.
danpost danpost

2021/11/7

#
nafridi217 wrote...
My timer code is in the world class where I also have code to display the timer on the main display. Should I get rid of that code?
Sounds right. Show codes for review.
danpost danpost

2021/11/7

#
nafridi217 wrote...
java.lang.ClassCastException: class info cannot be cast to class MyWorld (info and MyWorld are in unnamed module of loader java.net.URLClassLoader @190b5f40) I get this error when setting the timer from the button. ((MyWorld)getWorld()).setTimeRemaining(3600); this line of code is in the button act method where I have code to instantiate the world class after the button has been clicked. What am I doing wrong? thank you
Sounds like the buttons are in one world and you are instantiating another world with the timer in it. If that is the case, you would create the world, then set its timer. Show codes tried.
nafridi217 nafridi217

2021/11/7

#
public class easyButton extends Actor
{
    public int time = 3600;
    // constructor
    
    public easyButton(){
    GreenfootImage myImage = getImage();
       int myNewH = (int)myImage.getHeight()/6;
       int myNewW = (int)myImage.getWidth()/6;
       myImage.scale(myNewW, myNewH);
    }
    /**
     * Act - do whatever the easyButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        
        if(Greenfoot.mouseClicked(this)){
            ((MyWorld)getWorld()).setTimeRemaining(3600);
            Greenfoot.setWorld(new MyWorld());
        }
    }
    
}
this is the code in the button class.
nafridi217 nafridi217

2021/11/7

#
danpost wrote...
nafridi217 wrote...
java.lang.ClassCastException: class info cannot be cast to class MyWorld (info and MyWorld are in unnamed module of loader java.net.URLClassLoader @190b5f40) I get this error when setting the timer from the button. ((MyWorld)getWorld()).setTimeRemaining(3600); this line of code is in the button act method where I have code to instantiate the world class after the button has been clicked. What am I doing wrong? thank you
Sounds like the buttons are in one world and you are instantiating another world with the timer in it. If that is the case, you would create the world, then set its timer. Show codes tried.
The buttons are in the subclasses of the actor class. I uploaded code for one of the button. but the line of code that you gave me to set the time did not work. I have to instantiate the world in the class first and then use the created object to call the setTimeRemaining method to pass the int value. the Timer shows up on the display but its not being updated from 0. which sounds like the int value is not being passed into the method.
nafridi217 nafridi217

2021/11/7

#
danpost wrote...
nafridi217 wrote...
java.lang.ClassCastException: class info cannot be cast to class MyWorld (info and MyWorld are in unnamed module of loader java.net.URLClassLoader @190b5f40) I get this error when setting the timer from the button. ((MyWorld)getWorld()).setTimeRemaining(3600); this line of code is in the button act method where I have code to instantiate the world class after the button has been clicked. What am I doing wrong? thank you
Sounds like the buttons are in one world and you are instantiating another world with the timer in it. If that is the case, you would create the world, then set its timer. Show codes tried.
public class easyButton extends Actor
{
    //public int time = 3600;
    MyWorld timer = new MyWorld();
    // constructor
    
    public easyButton(){
    GreenfootImage myImage = getImage();
       int myNewH = (int)myImage.getHeight()/6;
       int myNewW = (int)myImage.getWidth()/6;
       myImage.scale(myNewW, myNewH);
    }
    /**
     * Act - do whatever the easyButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        
        if(Greenfoot.mouseClicked(this)){
            timer.setTimeRemaining(4800);
            Greenfoot.setWorld(new MyWorld());
        }
    }
    
}
I tried the code provided in the previous reply (which did not work), but using the code provided in the reply works. I think this is what you were talking about? Also, what code needs to go in the SimpleActor class? can you please provide that code?
nafridi217 nafridi217

2021/11/7

#
danpost wrote...
nafridi217 wrote...
My timer code is in the world class where I also have code to display the timer on the main display. Should I get rid of that code?
Sounds right. Show codes for review.
if (timer > 0) {
            timer--;
            if (timer%60 == 0){
            timerText.setText("Time Left: " + (timer/60));
            }
            if(timer == 0) {
                Greenfoot.stop();
            }
        }
this is the timer code that is in the act method of MyWorld class. do I need to put it in another class? if so, where and if not, how do I use this code with the code you provided?
danpost danpost

2021/11/7

#
nafridi217 wrote...
<< Code Omitted >> this is the code in the button class.
Use:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        MyWorld mw = new MyWorld();
        mw.setTimeRemaining(time);
        Greenfoot.setWorld(mw);
    }
}
danpost danpost

2021/11/7

#
nafridi217 wrote...
what code needs to go in the SimpleActor class? can you please provide that code?
danpost wrote...
A simple actor could have a complete class code as follows:
public class SimpleActor extends greenfoot.Actor {}
There are more replies on the next page.
1
2