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
danpost danpost

2016/4/2

#
luke6728 wrote...
Does the first block of code you have given me all go in the world constructor?
I specified where each part goes in my first code post. In the last post with code, each line is the first line of a method or constructor (the declaration line for the block of code).
luke6728 luke6728

2016/4/3

#
What do you mean by act or method it calls?
danpost danpost

2016/4/3

#
luke6728 wrote...
What do you mean by act or method it calls?
The given code where I specified that should go within a 'public void act()' method or a method that is called from that method. The World class has an 'act' method provided to it just like the Actor class. The Actor class template places one in your code when you create a subclass of it, but the World class template does not. However, you can write one in it just the same.
luke6728 luke6728

2016/4/4

#
Are the instance fields coloured white at the top of the editor?
danpost danpost

2016/4/4

#
In my editors, the instance fields have a white background; just like the import statements and the commands within the method bodies. A class will usually be outlined as follows:
/** import statements */

public class ClassName extends SuperClassName
{
    /** all static fields, initializers and methods */

    /** all instance fields (non-static fields) */

    /** all constructors -- like 'public ClassName() {...} ' */

    /** all instance methods -- like, 'public void act() {...}' */
}
luke6728 luke6728

2016/4/5

#
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);
    }
    public void act()   
    {
        if (startTime == 0) startTime = System.currentTimeMillis();
        else if (startTime+1000 <= System.currentTimeMillis())
            {
                counter2.add(1);
                startTime = System.currentTimeMillis();
            }    
    }    
}
Is this all I need and is there anything wrong?
danpost danpost

2016/4/5

#
luke6728 wrote...
Is this all I need and is there anything wrong?
Well, I do not see where a Counter2 object is being assigned to the 'counter2' field. Line 26 should be causing an error because of this. It should probably be created during world creation.
luke6728 luke6728

2016/4/7

#
My counter class is called Counter2 so what do I change?
danpost danpost

2016/4/7

#
luke6728 wrote...
My counter class is called Counter2 so what do I change?
danpost wrote...
I do not see where a Counter2 object is being assigned to the 'counter2' field. It should probably be created during world creation.
This should suggest what you need to do. Your Counter2 class is just a blueprint for the objects that the class will create. You will not have a Counter2 object until you use 'new Counter2()'. A reference to the object created is returned when you create one. Therefore, you can assign the object created to your 'counter2' field with:
counter2 = new Counter2();
ryguy ryguy

2016/4/19

#
were do you put for the method for currentTimeMillis();
danpost danpost

2016/4/19

#
ryguy wrote...
were do you put for the method for currentTimeMillis();
Your act method seems fine as is. You just need to add the one line I gave you after the 'super' call in your constructor.
You need to login to post a reply.
1
2