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

2016/3/10

Counting class in the world

glendriv glendriv

2016/3/10

#
Please help my school project i want make game that counting how much bug in world than press number button to declare the number but i have a trouble about count how much bug.class in the world, bug.class is spawn when i press button.class . and than can you make a code for the number button? please i am new here sorry for my bad langguag.
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
import greenfoot.*;
import java.util.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class button here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class button extends Actor
{
    int i = Greenfoot.getRandomNumber(9);
    /**
     * Act - do whatever the button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        start();    }   
    public void start() {
    if(Greenfoot.mouseClicked(this)) {
    MouseInfo mouse =    Greenfoot.getMouseInfo();
    for(int x=0; x <i ; x++)
        {
            getWorld().addObject(new bug(), 76, 90);
            Greenfoot.delay(5);
        }} }
  
}
danpost danpost

2016/3/10

#
If you are trying to get the number of bugs in the world after the button class is instantiated, you can use the World class method 'getObjects' with 'bug.class' as the parameter; this will return a List object containing all bugs in the world. Then, you can use the List class method 'size' to determine the number of bugs in the world:
1
int numBugs = getObjects(bug.class).size();
The above line of code is written as if placed in a World subclass.
glendriv glendriv

2016/3/11

#
How about the number button?
glendriv glendriv

2016/3/11

#
there are 9 button(1-9) if u press button 1 if bug=1 pop up massage u Right else u fail. thanks
danpost danpost

2016/3/11

#
glendriv wrote...
there are 9 button(1-9) if u press button 1 if bug=1 pop up massage u Right else u fail. thanks
Is there a question here? If you are having an issue with the buttons, maybe showing the code for the class that creates the buttons plus the code that actually creates and adds the buttons into the world might help.
glendriv glendriv

2016/3/14

#
The bug is always counting as 17
danpost danpost

2016/3/14

#
You need to show the code of the class that creates the number buttons (to begin with -- and possibly to end with).
glendriv glendriv

2016/3/14

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.util.List;
/**
 * Write a description of class back here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class back extends World
{
public int bug= getObjects(bug.class).size();  
 
    /**    * Constructor for objects of class back.
     *
     */
    public back()
    {   
     
    super(600, 400, 1);
    prepare();
    show();
   counting();
   }
 
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        semak semak = new semak();
        addObject(semak,43,30);
        semak semak2 = new semak();
        addObject(semak2,50,87);
        satu satu = new satu();
        addObject(satu,37,336);
        semak2.setLocation(40,165);
        semak semak3 = new semak();
        addObject(semak3,60,88);
        semak2.setLocation(39,165);
        semak2.setLocation(27,141);
        satu.setLocation(26,317);
        lima lima = new lima();
        addObject(lima,256,326);
        sembilan sembilan = new sembilan();
        addObject(sembilan,432,319);
        tujuh tujuh = new tujuh();
        addObject(tujuh,496,262);
        delapan delapan = new delapan();
        addObject(delapan,539,331);
        dua dua = new dua();
        addObject(dua,559,289);
        empat empat = new empat();
        addObject(empat,564,207);
        tiga tiga = new tiga();
        addObject(tiga,431,229);
        enam enam = new enam();
        addObject(enam,394,294);
        lima.setLocation(100,314);
        dua.setLocation(63,371);
        dua.setLocation(68,373);
        lima.setLocation(271,245);
        tiga.setLocation(362,241);
        lima.setLocation(109,314);
        lima.setLocation(365,333);
        tiga.setLocation(108,316);
        empat.setLocation(153,369);
        empat.setLocation(154,372);
        lima.setLocation(197,318);
        lima.setLocation(196,318);
        lima.setLocation(196,313);
        lima.setLocation(200,312);
        enam.setLocation(244,370);
        tujuh.setLocation(300,313);
        tujuh.setLocation(276,314);
        sembilan.setLocation(416,321);
        tujuh.setLocation(282,313);
        tujuh.setLocation(288,313);
        tujuh.setLocation(288,312);
        delapan.setLocation(344,369);
        sembilan.setLocation(387,312);
        semak semak4 = new semak();
        addObject(semak4,579,44);
        semak semak5 = new semak();
        addObject(semak5,588,89);
        semak semak6 = new semak();
        addObject(semak6,553,123);
        button button = new button();
        addObject(button,562,367);
     
   flower flower = new flower();
addObject(flower,75,92);
}
 
 public void show() {
     
    showText("Jumlah: Hitung jumlah serangga",250,25);
    }
     public void counting() {
     
    bug = numberOfObjects();
     showText("Jumlah: "+bug,250,50);
    
}
glendriv glendriv

