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

2013/1/30

Move Towards

wafflepudding wafflepudding

2013/1/30

#
I have two classes, Bullet and Crosshairs. They both extend another class, Methods. Assuming that Crosshairs could be anywhere, how can I make Bullet move towards Crosshairs when it is created?
MatheMagician MatheMagician

2013/1/30

#
You can set the rotation of the bullet so that it is facing the Crosshairs and then use the move(int speed) to travel towards it. You can do this using the turnTowards(int x,int y) method. Put something like this in your addedToWorld() method:
if(getWorld().getObjects(Crosshair).length>0)
{
      Actor crosshair = (Actor)(getWorld().getObjects(Crosshair.class).get(0));
      turnTowards(crosshair.getX(),crosshair.getY());
}
and in your act method, just add the move method
move(2);
danpost danpost

2013/1/30

#
Moving only a distance of '2' will only produce up to 12 different direction of movement (more, if you are using 'double's to keep track of the location of the bullet). If the Crosshair object is some distance away, you will miss it more times than not. Better would be to save the location of the Crosshair object from within the addedToWorld method; and do both turnTowards and move from within the act, turning toward the saved coordinates of where to Crosshair object was when the bullet was created.
MatheMagician MatheMagician

2013/1/31

#
@Danpost, I believe you would run into some problems if you used your way. After the bullet passed the original coordinates of the mouse, it would turn back towards them and get stuck. I do see your point though. I just put 2 there as a ridiculous number, assuming wafflepudding would change it to whatever he needed. A more realistic speed would be more than 10 and would be much more accurate.
danpost danpost

2013/1/31

#
@MatheMagician, true. The target would have plenty of time to move out of the target zone if far enough away. But it would make a good smart bullet if you saved to target object and adjusted to its location each act.
wafflepudding wafflepudding

2013/1/31

#
How would I create an addedToWorld() method? I understand the purpose, but I just wouldn't know how to do it.
danpost danpost

2013/1/31

#
public void addedToWorld(World world)
{
    // put code provided by MatheMagician here
}
wafflepudding wafflepudding

2013/1/31

#
public class Bullet extends Methods
{
    public void act() 
    {
         addedToWorld(World);
         move(10);
    }    
    public void addedToWorld(World world)
    {
        if(getWorld().getObjects(Crosshair).length>0)  
        {  
            Actor crosshair = (Actor)(getWorld().getObjects(Crosshair.class).get(0));  
            turnTowards(crosshair.getX(),crosshair.getY());  
        }
    }    
}
It says 'cannot find symbol - variable World'. I also tried 'addedToWorld()' and 'addedToWorld(World world)', to which I received the errors 'cannot find symbol - addedToWorld' and ') expected' respectively.
danpost danpost

2013/1/31

#
Remove line 5 from your code. The addedToWorld method is executed automatically when an object is 'addObject'ed into the world. You do not call it yourself.
wafflepudding wafflepudding

2013/1/31

#
Thanks, fixed it. But line ten, it says it can't find variable Crosshair. Changed it and all the rest to Crosshairs, (that's what it is), same problem.
danpost danpost

2013/1/31

#
Only change the one in line 10 to 'Crosshairs.class'. The rest should not have been changed. EDIT: except for line 12 with 'getObjects(Crosshairs.class)'.
wafflepudding wafflepudding

2013/1/31

#
if(getWorld().getObjects(Crosshairs.class).length>0) Now it can't find the variable length. Do I need to create it?
danpost danpost

2013/1/31

#
That should be 'size()'. However, better would be:
if(!getWorld().getObjects(Crosshairs.class).isEmpty())
wafflepudding wafflepudding

2013/1/31

#
It works! I used the !getWorld question. I really appreciate all the time you've spent helping me with this, and I'm also definitely going to make a smart rocket with the following code. Both of you were extremely helpful.
You need to login to post a reply.