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

2017/6/10

non static method cannot be referenced from a static context java

mrlemon mrlemon

2017/6/10

#
So i'm trying to update my score when an enemy dies in a game but it keeps me giving the error message in de code above. Can someone help me out? Here's the code in my world class:
1
2
3
4
5
public void updateScore(int amount)
    {
        score = score + amount;
        showText("Score: " + score, 100, 50);
    }
And this is the code in my enemy class:
1
2
3
4
5
6
7
8
9
private void checkAlive()
    {
         
        if(isAlive=false)
        {
            MyWorld theWorld = (MyWorld) getWorld();
            MyWorld.updateScore(15);
        }
    }
Thanks!
Super_Hippo Super_Hippo

2017/6/10

#
Change 'MyWorld' in line 7 to 'theWorld'.
mrlemon mrlemon

2017/6/10

#
thanks, that helped but now the score is not counting
Super_Hippo Super_Hippo

2017/6/10

#
Do you call the 'checkAlive' method at the end of the act method in the enemy class?
mrlemon mrlemon

2017/6/10

#
yes i do. here's the code for the enemy 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Enemy2 extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage image9;
    private GreenfootImage image10;
    private GreenfootImage image11;
    private GreenfootImage image12;
    private boolean isAlive;
    private boolean facingRight;   
    private int frame = 1;
    private int animationCounter = 0;
 
    public Enemy2()
    {
 
        image1 = new GreenfootImage("basicenemystance1.png");
        image2 = new GreenfootImage("basicenemystance2.png");
        image3 = new GreenfootImage("basicenemypreattack1.png");
        image4 = new GreenfootImage("basicenemypreattack2.png");
        image5 = new GreenfootImage("basicenemyattack.png");
        image6 = new GreenfootImage("basicenemydies.png");
        image7 = new GreenfootImage("basicenemystance1left.png");
        image8 = new GreenfootImage("basicenemystance2left.png");
        image9 = new GreenfootImage("basicenemypreattack1left.png");
        image10 = new GreenfootImage("basicenemypreattack2left.png");
        image11 = new GreenfootImage("basicenemyattackleft.png");
        image12 = new GreenfootImage("basicenemydiesleft.png");
        isAlive = true;
    }
 
    /**
     * Act - do whatever the Enemy2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkDirection();
        move();
        attackLeft();
        attack();  
        die();
        animationCounter++;
        checkAlive();
         
    }   
 
    private void checkDirection()
    {
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        int playerX = player.getX();
        if(getX() <= playerX)
        {
            facingRight = true;
        }
        else
        {
            facingRight = false;
        }
    }
 
    private void move()
    {
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        int playerX = player.getX();
        if(!isTouching(Player.class) && facingRight == true){  
            if(playerX-getX()>300){
                setLocation(getX()+3, getY());
                if(animationCounter % 8 == 0)
                {
                    animateWalk();
                }
            }
            else{
                setLocation(getX()+3, getY());
                if(animationCounter % 8 == 0)
                {
                    animatePreAttack();
                }
 
            }           
        }
        if(!isTouching(Player.class) && facingRight == false){  
            if(getX()-playerX>300){
                setLocation(getX()-3, getY());
                if(animationCounter % 8 == 0)
                {
                    animateWalkLeft();
                }
            }
            else{
                setLocation(getX()-3, getY());
                if(animationCounter % 8 == 0)
                {
                    animatePreAttackLeft();
                }
 
            }
 
        }
    }
     
    private void attack()
    {
        if(isTouching(Player.class) && facingRight==true)
        {
            setImage(image5);
 
        }
    }
    private void attackLeft()
    {
        if(isTouching(Player.class) && facingRight==false)
        {
            setImage(image11);
 
        }
    }
    private void die(){
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        if (facingRight==true && isTouching(Player.class) &&  (player.getImage().equals(player.image9) || player.getImage().equals(player.image11)
        || player.getImage().equals(player.image15)|| player.getImage().equals(player.image19)|| player.getImage().equals(player.image21)))
        {
            Greenfoot.playSound("receivehit.wav");
            setImage(image6);
            isAlive = false;
            int x = getX();
            for(int y = getY(); y<=515; y++)
            {
                setLocation(x, y);
            }
             
            Greenfoot.delay(10);
            getWorld().removeObject(this);            
        }
         
        else if (facingRight==false && isTouching(Player.class) && (player.getImage().equals(player.image10) || player.getImage().equals(player.image12)
            || player.getImage().equals(player.image16)|| player.getImage().equals(player.image20)|| player.getImage().equals(player.image22)))
        {
            Greenfoot.playSound("receivehit.wav");
            setImage(image12);
            isAlive = false;
            int x = getX();
            for(int y = getY(); y<=515; y++)
            {
                setLocation(x, y);
             
            }
            Greenfoot.delay(10);
            getWorld().removeObject(this);
        }        
    }
       
    private void animateWalk() //animation for enemy walking right
    {
        if(frame == 1)
        {
            setImage(image2);
 
        }
        else if(frame == 2)
        {
            setImage(image1);
            frame = 1;
            return;
        }
        frame++;
    }
     
    private void animateWalkLeft() //animation for enemy walking left
    {
        if(frame == 1)
        {
            setImage(image8);
 
        }
        else if(frame == 2)
        {
            setImage(image7);
            frame = 1;
            return;
        }
        frame++;
    }
 
    private void animatePreAttack() //animation for enemy walking left while near player
    {
        if(frame == 1)
        {
            setImage(image3);
 
        }
        else if(frame == 2)
        {
            setImage(image4);
            frame = 1;
            return;
        }
        frame++;
    }
     
    private void animatePreAttackLeft() //animation for enemy walking right while near player
    {
        if(frame == 1)
        {
            setImage(image9);
        }
        else if(frame == 2)
        {
            setImage(image10);
            frame = 1;
            return;
        }
        frame++;
    }
    private void checkAlive()
    {
         
        if(isAlive=false)
        {
            MyWorld theWorld = (MyWorld) getWorld();
            theWorld.updateScore(15);           
        }
    }
 
}
Super_Hippo Super_Hippo

2017/6/10

#
Change line 230 to this:
1
if (isAlive == false) // 2 = instead of 1
or just
1
if (!isAlive)
mrlemon mrlemon

2017/6/10

#
i tried doing that but than it gives me the next error: java.lang.NullPointerException at Enemy2.checkAlive(Enemy2.java:233) at Enemy2.act(Enemy2.java:52) 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

2017/6/10

#
Remove both instances of
1
getWorld().removeObject(this);
from the class (lines 145 and 161) and add
1
theWorld.removeObject(this);
after line 233.
mrlemon mrlemon

2017/6/10

#
in that case the counter quickly runs up to a 6 digit number and aftwerwards falls back to zero.
Super_Hippo Super_Hippo

2017/6/10

#
Well, nothing in the code you showed lets the counter "fall back to zero". Did you add the 'theWorld.removeObject(this);'? How many enemies are in the world?
mrlemon mrlemon

2017/6/10

#
the enemies are added randomly to the world in the world 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class MyWorld extends World
{
   public Player thePlayer;
   private int score;  
   private int amount;
    
    public MyWorld()   
    {   
        super(1200, 800, 1, true); //The extra 'true' parameter means that the world is bounded.
        addFloor();         
        score = 0;       
        thePlayer = new Player();
        addObject(thePlayer, 600, 415); //Creates the player.
        amount = 15;
    }
     
    public void addFloor()
    {
        addObject(new Floor(), 0, 400);       
    }
     
    /**
     * Creates enemies.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(200) < 1) //create Enemy2 object at a given x and y coordinate at a chance of 1 in 200
        {
                       
            addObject(new Enemy2(), 1,415);
             
        }
         
        if (Greenfoot.getRandomNumber(200) < 1)
        {
                         
            addObject(new Enemy2(), 1199,415);
             
        }
         
        if (Greenfoot.getRandomNumber(700) < 1 && getObjects(Enemy1.class).size() < 1) //create enemy1 at a given x and y coordinate at a chance of 1 in 700 if there is no other Enemy1 object in the world
        {
                         
            addObject(new Enemy1(), 1,415);
             
        }
         
        if (Greenfoot.getRandomNumber(700) < 1 && getObjects(Enemy1.class).size() < 1)
        {          
             
            addObject(new Enemy1(), 1199,415);
             
        }
        updateScore(score);
    }
     
    public Player getPlayer()
    {
        return thePlayer;       
    }
     
    public void updateScore(int amount)
    {
        score = score + amount;
        showText("Score: " + score, 100, 50);
    }
}
Super_Hippo Super_Hippo

2017/6/10

#
Move line 57 to the constructor line 17. Right now, you are doubling the score every act cycle.
mrlemon mrlemon

2017/6/10

#
Oh waw, I can't beleive i missed that. Guess that is what looking at the same screen for too long leads to :-). Thanks a lot Super_Hippo!!!
You need to login to post a reply.