2016/3/14

#
For button 1, but always cannot find variable bug
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
import greenfoot.*;
import java.util.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class satu here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class satu extends Actor
{
    public back actors;
    /**
     * Act - do whatever the satu wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        press();
        
    }   
     
    private void press() {
    if(Greenfoot.mouseClicked(this)) {
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (bug=1){
    }
    }
    }
}
glendriv glendriv

2016/3/14

#
I am very confused with buttons and counting. one more question why bug cannot be return ( return bug; )
danpost danpost

2016/3/14

#
Let us start with your 'satu' class. Line 2 can be removed as you are not using what that line imports anywhere within the class. Class names by convention start with uppercase letters; you should have it named 'Satu' (not 'satu') at line 10. Line 12 can be removed as the 'actors' field is not used anywhere within the class. Line 25 can be removed as the 'mouse' variable is not used after being assigned what is returned by 'getMouseInfo'. Line 26 is troublesome in more than one way. First, 'bug' is not defined anywhere in the class, not as a field or as a local variable, before it is used in line 26. Second, the expression 'bug=1' is an assignment statement that tries to set 'bug' (which does not exist) to the value of one (it does not compare the non-existent variable with the number, which using '==' would). This means that if 'bug=1' was evaluated properly, it would be a numeric value, not a boolean (true/false) value which would be required for the 'if' condition. So, for now, this is what we have for that class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;
 
public class Satu extends Actor
{
    public void act() {
        press();
    }
     
    private void press() {
        if (Greenfoot.mouseClicked(this)) {
            if (/** condition */) {
                /** action */
            }
        }
    }
}
Now for the 'back' (soon to be 'Back' class). Lines 2 through 4 can be removed as none of the classes imported by them are explicitly used within the class. Line 13 declare an instance field called 'bug' and is assigned a value from an evaluated expression that will, when executed, always return zero. I really do not see a need for this instance field as there is no need to retain the value between act cycles -- just calling the method to get the value when needed is sufficient. At line 104, you are assigning a value to this field using 'numberOfObjects', which will not count just the bugs, but also include your buttons and flowers. In fact, if you count the number of 'addObject' lines in the prepare method, you will count 17 -- the number that you keep getting. If you want the number of bugs in your world to vary, you will need to get a random number and implement a loop or switch structure. Simplifying your world class, we come to this:
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
import greenfoot.*;
 
public class Back extends World
{
    public Back()
    {   
        super(600, 400, 1);
        prepare();
        show();
        counting();
    }
    private void prepare()
    {
        addObject(new Satu(), 26, 317);
        addObject(new Dua(), 68, 373);
        addObject(new Tiga(), 108, 316);
        addObject(new Empat(), 154, 372);
        addObject(new Lima(), 200, 312);
        addObject(new Enam(), 244, 370);
        addObject(new Tujuh(), 288, 312);
        addObject(new Delapan(), 344, 369);
        addObject(new Sembilan(), 387, 312);
         
        addObject(new Semak(), 43, 30);
        addObject(new Semak(), 27, 141);
        addObject(new Semak(), 60, 88);
        addObject(new Semak(), 579, 44);
        addObject(new Semak(), 588, 89);
        addObject(new Semak(), 553, 123);
         
        addObject(new Button(), 562, 367);
        addObject(new Flower(), 75, 92);
    }
 
    public void show()
    {
        showText("Jumlah: Hitung jumlah serangga", 250, 25);
    }
 
    public void counting()
    {
        showText("Jumlah: "+getObjects(Semak.class).size(), 50, 50);
    
}
Now, for you number buttons -- the condition would be comparing the button number to 'getWorld().getObjects(Semak.class).size()'.
glendriv glendriv

2016/3/15

#
Thanks i will try it
You need to login to post a reply.