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

2018/1/31

Separate scoreboard and timer for 3 classes

1
2
3
danpost danpost

2018/1/31

#
frequency wrote...
What? Its empty? The code is nearly the same << Code Omitted >> thats it lol
You show codes to a different class than I stated.
frequency frequency

2018/1/31

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class World_30 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class World_90 extends World
{

    /**
     * Constructor for objects of class World_90.
     * 
     */
    private static int score=0;
    public World_90()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500, 800, 1); 
        checkKeyboard();
        addObject(new ninety_seconds(),120,90);
        addObject(new Score90(),400,700);
    }
    
        void checkKeyboard()
   {
       if (Greenfoot.isKeyDown("space"))
       {
           score=score+1;
       }
    }

    
      public static int getScore()
    {
        return score;
    }
}
    

There it is!
danpost danpost

2018/1/31

#
Sorry ... I meant the Score90 class.
frequency frequency

2018/1/31

#
hahah :P. score90 class is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Score90 extends Scores
{
    private static final Color TEXT_COLOR=new Color(200,0,0);
    private static final Color TRANSPARENT_COLOR=new Color(0,0,0,0);
    
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private void updateImage()
    {
       
       
    }
    
    public void act() 
    {
       updateImage();
  
    }
    public Score90()
    {
        updateImage();
    
    }
    
  
  
    
}
danpost danpost

2018/1/31

#
No code in the 'updateImage' method !!!
frequency frequency

2018/1/31

#
Done.!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class thirty_seconds here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ninety_seconds extends Actor
{
    public static int score=0;
    boolean bla=false;
    
    public void act()
    {
        
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new World_90());
        }
        if (Greenfoot.isKeyDown(" ") && bla==false){
            checkKeyboard();
            bla=true;
        }
        else if(!Greenfoot.isKeyDown(" ") && bla==true){
            bla=false;
        }
    }

    void checkKeyboard()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            score=score+1;
        }
    }

    public static int getScore()
    {
        return score;
    }
    
}













Now i want the score to reset after i press reset. I added
private static int score=0;
on some actors and worlds but can not reset!
danpost danpost

2018/1/31

#
frequency wrote...
i want the score to reset after i press reset. I added
private static int score=0;
on some actors and worlds but can not reset!
Adding more static int score field is just going to complicate things (and it does not help at all). All you need is to zero the appropriate score when each type world is created (from the constructor of the worlds).
frequency frequency

2018/2/1

#
http://www.greenfoot.org/scenarios/20776 Take a look.
danpost danpost

2018/2/1

#
First, right click on and delete the MyWorld, Color30, Color60 and Color90 classes. Then, why do you add buttons into your World30, World60 and World90 classes?
frequency frequency

2018/2/1

#
Cause they would add for some reason and could not remove them! So i moved them! Done removed them!
frequency frequency

2018/2/1

#
Based on your scenario http://www.greenfoot.org/scenarios/14834 What piece of code should i paste into my program so as to create a 3 different timers?
danpost danpost

2018/2/1

#
frequency wrote...
Based on your scenario http://www.greenfoot.org/scenarios/14834 What piece of code should i paste into my program so as to create a 3 different timers?
Since you like creating multiple classes (one for each amount of time), maybe you should just create 3 timer classes.
frequency frequency

2018/2/1

#
Timer 30,timer 60,timer 90? And i should add them as objects from world?!
danpost danpost

2018/2/1

#
frequency wrote...
Timer 30,timer 60,timer 90? And i should add them as objects from world?!
If you want the player to be able to see his progress per time during game play -- yes. But, you probably want the time to begin when the space is first hit -- not when object is created. So, you will need to set the end time field when the space is pressed (and when not yet set -- the field still as a zero value). So, in act, something like this (for a 30 second timer):
if (endTime == 0 && Greenfoot.isKeyDown("space")) endTime = 30000+System.currentTimeMillis();
if (endTime == 0) return;
// calculate time remaining and adjust display here
You could also add a public static boolean field to show the completion state of the timer. Set it to false in the constructor and set it to true when the end time is reached. That way, the circle can regulate scoring properly by checking the field.
frequency frequency

2018/2/1

#
I dont know how to syntax the bool! :/ Timer 30 class consists of
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer30 extends thirty_seconds
{
    private static final Color TEXT_COLOR = new Color(255,255,255);
    private static final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
    private long timeStarted;
    private int seconds=10;
    
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void Timer()
    {
        updateImage();
        timeStarted=System.currentTimeMillis();
    }
    
    public void act() 
    {
        updateImage();
        Timer();
        
        if (System.currentTimeMillis()-timeStarted>1000)
        {
            timeStarted=System.currentTimeMillis();
            seconds--;
        }
    
    } 
    
    private void updateImage()
    {
        String text="Seconds left";
        GreenfootImage image =new GreenfootImage(text,30,TEXT_COLOR,TRANSPARENT_COLOR);
        setImage(image);
    }
}
Where will i place the code?
There are more replies on the next page.
1
2
3