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

2015/4/8

Set a Score counter

dyl_fisher dyl_fisher

2015/4/8

#
I have a game that scores you on how long you last. i need to make a counter that counts in seconds and displays this score while you pay.
danpost danpost

2015/4/8

#
It is better and easier to count act cycles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** IN WORLD SUBCLASS */
 
// add instance fields
private int actCount; // the counter field
private Actor timeDisplay; // the time display actor
 
// in constructor
timeDisplay = new SimpleActor();
setTimeText();
addObject(timeDisplay, 80, 20);
 
// in act method
if (!getObjects(Player.class).isEmpty()) // only run timer if player in world
{
    actCount++;
    if (actCount%55 == 0) setTimeText();
}
 
// with added method
private void setTimeText()
{
    timeDisplay.setImage(new GreenfootImage("Time: "+(actCount/55), 24, null, null);
}
Create a subclass of Actor called SimpleActor and make the following its ENTIRE class code:
1
public class SimpleActor extends greenfoot.Actor{}
dyl_fisher dyl_fisher

2015/4/8

#
Im a bit confused where to place all of this code
danpost danpost

2015/4/9

#
Which part(s) are you confused with? Show the code for your subclass of World where you are trying to put it.
dyl_fisher dyl_fisher

2015/4/9

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Sand here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Highway extends World
{
         
    /**
     * Constructor for objects of class Sand.
     *
     */
    public Highway()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1);
         
        prepare();
         
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
 
        int minimum = 320;
        int maximum = 599;
        int randomRangeValue = minimum + Greenfoot.getRandomNumber( maximum - minimum + 1 );
 
        Runner runner = new Runner();
        addObject(runner, 35, Greenfoot.getRandomNumber(600));
 
        Ambulance car = new Ambulance();
        addObject(car, 158, randomRangeValue);
        car.setLocation(159, randomRangeValue);
 
        Green_Car green_car = new Green_Car();
        addObject(green_car, 223, Greenfoot.getRandomNumber(280));
        green_car.setLocation(218, Greenfoot.getRandomNumber(280));
 
        Ambulance car2 = new Ambulance();
        addObject(car2, 159, Greenfoot.getRandomNumber(280));
        car2.setLocation(159, Greenfoot.getRandomNumber(280));
 
        End end = new End();
        addObject(end, 1079, 305);
        end.setLocation(1074, 300);
         
        Blue_car blue_car = new Blue_car();
        addObject(blue_car, 219, randomRangeValue);
        blue_car.setLocation(217, randomRangeValue);
    }
   
danpost danpost

2015/4/9

#
Insert my 4 and 5 at your line 11. Insert my 8 through 10 at your line 20. Insert my 20 through 23 at your line 24. After what you inserted at line 24, add an act method:
1
2
3
4
public void act()
{
 
}
and insert my lines 13 though 17 at line 3 in the act method. Make sure that 'Player.class' is adjusted to the class name for your main character (I did not know, and still do not know, the name of the class for your main character -- maybe it should be 'Runner.class' -- but I cannot be sure).
You need to login to post a reply.