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

2013/3/24

adding sound to my object

1
2
geekykid2013 geekykid2013

2013/3/25

#
I have followed your instructions and the wind.wav file is now working, however it begins to play as soon even before I press the run button. I have also inserted the code in the Greenfoot.isKeyDown("left); so that when i press the left arrow key the sound play, but this does not happen
geekykid2013 geekykid2013

2013/3/25

#
public class basket extends Actor
{   int speed = 10;
    /**
     * Act - do whatever the basket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void move()
    {
        //System.out.println("move funtion called");
        /*if (Greenfoot.isKeyDown("up")){
            setLocation(getX(), getY()-1);
        }
        
        if (Greenfoot.isKeyDown("down")){
            setLocation(getX(), getY()+1);
        }*/
        if (Greenfoot.isKeyDown("left")&& getX()>10+getImage().getWidth()/2){ 
            setLocation(getX() - speed, getY());
            Greenfoot.playSound("wind.wav");
            
        }
        
        if (Greenfoot.isKeyDown("right")&& getX()<getWorld().getWidth()-getImage().getWidth()/2-10){ 
            
        }
     }
     
     public void act() 
     {
         makeSmoke(); // use this code to declare make smoke   
         move();
     }
     
      /**
     * Produce a puff of smoke
     */
    public void  makeSmoke()
    {
        getWorld().addObject ( new smoke(), getX(), getY());
        
    }
      
      //public boolean atWorldEdge()  // How to make the new object appear automatically onto the stage
      //if (getX() < 10 || getX() > getWorld().getWidth() - 10) {
        //  return true;
        // }
         //if (getY() < 10 || getY() > getWorld().getHeight() - 10) {
             // return true;
             
             //}
             //return false;
             //}
     
        
}
danpost danpost

2013/3/25

#
Where the code to play the "wind.wav" is now, it will start when you create a ball object (when you use 'new Ball()'). If you do not want it to start at that point you need to move the code starting the sound file to a different location. Where would depend on when you wanted it to start. It will not stop until the sound file has completely played; only after this will you be able to start the sound file again. To gain more control of the playing of a sound file, use the GreenfootSound constructor and methods.
// add instance field to class
GreenfootSound wind = new GreenfootSound("wind.wav");
// then when triggered to start
wind.play();
Look at the GreenfootSound class API for other available methods that can be used on the GreenfootSound objects.
geekykid2013 geekykid2013

2013/3/25

#
Thanks danpost i will try this solution. Also I'm practicing tutorial 16 greenfoot, but I get an error saying: cannot find symbol - method canSee(java.lang.class<Lettuce>). I compared it to the original code from the original download file and the code Ive written looks identical to the original code, why am I getting an error once I compile?
geekykid2013 geekykid2013

2013/3/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Turtle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turtle extends Animal
{

    private int points = 0;
    
    public void act() 
    {
        move(4);
        checkKeys();
        tryToEat();
    }    
    
    /**
     * Check whether the control keys are being pressed, and turn
     * if they are.
     */
    
    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(5);
        }
    }
    
    /**
     * Check whether we can see Lettuce or bugs. If we can, eat it.
     */
    public void tryToEat()
    {
        if (canSee(Lettuce.class) )
        {
            eat(Lettuce.class);
            points++; //when turtle eat some lettuce we get points added incrementally
            Greenfoot.playSound("slurp.wav");
        }
        
        if (canSee(Bug.class) )
        {
            eat(Bug.class);
            points = points+5; // when we eat a bug we also get 5 points added to our score
            Greenfoot.playSound("slurp.wav");
            createNewBug();
        }
        
        if (points == 15) 
        {
            gameOver();
        }
        
    }
    
    /**
     * Create a new bug and insert it at a random location in the world
     */
    private void createNewBug()
    {
        Bug newBug;
        newBug = new Bug();
        
        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        
        world.addObject(newBug, x, y);
    }
    
    /**
     * We have won the game.
     */
    public void gameOver()
    {
        Greenfoot.playSound("fanfare.wav");
        Greenfoot.stop();
    }
}
geekykid2013 geekykid2013

2013/3/26

#
Also I'm practicing tutorial 16 greenfoot, but I get an error saying: cannot find symbol - method canSee(java.lang.class<Lettuce>). I compared it to the original code from the original download file and the code Ive written looks identical to the original code, why am I getting an error once I compile?
geekykid2013 geekykid2013

2013/3/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Turtle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turtle extends Animal
{

    private int points = 0;
    
    public void act() 
    {
        move(4);
        checkKeys();
        tryToEat();
    }    
    
    /**
     * Check whether the control keys are being pressed, and turn
     * if they are.
     */
    
    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-5);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(5);
        }
    }
    
    /**
     * Check whether we can see Lettuce or bugs. If we can, eat it.
     */
    public void tryToEat()
    {
        if (canSee(Lettuce.class) )
        {
            eat(Lettuce.class);
            points++; //when turtle eat some lettuce we get points added incrementally
            Greenfoot.playSound("slurp.wav");
        }
        
        if (canSee(Bug.class) )
        {
            eat(Bug.class);
            points = points+5; // when we eat a bug we also get 5 points added to our score
            Greenfoot.playSound("slurp.wav");
            createNewBug();
        }
        
        if (points == 15) {
        
            gameOver();
        }
        
    }
    
    /**
     * Create a new bug and insert it at a random location in the world
     */
    private void createNewBug()
    {
        Bug newBug;
        newBug = new Bug();
        
        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        
        world.addObject(newBug, x, y);
    }
    
    /**
     * We have won the game.
     */
    public void gameOver()
    {
        Greenfoot.playSound("fanfare.wav");
        Greenfoot.stop();
    }
}
danpost danpost

2013/3/26

#
You do not have to double post (unless your request for help goes days without a response -- then, just post 'bump'). There does not appear to be anything wrong with the Turtle class code as far as the 'canSee' method call. Maybe the Animal class code was altered (possibly by accident). Please show the code you have in the Animal class.
You need to login to post a reply.
1
2