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

2015/3/13

Help Needed! non-static method getx() cannot be referenced from a static context

TheGoldenBucket TheGoldenBucket

2015/3/13

#
Hey guys! Needed a bit of guidance on what to do with this... I had this code in my program and it returned the error shown in the title. Help? PS: It was in the directionCheck() method
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
import greenfoot.*;
 
/**
 * Write a description of class Skeleton here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Skeleton extends Actor
{
    private int health;
    private int directionY;
    private int directionX;
     
    /**
     * Act - do whatever the Skeleton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        directionCheck();
        move();
        healthCheck();
        attack();
        deadCheck();
    }   
     
    public void directionCheck()
    {
        if (PlayerChar.getX() > getX)
        {
            directionX = (1);
        }
        if (PlayerChar.getX() < getX)
        {
            directionX = (2);
        }
        if (PlayerChar.getY() > getY)
        {
            directionY = (1);
        }
        if (PlayerChar.getY() < getY)
        {
            directionY = (2);
        }
    }
     
    public void move()
    {
        if (directionX = (1))
        {
            setLocation (1,0);
        }
        if (directionX = (2))
        {
            setLocation (-1,0);
        }
        if (directionY = (1))
        {
            setLocation (0,1);
        }
        if (directionY = (2))
        {
            setLocation (0,-1);
        }
    }
     
    public void healthCheck()
    {
        if (health = (0))
        {
            health = (3);
        }
        //Insert collision code here.
    }
     
    public void attack()
    {
        if (health = (4))//collision code with player
        {
            Main_World.playerHealth--;
        }
    }
     
    public void deadCheck()
    {
        if (health = (4))//collision code with sword
        {
            health--;
        }
        if (health = (4))//collision code with arrow
        {
            health--;
        }
        if (health = (1))
        {
            getWorld.removeObject(this);
        }
    }
}
davmac davmac

2015/3/13

#
I take it that PlayerChar is a class. When you say 'PlayerChar.getX()', you are asking, therefore, for the location of a class. Classes do not have a location. Actors - i.e. instances of classes - have a location. You need to get a reference to the PlayerChar actor that you want the location of, and call getX() on that. See for example tutorial #6.
You need to login to post a reply.