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

2014/12/7

Click on a actor and let it move by clicking

exhibition exhibition

2014/12/7

#
hello, I'm making a game, the goal is that you can direct a ship with your mouse. i already finished the part where you can select the object and can direct it. but somehow it is not working correctly. The object doesnt go where i want it to go. its always 1 or 2 centimeter from the point where i click. And when i click on my object its change its direction too. this is my current code: public void move1() { if (state == true) { if (Greenfoot.mouseClicked(this)) { state = false; } } else if (state == true) { if(Greenfoot.getMouseInfo() != null) { if(Greenfoot.getMouseInfo().getButton() == 1) { mouseX = Greenfoot.getMouseInfo().getX(); mouseY = Greenfoot.getMouseInfo().getY(); turnTowards(mouseX, mouseY); } } } } I hope you can help me out
danpost danpost

2014/12/7

#
Here is what you have: you have several Ship objects that only one of which can be moved by mouse-clicks at any time. Obviously, this involves all the ships, not just one; so, the Ship class is not the place for the required code. The other object that will be clicked on is the World object. Since all the ships will be in this world, that would seem the logical place to put the required code. Since you need to distinguish which Ship object is, as I will call it, 'active', a reference field to hold that active ship would do nicely. When a ship is clicked on, it should set this World subclass 'activeShip' field to itself. The condition for a ship to move should be whether this 'activeShip' field hold a reference to it or another ship (or null). A 'get' and 'set' method can be placed in the World subclass to deal with the field. As an alternative to the Ship class detecting clicks and setting and getting the value of the 'activeShip' field in the world class, the world is capable of doing most of it (if not all of it) by itself. This would eliminate the need for the 'get' and 'set' methods.
exhibition exhibition

2014/12/7

#
Well thanks i got the solution. But I have an other problem now, When i select the ship and try to Deselect it the object goes to the position of the mouse. When the mouse clicked on the object to deselect is the object changes its direction. But i dont want that to happen because i only want to deselect it. The 'deselection' works but the 'direction change' shouldnt happen. So basicly what i want is that the first thing he does is that he checks if its deselected and if not it changes its direction, public void move1() { if (state == true) { if (Greenfoot.mouseClicked(this)) { state = false; } } else if (state == true) { if(Greenfoot.getMouseInfo() != null) { if(Greenfoot.getMouseInfo().getButton() == 1) { mouseX = Greenfoot.getMouseInfo().getX(); mouseY = Greenfoot.getMouseInfo().getY(); turnTowards(mouseX, mouseY); } } } } i tried this but it doesnt work.
danpost danpost

2014/12/7

#
You will need to save the location of the 'move-to' mouse click and use the saved location as the target to move toward instead of the changing location of the mouse-clicks.
exhibition exhibition

2014/12/7

#
I've no clue how to do that to be honest, im a beginner in greenfoot and java. Also is there a way to use the mouseclick function to the world instead of this. what i mean: if (Greenfoot.mouseClicked(this)) will change in if (Greenfoot.mouseClicked(World.name or not.this)) I was wondering if you can do something like this.
danpost danpost

2014/12/7

#
As far as missing the location clicked when moving: the 'turnToward' method sets an 'int' rotation to the actor that puts it on a line as close to the point as possible. Your point may end up between two consecutive int values of rotation, which explains the centimeter or two off your click. You can use the method above (however, your actor moves faster than 1 pixel at a time, it could end up jumping back and forth other the destination point when it gets there, unless you add some extra code to deal with that issue) or you can incorporate a smooth-mover system to the actor, which can pretty much eliminate missing the destination point. With you as a beginner, it may be best to go with the first mentioned approach and not worry to much about getting the behavior exact; but, just getting it working to begin with. With mouseclicks on the world:
if (Greenfoot.mouseClicked(getWorld())
would detect a click on the world from an actor class.
exhibition exhibition

2014/12/7

#
if (Greenfoot.mouseClicked(getWorld()) isnt working somehow. Or do you have to type your own world? like: if (Greenfoot.mouseClicked(getWorld.World1())
danpost danpost

2014/12/7

#
I would think it would work. It is the same as saying 'if (Greenfoot.mouseClicked(this))' from the world class. Are you getting an error message of any kind? if so, what? I did miss a closing round bracket at the end. Try this:
if (Greenfoot.mouseClicked(getWorld()))
exhibition exhibition

2014/12/7

#
its working perfectly fine now, thanks! I noticed that the movement of the ships are not really exact. I've absolutely no clue how to make it more smooth. Can you help me with that or suggest anything? right now i have this: public void move1() { if(Greenfoot.getMouseInfo() != null) { if (Greenfoot.mouseClicked(getWorld())) { mouseX = Greenfoot.getMouseInfo().getX(); mouseY = Greenfoot.getMouseInfo().getY(); turnTowards(mouseX, mouseY); } } }
danpost danpost

2014/12/7

#
At least two things can cause missing the target destination (1) the limitation of rotation to int values and (2) the limitation of pixels on your screen. How fast your actor moves can limit direction because of (2). For example, at a speed of 1, you actor can only go at a multiple of 45 degrees to 8 possible locations at a time -- the pixels to either side, above or below and to the four nearest pixels along the diagonals. At a speed of 2, you cannot add more than 8 new possible directions to go in; so, the number of possible directions your actor can go is still quite limited. You can either do as I suggested above and save the mouseclick location coordinates in fields to turnToward continuously; or you can implement a smooth-mover system.
You need to login to post a reply.