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

2015/10/2

How do you make the crab give a certain sound after it turns more than 18 degrees?? It already moves when the left/right keys are pressed. What code do I add? PLEASE HELP!!!

Mack Mack

2015/10/2

#
How do you make the crab give a certain sound after it turns more than 18 degrees?? It already moves when the left/right keys are pressed. What code do I add? PLEASE HELP!!!
public class Crab extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten;
    
    /**
     * Create a crab and initialize its two images.
     */
    public Crab()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
        wormsEaten = 0;
    }
        
    /** 
     * Act - do whatever the crab wants to do. This method is called whenever
     *  the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (isAtEdge() )
        { turn(5);
        } 
        checkKeypress();
        move(5);
        lookForWorm();
        switchImage();
    }
    public void turnAtEdge()
    {
        if ( isAtEdge() ) 
        {
            turn(17);
        }
    }
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) 
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }
   
    /**
     * Alternate the crab's image between image1 and image2.
     */
    public void switchImage()
    {
        if (getImage() == image1) 
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
            
    /**
     * Check whether a control key on the keyboard has been pressed.
     * If it has, react accordingly.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left")) 
        {
            turn(-4);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(4);
        }
    }
    
    /**
     * Check whether we have stumbled upon a worm.
     * If we have, eat it. If not, do nothing. If we have
     * eaten eight worms, we win.
     */
    public void lookForWorm()
    {
        if ( isTouching(Worm.class) ) 
        {
            removeTouching(Worm.class);
            Greenfoot.playSound("slurp.wav");
            
            wormsEaten = wormsEaten + 1;
            
            if (wormsEaten == 8) 
            {
                Greenfoot.playSound("fanfare.wav");
                Greenfoot.stop();
            }
        }
    }
}
danpost danpost

2015/10/3

#
Mack wrote...
How do you make the crab give a certain sound after it turns more than 18 degrees?? It already moves when the left/right keys are pressed. What code do I add?
Please be more specific. Eighteen degrees is specific enough; however, from what, how often -- every 18 degrees of turning? Might be a good idea to list here the rotational values at which you want the sound to play or if it is the sum of rotational change of both left and right turning that you want for playing the sound. Either way, it is not to difficult to code. The easier one to code is for every 18 degrees. The following has an undeclared method 'playTurnSound' which is used to replace actual code you would use. Simply by checking the rotational range before turning with the range after turning:
// before turning
int beforeRange = getRotation()/18;
// after turning
if (beforeRange != getRotation()/18) { playTurnSound(); }
You need to login to post a reply.