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

2015/3/10

How to set values?

1
2
RagingAsian RagingAsian

2015/3/10

#
I have a counter that is suppose to increase every time an enemy is killed. Although, I cannot seem to do it, can anyone help? I also cannot seem to set my values for an object using the values in the constructors. If you want a current version of my project, it is on my profile, will also post code if asked for.
Super_Hippo Super_Hippo

2015/3/10

#
You will need a reference to the Counter object and call a method on it to set the new value (or just change it the field is public).
1
2
3
4
5
6
7
8
9
10
private int value = 0;
public void setValue(int newValue)
{
    value = newValue;
}
//or in your case maybe better
public void increaseValue(int diff)
{
    value += diff;
}
Then, if you want to increase it, you only need to get a reference to the counter and call the method on it. If you don't have a reference and there is only one Counter in the world, you can use this:
1
((Counter)(getWorld().getObjects(Counter.class).get(0))).increaseValue(1);
RagingAsian RagingAsian

2015/3/10

#
I still cannot get it.
Super_Hippo Super_Hippo

2015/3/10

#
Please explain with what exactly you have problems.
RagingAsian RagingAsian

2015/3/10

#
I got an NullPointerExecption error right as the ice tower shot. java.lang.NullPointerException at Bullets.<init>(Bullets.java:19) at IceBullet.<init>(IceBullet.java:14) at Ice.shoot(Ice.java:38) at Ice.act(Ice.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Bullets Class:
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
import greenfoot.*; 
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullets extends Actor
{
    private int x=0;
    private Actor enemy;
    private Counter2 money;
    private List<Counter2> myList= new ArrayList<Counter2>();
    public Bullets(Actor enemy)
    {      
        this.enemy=enemy;
        myList = getWorld().getObjects(Counter2.class);
        money = myList.get(0);
    }
     
    public void act()
    {
        move();
        ifAHit();
        ifAtWorldEdge();
    }
     
    public void ifAtWorldEdge()
    {
//         if(getX() >= getWorld().getWidth()-1)
//         {
//             getWorld().removeObject(this);
//             return;
//         }
//         if(getX() >= getWorld().getWidth()+1)
//         {
//             getWorld().removeObject(this);
//             return;
//         }
//         if(getY() >= getWorld().getHeight()-1)
//         {
//             getWorld().removeObject(this);
//             return;
//         }
//         if(getY() >= getWorld().getHeight()+1)
//         {
//             getWorld().removeObject(this);
//             return;
//         }
         
    }
    /**
     * Bullet move towardds a greep
     *
     */
    public void move()
    {
        if (enemy.getWorld()!=null)
        {
            int x=enemy.getX();           
            int y=enemy.getY();
            turnTowards(x,y);
            move(2);
             
        } else {
            move (2);
        }
      
    }
     
    /**
     * removes objects if bullet and Greep intersect
     *
     */
    public void ifAHit()
    {
        Actor creeper = getOneIntersectingObject(Greeps.class);
        if(creeper!=null)
        {
             
            getWorld().removeObject(creeper);
            getWorld().removeObject(this);
            if(Small.class!=null)
            {
                money.add(10);
            }
             
        }
    }
         
     
}
Ice Class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Ice here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ice extends Towers
{
    private int canShoot=0;
    int count=0;
    public Ice(Counter2 money)
    {
       super(5,50,15);
    }
     
    /**
     * Act - do whatever the Ice wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        super.Act();
        shoot(enemies);
        kind=3;
    }   
     
    public void shoot(List<Greeps> enemies)
    {
          canShoot=175;
          for(int i=0;i<enemies.size();i++)
          {
          Actor closestEnemy=enemies.get(i);
          count++;
          if(count>=canShoot)
          {
              getWorld().addObject(new IceBullet(closestEnemy),getX(),getY());
              if(count==canShoot)
              {
                  count=0;
              }
          }
        }
          System.out.println(count);
        
    }
}
davmac davmac

2015/3/10

#
You're calling getWorld() from the Bullets constructor. Calling getWorld() returns the world that the actor is in; if you call it in the constructor, the actor is not yet in any world, so it returns null. Probably you can use the addedToWorld() method to accomplish what you were trying to do from the constructor (you will need to add it to your Bullets class).
Super_Hippo Super_Hippo

2015/3/10

#
1
myList = getWorld().getObjects(Counter2.class);
The constructor is executed, before the object is added to the world, so 'getWorld()' returns 'null'. Calling a method (here 'getObjects') on null gives you a nullpointer exception.
RagingAsian RagingAsian

2015/3/11

#
OK its not causing an error anymore, but still need help increasing the counter every time a greep is killed.
Super_Hippo Super_Hippo

2015/3/11

#
What I already said: "Please explain with what exactly you have problems." Maybe show what you have tried to increase the counter.
danpost danpost

2015/3/11

#
Line 85 of your Bullets class will always return true if you created a class called 'Small'. The expression 'Small.class' has nothing to do with objects created from the class. It is a reference to the class itself. 'Small' is the name of the class and is the type of object that can be created from the class (the objects created are considered as being of type 'Small'). It is not clear as to the intention of line 85 (what condition you were trying to check to regulate the score increase).
RagingAsian RagingAsian

2015/3/11

#
everytime i try to increase the money counter I get this: java.lang.NullPointerException at Bullets.addMoney(Bullets.java:86) at SniperBullet.act(SniperBullet.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here is the source of the error that I'm using to try and get the money counter to increase
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
import greenfoot.*; 
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bullets extends Actor
{
    private int x=0;
    private Actor enemy;
    Counter2 money;
    private List<Counter2> myList= new ArrayList<Counter2>();
    public Bullets(Actor enemy)
    {      
        this.enemy=enemy;
        
 
    }
     
    public void act()
    {
        move();
        ifAHit();
        ifAtWorldEdge();
        addMoney();
        myList = getWorld().getObjects(Counter2.class);
        money = myList.get(0);
    }
     
    public void ifAtWorldEdge()
    {
        if(getX() >= getWorld().getWidth())
        {
            getWorld().removeObject(this);
            return;
        }
 
        if(getY() >= getWorld().getHeight())
        {
            getWorld().removeObject(this);
            return;
        }
         
    }
    /**
     * Bullet move towardds a greep
     *
     */
    public void move()
    {
        if (enemy.getWorld()!=null)
        {
            int x=enemy.getX();           
            int y=enemy.getY();
            turnTowards(x,y);
            move(2);
             
        } else {
            move (2);
        }
      
    }
     
    /**
     * removes objects if bullet and Greep intersect
     *
     */
    public void ifAHit()
    {
        Actor creeper = getOneIntersectingObject(Greeps.class);
        if(creeper!=null)
        {
             
            getWorld().removeObject(creeper);
            getWorld().removeObject(this);
        }
    }
         
    public void addMoney()
    {
        if (Greeps.class!=null)
        {
            money.add(15);
        }
    }
}
The SniperBullet class is a subclass of the bullet class and calls the super method only. But if you need it, Line 24 of SniperBullet is :
1
super.addMoney();
danpost danpost

2015/3/11

#
Your 'Counter2 money' field declared on line 14 of the Bullets class is never assigned any Counter2 object to it (at least not that I can see -- and the error message indicates that the field has a 'null' value.
davmac davmac

2015/3/11

#
danpost wrote...
Your 'Counter2 money' field declared on line 14 of the Bullets class is never assigned any Counter2 object to it
It is assigned in the 'act' method, but from the stack trace it appears that the act method is overridden in a subclass (SniperBullet). I suspect the subclass no longer sets 'money' (and doesn't call the super.act() method) and so it stays null. In any case, the act method seems like the wrong place to assign the variable.
RagingAsian RagingAsian

2015/3/12

#
Ok the money counter works perfectly now. But can i get help with getting my lives counter working? I can't get the lives counter to decrease when they reach the end of the path. Greeps Super Class
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
120
121
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Creepers here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Greeps extends Actor
{  
    public int type;
    int HP;
    int SPEED;
     
    int[]WayX;
    int[]WayY;
    int index=0;
     
    double distance=150;
     
    public int hp;
    public double speed;
     
    private int level;
    Counter lives;
    public Greeps(int hp,double speed, int lv)
    {
        level = lv;
        this.hp=HP;
        this.speed=SPEED;
        setRotation(90);
        if(type==1) setImage("Enemy-small.png");
        if(type==2) setImage("Enemy-medium.png");
        if(type==3) setImage("Enemy-heavy.png");
        if (IntroScreen.level==3)
        {
            setRotation(90);
        }
        waypoints();
         
    }
     
    public void act()
    {
        movement();
    }                       
     
    /**
     * Sets the points where the Greeps will turn
     *
     */
    public void waypoints()
    {
 
        if(level== 1)
        {
           WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,599};
           WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
         
        }
        if (level == 2){
           WayX= new int[] {478,478,272,272,599};
           WayY= new int[]{171,380,380,19,19};
        }
        if (level == 3){
           WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27};
           WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10};
        }
        if (level == 4){
           WayX= new int[]{319,319,56,56,431,431,525,525,599};
           WayY= new int[]{376,21,21,288,288,21,21,375,375};
        }
    }
     
    /**
     * Sets the speed of the Greeps and determins when to move onto the next waypoint
     *
     */
    public void movement()
    {       
        move(1);
        int theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));  
        lives=lives;
        if (theDistance < 1  )
        {
            if(index<WayX.length-1)
            {
                index++;
            } else
            {
                if((level==1)&&(index==12))
                {
                    getWorld().removeObject(this); 
                    lives.add(-1);
                    return;
                }
                if((level==2)&&(index==4))
                {
                    getWorld().removeObject(this); 
                    lives.add(-1);
                    return;
                }
                if((level==3)&&(index==10))
                {
                    getWorld().removeObject(this); 
                    lives.add(-1);
                    return;
                }
                if((level==4)&&(index==8))
                {
                    getWorld().removeObject(this); 
                    lives.add(-1);
                    return;
                }
            }
             
        }
        turnTowards(WayX[index], WayY[index]);
        
    }          
 }
davmac davmac

2015/3/12

#
Please properly explain your problems. What does "I can't get the lives counter to decrease" mean? You get an error of some kind, or you don't get the behavior that you want (in which case, what does actually happen)? I can see at least this (which is similar to the problem you had initially): On line 25 you declare 'lives':
1
Counter lives;
... but, you don't seem to set it to anything, except on line 83, where you set it to itself - which doesn't make any sense. Given that you've never set it to anything, I would that no Counter actually exists. Or, if you've created a Counter separately, that it exists independently with no connection to the 'lives' variable.
There are more replies on the next page.
1
2