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

2020/5/17

how would i fix a void type not allowed here issue

1
2
3
4
Weeb2.exe Weeb2.exe

2020/5/17

#
so as asked above how would i fix it here?
public void fire()
    {
        if (detectHuman(<the issue is the void here>) == true)
        {
            shoot();
            
        }
    }
danpost danpost

2020/5/17

#
Show detectHuman method.
Weeb2.exe Weeb2.exe

2020/5/17

#
this is the detectHuman method
public void detectHuman()
    {
        int dist = 100;
        Actor closest = null;

        if(!getObjectsInRange(dist, Betty.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, Betty.class))
            {
                Actor guy = (Actor) obj;
                int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
                if (closest == null || guyDist< dist)
                {
                    closest = guy;
                    dist = guyDist;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }   
    }
danpost danpost

2020/5/18

#
Your fire method treats the detectHuman method as if it returns a boolean value, when in fact, it does not. Side note: closest in detectHuman could still be null by line 18.
Weeb3.exe Weeb3.exe

2020/5/18

#
danpost wrote...
Your fire method treats the detectHuman method as if it returns a boolean value, when in fact, it does not. Side note: closest in detectHuman could still be null by line 18.
so how would i fix it?
danpost danpost

2020/5/18

#
Weeb3.exe wrote...
so how would i fix it?
You could invoke fire from the detectHuman method (instead of from act).
Weeb3.exe Weeb3.exe

2020/5/18

#
im a bit confused could you explain how i would do that?
danpost danpost

2020/5/18

#
Weeb3.exe wrote...
im a bit confused could you explain how i would do that?
What did you try?
Weeb3.exe Weeb3.exe

2020/5/18

#
if (detectHuman())
        {
            shoot();
        }
but now it says it cant convert to boolean
danpost danpost

2020/5/18

#
Weeb3.exe wrote...
<< Code Omitted >> but now it says it cant convert to boolean
Provide entire method.
Weeb3.exe Weeb3.exe

2020/5/18

#
robot enemy class
public class RobotEnemy extends EnemyCharacters
{
    private GreenfootImage Robot1= new GreenfootImage("RobotEnemyIdle.png"); 
    private GreenfootImage Robot2= new GreenfootImage("RobotEnemy1.png");
    private GreenfootImage Robot3= new GreenfootImage("RobotEnemy2.png");
    private int animatecounter;
    public int fireTimer = 0;
    private boolean fire = false;
    /**
     * Act - do whatever the RobotEnemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        RobotMovement();
        animate();
        if (detectHuman())
        {
            shoot();
        }
        detectBillyMetal();
        detectBilly();
    }

    private void RobotMovement()  
    {

    }

    public void animate() 
    {

        animatecounter++;
        if (animatecounter > 5) {
            animatecounter = 0;

            if (getImage() == Robot2) {
                setImage(Robot3);
            }
            else  
            {
                setImage(Robot2); 
            }

        }
    }
    public void fire()
    {
        
    }
    public void shoot()
    {
        if ( fire == false)
        {
            fireTime();
            fire = true;
            fireTimer = 25;

        }
    }
    public void fireTime()
    {
        
        EnemyBullet bullet = new EnemyBullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.move(06);
    }
}
and detect human?
public class EnemyCharacters extends Actor
{
    /**
     * Act - do whatever the EnemyCharacters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }
    public void detectHuman()
    {
        int dist = 100;
        Actor closest = null;

        if(!getObjectsInRange(dist, Betty.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, Betty.class))
            {
                Actor guy = (Actor) obj;
                int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
                if (closest == null || guyDist< dist)
                {
                    closest = guy;
                    dist = guyDist;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }   
    }
    public void detectBillyMetal()
    {
        int dist = 100;
        Actor closest = null;

        if(!getObjectsInRange(dist, BillyMetal.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, BillyMetal.class))
            {
                Actor guy = (Actor) obj;
                int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
                if (closest == null || guyDist< dist)
                {
                    closest = guy;
                    dist = guyDist;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }   
    }
    public void detectBilly()
    {
        int dist = 100;
        Actor closest = null;

        if(!getObjectsInRange(dist, Billy.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, Billy.class))
            {
                Actor guy = (Actor) obj;
                int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
                if (closest == null || guyDist< dist)
                {
                    closest = guy;
                    dist = guyDist;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }   
    }
}
Weeb3.exe Weeb3.exe

2020/5/18

#
what method do you need?
danpost danpost

2020/5/18

#
Weeb3.exe wrote...
what method do you need?
Remove lines 17 thru 20. Replace lines 28, 48 and 68 with:
if (closest != null)
{
    turnTowards ...
    shoot();
}
Weeb3.exe Weeb3.exe

2020/5/18

#
which class?
Weeb3.exe Weeb3.exe

2020/5/19

#
danpost wrote...
Weeb3.exe wrote...
what method do you need?
Remove lines 17 thru 20. Replace lines 28, 48 and 68 with:
if (closest != null)
{
    turnTowards ...
    shoot();
}
Which class do i put it in?
There are more replies on the next page.
1
2
3
4