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

2016/12/9

Turn Towards

BaccaKing1 BaccaKing1

2016/12/9

#
I can't get the position of my "Shooter" from my "asteroid" that i am having turn towards "Shooter"
danpost danpost

2016/12/9

#
First, the asteroid must be in a world
if (getWorld() != null)
Then, a Shooter object must be in the same world:
{
    if ( ! getWorld().getObjects(Shooter.class).isEmpty())
    {
Now, we can get a reference to the Shooter object and turn towards it:
        Actor shooter = getWorld().getObjects(Shooter.class).get(0);
        turnTowards(shooter.getX(), shooter.getY());
    }
}
BaccaKing1 BaccaKing1

2016/12/9

#
the .isEmpty is not working it says "cannot find symbol- method isEmpty()".
danpost danpost

2016/12/9

#
BaccaKing1 wrote...
the .isEmpty is not working it says "cannot find symbol- method isEmpty()".
Show the code you are using. The code provided should work; so, it must be how you incorporated it into your code. Show at least the code for the method you placed it in.
BaccaKing1 BaccaKing1

2016/12/12

#
public World w = getWorld();
    public boolean start = true;
    public void act() 
    {
        move(1);
        if(isAtEdge())
        {
            //w.removeObject(this);
        }
        if(start)
        {
            spawn();
            start = false;
        }
    }
    public void spawn()
    {
        if(getWorld() != null)
        {
            if(!getWorld().getObjects(Shooter.class.isEmpty()))
            {
                 Actor shooter = getWorld().getObjects(Shooter.class).get(0);   
                 turnTowards(w.getWidth()/2, w.getHeight()/2);
            }
        }
    }
danpost danpost

2016/12/12

#
Remove line 1 -- 'w' will be 'null' there. Replace 'w' throughout your code with 'getWorld()'.
Nosson1459 Nosson1459

2016/12/13

#
danpost wrote...
Remove line 1 -- 'w' will be 'null' there. Replace 'w' throughout your code with 'getWorld()'.
... and line 20 should read
if(!getWorld().getObjects(Shooter.class).isEmpty())
NOT
if(!getWorld().getObjects(Shooter.class.isEmpty()))
you have to move the parenthesis.
BaccaKing1 BaccaKing1

2016/12/13

#
Nosson1459 wrote...
danpost wrote...
Remove line 1 -- 'w' will be 'null' there. Replace 'w' throughout your code with 'getWorld()'.
... and line 20 should read
if(!getWorld().getObjects(Shooter.class).isEmpty())
NOT
if(!getWorld().getObjects(Shooter.class.isEmpty()))
you have to move the parenthesis.
the last one is for the end of the if the next one is for getobjects and the next two are isEmpty Danpost help!!!!
danpost danpost

2016/12/13

#
The 'isEmpty' method call does not work on a Class object -- it works on a List object. The 'getObjects' parameter should end after the class. It returns a List object which you then can call 'isEmpty' on: getObjects(Shooter.class).isEmpty() is correct as Nosson1459 pointed out.
Nosson1459 Nosson1459

2016/12/14

#
danpost wrote...
getObjects(Shooter.class).isEmpty() is correct as Nosson1459 pointed out
If you would have tried it after I told you to, you would have seen that it works.
BaccaKing1 BaccaKing1

2016/12/14

#
i'm not the brightest that's why i am asking for help (high school freshman that started greenfoot about a month ago with a teacher that doesn't really know what he is teaching)
BaccaKing1 BaccaKing1

2016/12/14

#
okay i fixed it but they're not completely pointing towards my shooter they're just going to the side of it passing right by it
danpost danpost

2016/12/14

#
BaccaKing1 wrote...
okay i fixed it but they're not completely pointing towards my shooter they're just going to the side of it passing right by it
Couple ways to improve aim. Save original location and increase the distance to move from that spot each act is one way. Saving current location with more precision by using double fields or fractioinal units is another.
You need to login to post a reply.