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

2017/11/6

shooting

Dipneop Dipneop

2017/11/6

#
how can I make an actor shoot at other actors if they are close to him?
danpost danpost

2017/11/6

#
Use the 'getObjectsInRange' method to get a list of all actor of whatever type that are within your desired range of closeness. If the list is empty (it will never be null), they no actors can be shot at. If the list has a size of one, then shoot at it. If the list has more than one, you can shoot at the first one in the list, the last one in the list, any random one from the list or determine the closest one in the list and shoot at it. To shot, have a new bullet added into the world turn towards the actor of choice.
Dipneop Dipneop

2017/11/6

#
I did:
import greenfoot.*;
import java.util.List;
public class Shooter extends Actor
{
    public void act() 
    {
        List enemy = getObjectsInRange(250, Enemy.class);
        if(enemy.size() > 0) 
        {  
            getWorld().addObject(new Bullet(), getX(), getY());
        }
    }    
}
import greenfoot.*;
public class Bullet extends Actor
{
    public void act() 
    {
        Enemy enemy = new Enemy();
        turnTowards(enemy.getX(), enemy.getY());
        move(5);
    }      
}
and it says: "Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed."
danpost danpost

2017/11/6

#
In the Shooter class, you are properly getting a list of Enemy objects within 250 cells. In the Bullet class, however, you are creating a new Enemy which is not one that was in the world (and furthermore was not placed into the world) and trying to have the new Bullet turn toward it (which not only is impossible because it is not in the world, but is not the Enemy object you previously found for the new Bullet to turn towards). Remove lines 6 and 7 from the Bullet class. You found the actor you want your new Bullet to turn towards in the Shooteer class. Replace line 10 in that class with the following lines of code:
Actor bullet = new Bullet(); // create a bullet
getWorld().addObject(bullet, getX(), getY()); // add bullet to world
Actor target = enemy.get(0); // gets first enemy in list as target
bullet.turnTowards(target.getX(), target.getY()); // turn bullet toward target
Dipne0p Dipne0p

2017/11/6

#
Now it says: "incompatible types: java.lang.Object cannot be converted to greenfoot.Actor" at get(0)
import greenfoot.*;
public class Bullet extends Actor
{
    public void act() 
    {   
        move(5);
    }
}
import greenfoot.*;
import java.util.List;
public class Shooter extends Actor
{
    public void act() 
    {
        List enemy = getObjectsInRange(250, Enemy.class);
        if(enemy.size() > 0) 
        {  
            Actor bullet = new Bullet(); // create a bullet
            getWorld().addObject(bullet, getX(), getY()); // add bullet to world
            Actor target = enemy.get(0); // gets first enemy in list as target
            bullet.turnTowards(target.getX(), target.getY()); // turn bullet toward target
        }
    }    
}
danpost danpost

2017/11/6

#
Dipne0p wrote...
Now it says: "incompatible types: java.lang.Object cannot be converted to greenfoot.Actor" at get(0) < Code Omitted >
Change line 12 to:
Actor target = (Actor)enemy.get(0);
Dipne0p Dipne0p

2017/11/6

#
Thanks a bunch!
Dipneop Dipneop

2017/11/6

#
Now it says
import greenfoot.*;
import java.util.List;
public class Shooter extends Actor
{
    public void act() 
    {
        List enemy = getObjectsInRange(250, Enemy.class);
        if(enemy.size() > 0) 
        {  
            Actor bullet = new Bullet(); // create a bullet
            getWorld().addObject(bullet, getX(), getY()); // add bullet to world
            Actor target = enemy.get(0); // gets first enemy in list as target
            bullet.turnTowards(target.getX(), target.getY()); // turn bullet toward target
        }
    }    
}
import greenfoot.*;
public class Bullet extends Actor
{
    public void act() 
    {   
        move(5);
    }
}
danpost danpost

2017/11/6

#
danpost wrote...
Dipne0p wrote...
Now it says: "incompatible types: java.lang.Object cannot be converted to greenfoot.Actor" at get(0) < Code Omitted >
Change line 12 to:
Actor target = (Actor)enemy.get(0);
It does not look like you did this.
You need to login to post a reply.