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

2015/3/9

Follow the mouse?

Giantmac Giantmac

2015/3/9

#
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
danpost danpost

2015/3/9

#
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.
Giantmac Giantmac

2015/3/9

#
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 )
danpost danpost

2015/3/9

#
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.
Giantmac Giantmac

2015/3/9

#
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..
danpost danpost

2015/3/9

#
You are using 'setLocation', which is not what you want. You need to use 'turnTowards' followed by 'move'.
Giantmac Giantmac

2015/3/9

#
thank you
Giantmac Giantmac

2015/3/11

#
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?
Super_Hippo Super_Hippo

2015/3/11

#
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.
danpost danpost

2015/3/11

#
Super_Hippo wrote...
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).
Giantmac Giantmac

2015/3/11

#
thanks a lot ! what does "(student)" do? does it invoke the student.class ?
Super_Hippo Super_Hippo

2015/3/11

#
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.
Giantmac Giantmac

2015/3/12

#
thank you ! ^^
You need to login to post a reply.