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

2018/1/27

HELP

nick_crompton nick_crompton

2018/1/27

#
for some reason line 32 wont execute and line 38 happens twice, which it should not.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class lobster here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class lobster extends Animal
{
    public int speed = 1;
    private boolean react = false;
    /**
     * Act - do whatever the lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       
       
        chaseCrab();
       
    }
public void chase()
{        turnTowards(Crab.crabX, Crab.crabY);
        move(speed);
    }
    public void chaseCrab()
    {
            if(!react && !getObjectsInRange(200, foot.class).isEmpty())
    {
        move(speed);
       react=true;
       
    turn(180);
    
    
}
    else 
      {  
       
        chase();
       
    }
     die();
     
    
    }


    
    
    public void die()
    {
    Actor foot = getOneIntersectingObject(foot.class);
    
    if(foot!=null)
    {   getWorld().removeObject(foot);
        getWorld().removeObject(this);
        
    }
    }
 
}
Super_Hippo Super_Hippo

2018/1/27

#
What lines are executed twice or are not executed? The lines in the title don't seem to make sense.
nick_crompton nick_crompton

2018/1/27

#
35 happens twice and 32 doesnt execute
nick_crompton nick_crompton

2018/1/27

#
the lobster turns 180 degrees and then 180 degrees again, on top of that when i delete line 42 i see that the lobster turns 180 degrees once but does not move at all.
danpost danpost

2018/1/28

#
nick_crompton wrote...
the lobster turns 180 degrees and then 180 degrees again, on top of that when i delete line 42 i see that the lobster turns 180 degrees once but does not move at all.
I probably does move 1 pixel, which is hardly noticeable. It will not move further if you remove line 42 because you set the boolean to true and there would be no code to process in the 'else' part if you removed that line. With the line, your lobster should turn 180 for an instant (one act cycle -- again, hardly noticeable) and then turn toward the crab.
nick_crompton nick_crompton

2018/1/28

#
so how do i fix this?
danpost danpost

2018/1/28

#
nick_crompton wrote...
so how do i fix this?
Fix it to do what? You have not given any indication as to what you actually want it to do. You have only stated what you thought was happening as far as code execution -- which apparently does not match up with what the code would do, as far as I can tell.
nick_crompton nick_crompton

2018/1/28

#
i want the lobster to turn 180 degrees and move away when it sees a bullet in range. but it only turns 180 degrees and back and it does not want to move at all
danpost danpost

2018/1/28

#
You probably just need an 'else if' block in between the current 'if' and 'else' blocks which says if reacting, just move:
else if (react)
{
    move(speed);
}
You need to login to post a reply.