'selected' is a field name (not a value). You can say 'selected = such'n'such;', but you cannot say 'such'n'such = selected;'. When assigning a value to a field, the field is always on the left and the value, or expression determining the value, is always on the right. The class name in front of it in the declaration statement is similar to 'int' or 'boolean' in from of their field names. In fact, other than the primitive type (int, double, float, byte, boolean, char, long), variables (or fields), the only other type of variable is one of an Object (or subclass of Object) type. A 'String' is an example of an Object type. Variables of an Object type can be either 'null' (absent of an object) or contain an object that is of the field's declared type.
The above, if you were to use it, which you will NOT, should be written this way:
'this' is a keyword that refers to the object being executed or acted on. If in your subclass of World, it would refer to the world instance that is being 'acted' on. If in an Actor subclass, it would refer an instance of that class type (an instance of a subclass is still an instance of that class). When running, greenfoot will execute the active world's act method and then execute the act method of all the actors within that world. 'this' would refer to the actor or world whose act method is being executed on. Lesson over.
The act method of your Tile class should be something like this:
I think 'getKey' is better for what you need here. Next, the 'moveSelected' method will need to be written into your Bejeweled_World class to process the move or refute it if no matches are present after the swapping.
if (selected == this && Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY()-1);
selected = null;
}
public void act()
{
if (selected != this) return;
int dx = 0, dy = 0;
String key = Greenfoot.getKey();
if (getY() > 0 && "up".equals(key)) dy--;
if (getY() < getWorld().getHeight()-1 && "down".equals(key)) dy++;
if (getX() > 0 && "left".equals(key)) dx--;
if (getX() < getWorld().getWidth()-1 && "right".equals(key)) dx++;
if (dx == 0 && dy == 0) return;
((Bejeweled_World)getWorld()).moveSelected(actor, dx, dy);
}
