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

2019/6/23

Adding points to a counter

Ram201 Ram201

2019/6/23

#
how can I create a reference to the counter from my actor class? here is my code in the actor class. Whenever I try to run it Greenfoot keeps telling me that a non-static method "getCounter1" cannot be refernced from static context. public void eat()
1
2
3
4
5
6
7
8
9
10
11
    {
       if(isTouching(Food.class)){
           removeTouching(Food.class);
           foodEaten++;
           speed++;
           Counter1 counter1 = SnakeWorld.getCounter1();
           counter1.addScore();
           SnakeWorld world=(SnakeWorld)getWorld();
           world.addFood();
        }
}
Super_Hippo Super_Hippo

2019/6/23

#
You use "SnakeWorld.getCounter1()" in line 6. You want to call "getCounter1" on the world object in which this object is in, not on the class. Just how you did it with the "addFood" method. If you move line 8 between 5 and 6, then you can use "world" instead of "SnakeWorld" in line 6.
Ram201 Ram201

2019/6/23

#
I did the changes like you said. Now Greenfoot is sending me the following message whenever I eat Food: java.lang.NullPointerException at SnakeHead1.eat(SnakeHead1.java:40) at SnakeHead1.act(SnakeHead1.java:30) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
Super_Hippo Super_Hippo

2019/6/23

#
Is line 40 this one?
1
counter1.addScore();
In this case, the most common issue is that the "getCounter1" method is not getting the counter which is in the world. It is getting no counter.
Ram201 Ram201

2019/6/23

#
no, I only have this code "counter.addScore();" in this line. I'll just send the Whole code in the class. And if the issue is as you described it how can I fix it ?
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
public class SnakeHead1 extends Players
{
    private final int OST= 0;
    private final int WEST= 180;
    private final int NORD= 270;
    private final int SUED= 90;
    private final int SPEED= 10;
    public int counter= 0;
    public int speed= 0;
    public int foodEaten= 0;
    public SnakeHead1()
    {
        GreenfootImage bild =new GreenfootImage(20,20);
        bild.setColor(Color.RED);
        bild.fill();
        setImage(bild);
        setRotation( Greenfoot.getRandomNumber(4)*90);
    }
    public void act()
    {
       moveAround();
       eat();
    }  
    public void eat()
    {
       if(isTouching(Food.class)){
           removeTouching(Food.class);
           foodEaten++;
           speed++;
           SnakeWorld world=(SnakeWorld)getWorld();
           Counter1 counter1 = world.getCounter1();
           counter1.addScore();
           world.addFood();
        }
       if(foodEaten==15){
           Greenfoot.stop();
        }
       if (facingEdge()){
           getWorld().removeObjects(getWorld().getObjects(SnakeHead1.class));
        }
    }
    private boolean facingEdge()
    {
        switch(getRotation()){
            case OST: return getX()==getWorld().getWidth()-1;
            case SUED: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()==0;
            case NORD: return getY()==0;
        }
        return false;
    }
    public void moveAround()
    {
        counter++;
        if(counter==SPEED)
        {   getWorld().addObject(new Tail1(foodEaten*SPEED), getX(), getY());
            move(1);
            counter=0;
        }
        if(Greenfoot.isKeyDown("right"))
        {   setRotation(OST);
        }
        if(Greenfoot.isKeyDown("down"))
        {   setRotation(SUED);
        }
        if(Greenfoot.isKeyDown("left"))
        {   setRotation(WEST);
        }
        if(Greenfoot.isKeyDown("up"))
        {   setRotation(NORD);
        }
    }
}
danpost danpost

2019/6/23

#
I does appear to be exactly the issue presumed. It can only be fixed in your SnakeWorld class (show please).
Ram201 Ram201

2019/6/23

#
here is my code in SnakeWorld subclass:
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.awt.Color;
 
public class SnakeWorld extends World
{
    int score=0;
    public Counter2 counter2;
    public Counter1 counter1;
    public SnakeWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels and sets a background. Prepares the world.
        super(30, 20, 20);
        GreenfootImage bild=new GreenfootImage(20,20);
        bild.drawRect(0, 0, 20, 20);
        bild.setColor(Color.BLACK);
        bild.fill();
        setBackground(bild);
        prepare();
    }
    public void addFood()
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Food(), x, y);
    }
    public void prepare(){
        //adds players and food to the world and place them randomly.Adds Counters to the world.
        int x= Greenfoot.getRandomNumber(getWidth());
        int y= Greenfoot.getRandomNumber(getHeight());
        addFood();//adds food.
        Counter1 counter1= new Counter1();//creates first counter.
        addObject(new Counter1(), 11, 0);//adds first counter.
        SnakeHead1 snakeHead1=new SnakeHead1();//creates firs player.
        addObject(new SnakeHead1(), x, y);//adds first player.
        Counter2 counter2= new Counter2();//creates second counter.
        addObject(new Counter2(), 19, 0);//adds second counter.
        SnakeHead2 snakeHead2=new SnakeHead2();//creates second player
        addObject(new SnakeHead2(), x, y);//adds second player.
    }
    public Counter1 getCounter1()
    {
        return counter1;
    }
    public Counter2 getCounter2()
    {
        return counter2;
    }
}
danpost danpost

2019/6/23

#
Remove the first word in both lines 31 and 35. By having them there, you are creating local variables and avoiding the fields declared on lines 7 and 8.
Ram201 Ram201

2019/6/23

#
well now it's running fine but still not counting.
danpost danpost

2019/6/23

#
Ram201 wrote...
well now it's running fine but still not counting.
Sorry -- did not see this before. Lines 31 and 36 are not adding the counters now stored in the fields at lines 7 and 8 into itself; but, creating new objects and adding them.
Ram201 Ram201

2019/6/23

#
so how can I fix it? sorry for asking too many questions but I'm very new to Greenfoot.
Super_Hippo Super_Hippo

2019/6/23

#
Use "counter1" instead of "new Counter1()" in line 32 and do the same for 36.
Ram201 Ram201

2019/6/23

#
now it's telling me that it cannot find the symbol-method counter1
Ram201 Ram201

2019/6/23

#
ok thanks I solved it.
Ram201 Ram201

2019/6/23

#
thank you very much for your help.
You need to login to post a reply.