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

2014/5/9

Score counter not working.

1
2
Bntyhntr501 Bntyhntr501

2014/5/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
60
61
62
63
64
65
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MetalWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
     
     
     
    /**
     * Constructor for objects of class MetalWorld.
     *
     */
    public SpaceWorld()
    {   
         
        super(800, 800, 1);
         
        populateWorld();
         
         
    }//end SpaceWorld
     
     
    public void populateWorld()
    {
        Counter counter = new Counter("Score: ");
        addObject(counter, 50, 790);
         
         
         
         
        Redship redship = new Redship(counter);
        addObject(redship, 400, 400);
         
         
         
         
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
        
         
        addObject(new Goldasteroid(),Greenfoot.getRandomNumber(650), Greenfoot.getRandomNumber(650));
         
 
 
         
    }//end populateWorld
     
     
    public Counter getCounter() 
    
        return (Counter)geObjects(Counter.class).get(0); 
    }
     
 
 
 
}//end World
danpost danpost

2014/5/9

#
Ok. It looks like you placed it properly in the world class. Now show the Alienship class again (as it is now).
Bntyhntr501 Bntyhntr501

2014/5/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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Alienship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Alienship extends Aliens
{
    private int health;
    private int stability;
    private int spawnTimer = 10;
     
 
     
 
    /**
     * Act - do whatever the Alienship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        turnAtEdge();
        randomTurn();
        move(2);
        lookForRedship();
         
        runSpawnTimer();
         
    }//end act
     
    public void healthLevel()
    {
        health = 1;
        stability = health;
        this.health = health;
    }//end health
     
    private void runSpawnTimer()
    {
        spawnTimer = (spawnTimer+1)%400;
        if(spawnTimer == 0) spawnAlienship();
    }//end runSpawnTimer
     
    private void spawnAlienship()
    {
        getWorld().addObject(new Alienship(), Greenfoot.getRandomNumber(700), Greenfoot.getRandomNumber(700));
    }//end spawnAlienship
     
     
    public void turnAtEdge()
    {
        if(atWorldEdge())
        {
            turn(Greenfoot.getRandomNumber(10));
        }//end if
    }//end turnAtEdge
     
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100) > 90)
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }//end if
    }//end randomTurn
     
     
     
    public void lookForRedship()
    {
        if(canSee(Redship.class))
        {
            destroy(Redship.class);
            getWorld().addObject(new ScoreBoard(((SpaceWorld)getWorld().getCounter().getValue()), 400, 400));
            Greenfoot.playSound("Blast.wav");
            Greenfoot.stop();
        }//end if
    }//end lookForRedship
     
        public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
             
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
        //end if/else
    }//end atWorldEdge
     
    public int getStability()
    {
        return stability;
    }
     
    public void hit(int damage)
    {
        stability = stability - damage;
        if(stability <= 0)
            destroy ();        
    }//end hit
     
    public void destroy()
    {
        if(health <= 0)
       {
         ((SpaceWorld)getWorld()).getCounter().add(10);
         getWorld().removeObject(this);
         
       }//end if
 
    }//end destory
     
 
     
 
}//end class
danpost danpost

2014/5/9

#
Replace line 75 with this:
1
getWorld().addObject(new ScoreBoard(((SpaceWorld)getWorld()).getCounter().getValue()), 400, 400));
there was a closing parenthesis missing after '((SpaceWorld)getWorld()'.
Bntyhntr501 Bntyhntr501

2014/5/9

#
Awesome it works great now, thank you so much for your time and help.
danpost danpost

2014/5/9

#
Best regards.
You need to login to post a reply.
1
2