Reputation: 183
Below is a piece of code of a game in an applet.I got the error: unexpected type required variable found value. Actually the error is because of my assignment in method repaint but how should it be? Any help would be much appreciated.
public class subclass of JApplet{
JApplet jp;
int yPos=230;
public void check{
if(jp.getX()>160 && jp.getY()<200)
repaint();
}
public void repaint(){
jp.getX()=jp.getWidth()-10;
jp.getY()=yPos;
}
}
Upvotes: 3
Views: 1282
Reputation: 21004
The problem reside in these two lines :
jp.getX()=jp.getWidth()-10;
jp.getY()=yPos;
I assume that getX
and getY
return some x
and y
variable. However, you can't mutate them that way, you need to create a setter method or acces them directly and modify them.
Something like :
public void setX(int x)
{
this.x = x;
}
Then you would do
jp.setX(someValue);
or if the field is not private you could directly do :
jp.x = someValue;
The error message "required variable, found value" refer to what is returned by getX
. The left side of an assignment must be a variable to hold the value, but in your case it is a value (returned by the getter) hence the error message.
Upvotes: 3
Reputation: 2357
You can't assign a value to a method call. Change repaint()
to something along the lines of:
jp.setX(jp.getWidth()-10);
jp.setY(yPos);
Upvotes: 1