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

2020/1/26

Need Help with Arrays

samnan22 samnan22

2020/1/26

#
Hi, I am trying to make a simulation in which every time my Bee object touches a circle object a random fact about bees from an array is displayed, and it stays there until the bee touches another one of the circle objects, maybe even a different one. My code is below. The current problem I have is that the facts are being displayed and switched at a very fast rate. Below the code for the Bee class is attached.
public class DroneBee extends Actor
{
    
    
    private String[] facts = 
        {"All drone bees are male!","Honey bee can fly 15mph!", "Honey Bee measure about 15 mm long!", "Honey bees communicate with each other by dancing!", "ALl worker bees are female!", "If a bee stings you, they die!", "Bees have five eyes!", "Bees have 2 pairs of wings!", "Bees live about 4 weeks!", "Bees have two stomachs!"}; 
    public DroneBee()
    {
        
        
    }
    public void act() 
    {
        int i = 0; 
        for(int k = 0; i < Greenfoot.getRandomNumber(10); k++)
        {
            i++;
        }
        keyControls();
        if(isTouching(Circle.class))
        {
            
            getWorld().showText(""+facts[i],200,200);
            
            
        }
        else
        {
            getWorld().showText("", 200,200);
            
        }
    }    
     public void keyControls()
        {
            
            if (Greenfoot.isKeyDown("up")) 
        {
                setRotation(270); 
                setLocation(getX(), getY()-2);
        }
        
        if (Greenfoot.isKeyDown("down")) 
        {
                setRotation(90);
                setLocation(getX(), getY()+2);
            
            
        }
     
        if (Greenfoot.isKeyDown("left")) 
        {
                setRotation(180);
                setLocation(getX()-2, getY());
            
            }
        if (Greenfoot.isKeyDown("right")) 
        {
                setRotation(0);
                setLocation(getX()+2, getY());
            
        }
    }
    
}

danpost danpost

2020/1/27

#
You want the text to change when the state of touching a circle goes from not touching one to touching one (not just touching). Therefore, you need to keep track of what state it is in. A boolean field should suffice. Compare the value of the field to current state to detect a change. When change occurs, change text only if new state is touching.
samnan22 samnan22

2020/1/29

#
I have added that. However, now the facts don't change and I am stuck with the same fact coming and going. How do I change it without the speed increasing rapidly
public class DroneBee extends Actor
{
    
    public boolean isTouching = false; 
    private String[] facts = 
        {"All drone bees are male!","Honey bee can fly 15mph!", "Honey Bee measure about 15 mm long!", "Honey bees communicate with each other by dancing!", "ALl worker bees are female!", "If a bee stings you, they die!", "Bees have five eyes!", "Bees have 2 pairs of wings!", "Bees live about 4 weeks!", "Bees have two stomachs!"}; 
    public DroneBee()
    {
        
        
    }
    public void act() 
    {
        if(isTouching(Circle.class))
        {
            isTouching = true; 
        }
        else
        {
            isTouching = false; 
        }
        
        
        keyControls();
        if(isTouching==true)
        {
            int i = 0; 
            getWorld().showText(""+facts[i],200,200);
            i++;
            
        }
        else
        {
            getWorld().showText("", 200,200);
            
        }
    }    
     public void keyControls()
        {
            
            if (Greenfoot.isKeyDown("up")) 
        {
                setRotation(270); 
                setLocation(getX(), getY()-2);
        }
        
        if (Greenfoot.isKeyDown("down")) 
        {
                setRotation(90);
                setLocation(getX(), getY()+2);
            
            
        }
     
        if (Greenfoot.isKeyDown("left")) 
        {
                setRotation(180);
                setLocation(getX()-2, getY());
            
            }
        if (Greenfoot.isKeyDown("right")) 
        {
                setRotation(0);
                setLocation(getX()+2, getY());
            
        }
    }
    
}
.
samnan22 samnan22

2020/1/29

#
never mind, i got it!
You need to login to post a reply.