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

2012/10/26

How can I make my "gun" point towards the crosshair?

pashmi pashmi

2012/10/26

#
I have a lighthouse (yes, I know) pointing towards a "bleeper". At the moment I have a standard: if (Greenfoot.isKeyDown("d")) turn (-1) if (Greenfoot.isKeyDown("a")) turn (1) In the lighthouse to compliment the movement script (simple left and right move -8/8) so it LOOKS like the lighthouse is pointing to the bleeper. However, this illusion breaks quickly when in use and I have to reset the lighthouse every time I want to test (don't want to save the world unfinished). Can anyone suggest away my lighthouse can "turnToward" the x and y coordinates of the bleeper? Thanks in advance for any help-please try to keep it simple as this is my first school computing project :)
pashmi pashmi

2012/10/26

#
Also I figured the basics of what might be the mouse version, however it will not work.
1
2
3
4
int x = Greenfoot.MouseInfo.getX
int y = Greenfoot.MouseInfo.getY
 
turnTowards(x, y);
Is this code fixable easily?
Gevater_Tod4711 Gevater_Tod4711

2012/10/26

#
getX() and getY() are methods so you need to put "()" after them. And every line has to end with ; I don't know if this code will work then bekause getX() and getY() are probably non-statics. But this code should fix your problem:
1
2
3
4
5
6
//write this in your act method;
MouseInfo mouse = Greenfoot.getMouseInfo();
 
if (mouse != null) {
    turnTowards(mouse.getX(), mouse.getY());
}
danpost danpost

2012/10/26

#
As far as the mouse version, getX and getY are methods in the MouseInfo class and must, therefore, be followed by parenthesis (getX() and getY()). These methods must be used on a MouseInfo object (not the MouseInfo class, as you are trying to do here). First, get a MouseInfo object, then call the methods on it. Refer to the Greenfoot API documentation. Also, java requires a semi-colon after every statement.
danpost danpost

2012/10/26

#
Getting a MouseInfo object is a lot easier than getting a "bleeper" object. The Greenfoot class has a method to get the MouseInfo object, but there is no specific method to get a "bleeper" object. The World class in Greenfoot does have methods that create lists of objects of a specified type, however. When using one of these, you must make certain the list is not empty before getting an object from the list. In pseudo-code:
1
2
3
4
5
if (the list of bleeper class objects is not empty)
{
    get the first object from the list of bleeper class objects;
    turnTowards(x of bleeper, y of bleeper);
}
The objects listed with those methods are Object class objects; and Actor class methods (getX() and getY()) cannot be performed on them without type-casting as an Actor class object or a Bleeper class object.
pashmi pashmi

2012/10/28

#
This explains a lot, thanks :) I'll try this when I get to a pc/mac that actually runs voids properly ATM (and trust me I'm not doing anything wrong) my mac that I'm working on (someone else's) can not run specified voids/methods from the constructor e.g eat() move() these will not run and I believe it is something to do with not having the latest java-also, if any of you work on a macbook/mac, is java safe-or are you in danger of flashback?
pashmi pashmi

2012/10/28

#
So are we creating a mouse object and setting that to Greenfoot.getMouseInfo();? That makes alot more sense than what I was doing :P
You need to login to post a reply.