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

2016/3/22

How do you make an imported counter go up every second?

1
2
luke6728 luke6728

2016/3/22

#
I need Help!!!
luke6728 luke6728

2016/3/22

#
Help me Danpost!
luke6728 luke6728

2016/3/22

#
Please @Danpost
danpost danpost

2016/3/23

#
// instance fields in world class
private Counter counter;
private long startTime;

// in constructor of world class
counter = new Counter();

// in act or method it calls (still world class)
if (startTime == 0) startTime = System.currentTimeMillis();
else if (startTime+1000 <= System.currentTimeMillis())
{
    counter.add(1);
    startTime = System.currentTimeMillis();
}
luke6728 luke6728

2016/3/23

#
Thanks dan post
luke6728 luke6728

2016/3/29

#
Danpos
import greenfoot.*;

/**
 * Write a description of class Level2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level2 extends World
{
    private Counter2 counter2;
    private long startTime;
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {    
        super(1050, 800, 1); 
        counter2=new Counter2();
        if(startTime==0)startTime=System.currentTimeMillis();
        else if(startTime+1000<=System.currentTimeMillis())
        {
            counter2.add(1);
            startTime=System.currentTimeMillis();
        }
    }
}
t what have i done wrong
luke6728 luke6728

2016/3/29

#
This is all in the world class
danpost danpost

2016/3/29

#
luke6728 wrote...
< Code Omitted > t what have i done wrong
Lines 21 through 26 will only execute once -- when the Level2 world is constructed. Use the act method if you want code to repeat during execution.
luke6728 luke6728

2016/3/30

#
Can you explain this as if I don't know much about coding (which is the case)?
danpost danpost

2016/3/30

#
The block of code with the declaration line of:
public Level2()
is called the constructor of the Level2 class. Its code is executed when you create a Level2 world object, and only then. The 'else' part of the code will never become true from within the constructor of the class. You require repeated checking on the passage of time for the code to execute properly. That means the code must be placed within a method with a declaration line of:
public void act()
Greenfoot will repeatedly call the method (many times per second at normal operating speed) for as long as the world is active.
luke6728 luke6728

2016/3/31

#
Does all the code you have given go in the constructor?
danpost danpost

2016/3/31

#
luke6728 wrote...
Does all the code you have given go in the constructor?
Please be specific. Which code are you referring to? In my last post the first line is the constructor declaration line of your Level2 class that you gave above (line 17). It is the "header" for the block of code that contained lines 21 through 26, which were not placed correctly. The second line in my last post is the "header" line you need to use for the block of code that should contain the misplaced lines. This block of code (with its declaration line) is totally separate from the constructor.
luke6728 luke6728

2016/4/1

#
Does the first block of code you have given me all go in the world constructor?
luke6728 luke6728

2016/4/1

#
And what is an instance field?
danpost danpost

2016/4/2

#
luke6728 wrote...
And what is an instance field?
An instance field is a non-static (one that is not declared 'static', which makes it a class field) variable declared within a class but outside of any methods and constructors. Each object created from the class will then be given a field as defined for it to use independent of other objects. The word 'instance' refers to the class being called on to create an object, or an 'instance' of the class.
There are more replies on the next page.
1
2