can someone help me make an object move with the movement of the mouse but with some delay , so that when you move the mouse the object doesn't move with it instantaneously. please help , thanks
Each act method, get a new MouseInfo object using 'Greenfoot.getMouseInfo'. If it does not return a null value, use its 'getX' and 'getY' methods for the values to 'turnTowards'.
If you have any questions on the MouseInfo object, refer to its documentation.
but the "turnTowards" turns the object to where the 'getX' and 'getY' of the mouse are... , cant i use the 'int values of getX and Y , in a function like move()? , for example: move(getX,getY)
p.s : (i know you cant use it in that way )
The 'turrnTowards' method only rotates the actor so that it 'faces' toward those coordinates. It does not move or relocate the actor in any way. The 'move' method can be used to move the actor in the direction it is then facing.
i have coded like this:
public void followmouse()
{
mouseInfo follow = Greenfoot.getMouseInfo();
if(follow != null) setLocation ( follow.getX() , followgetY() );
}
with this the object moves instantaneously with the mouse , i want to know if it is possible
and how to make the object not move instantly but with some delay .
sorry for the inconvenience..
i finally did it! (with a little help from someone.....)
protected void turntowards()
{
Student turn = (Student) getWorld().getObjects(Student.class).get(0);
turnTowards(turn.getX(), turn.getY());
}
but can someone tell me the reason why i have to use ".get(0)"?
because without it , it doesnt work?
getWorld().getObjects(Student.class) returns a list of all objects of type Student which are in the world (if getWorld() doesn't return 'null'). To get the first element of the list, you need to use 'get(0)' on this list.
getWorld().getObjects(Student.class) returns a list of all objects of type Student which are in the world (if getWorld() doesn't return 'null'). To get the first element of the list, you need to use 'get(0)' on this list.
Before you try to get that first element from the list, you must make sure that it has a first element. You can use either the 'isEmpty' method or the 'size' method, both of the java.util.List class, for that purpose (unless you are absolutely sure that 100 percent of the time there will be a Student object in the world).
The list contains objects. If you use (Student), then these objects are casted to type Student. This is needed, because the variable 'turn' is set to the first element of the list. 'turn' is a variable of type Student, so you have to set a Student object to it. You could also change both Student to Actor, because only the Actor methods 'getX' and 'getY' are used on it.