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

2017/5/5

Different Sound For Each Object When Removed

suge5 suge5

2017/5/5

#
My goal is for each animal to play a different when they are pressed on or reach the end of the world. Right now whenever the animal enters the screen it will play the noise associated with it over and over again. Thanks, Sam
abstract public class FrogsHit extends Actor
{
   public abstract void act(); //abstract to be used by all subclasses
   public void FrogHitter(){  
       //Greenfoot.playSound("Ribbit.wav");  //sound to play
        playHitSound();
       ((FrogWorld) getWorld()).countHit(this); //plays when hit and records hit
        getWorld().removeObject(this);} //removes hit object
   public abstract int getCount(); //abstract of getCount for all subclasses
   public abstract void playHitSound();
}

abstract public class Animals extends FrogsHit
{
    public abstract void act(); //abstract for act
    public abstract int getCount(); //abstract for getCount
    public abstract void playHitSound();//
}

public class SwimmingFrog extends Animals
{ 
   boolean remove=false; //creates a remove vareiable at false
   private int distance; //intializes speed
   public void act() {
        distance++; //increments speed
        setLocation(getX(), getY() -2);  //sets the x value but increases speed by 2
        if(distance%5 == 0){
            setLocation(getX()-1, getY()); //moves the frog left one for every 5 up
        }
        if (getY() == 0) {
            ((FrogWorld) getWorld()).lose(); //takes away life if a frog crosses
            remove=true; //if removed
            FrogHitter(); //removes the animal once it reaches the end of the world
        }
        playHitSound();
        // Greenfoot.playSound("Ribbit.wav");  
        // ((FrogWorld) getWorld()).countHit(this);
        // getWorld().removeObject(this);
   }
   public int getCount(){
        if(remove != true){   
            return 10; //gain 10 points if a frog crosses
        }
        return 0; //lose 0 points if frog crosses
   }
   public void playHitSound(){
        Greenfoot.playSound("Ribbit.wav");
        //((FrogWorld) getWorld()).countHit(this);
        (getWorld().removeObject(this));
        
    }
}
public class Fish extends Animals
{
   boolean remove = false; //sets boolean variable to false
   private int distance; //initializes distance variable
   public void act(){
       distance++; //increments distance
       setLocation(getX(), getY() -4); // sets x and increases speed of y by 4 
        if(distance%5 == 0){
            setLocation(getX()-1, getY()); //moves fish left as they go up
        }
        if (getY() == 0) {
            remove = true; //if removed
            FrogHitter(); //removes the animal once it reaches the end of the world
        } 
        playHitSound();
    }   
   public int getCount(){
        if(remove != true){
            return 2; // if you press on animal before it crosses you get 2 points
        }
        return 0; //if reaches end of world, you lose nothing
    }
    public void playHitSound(){
        Greenfoot.playSound("Fish.wav");
        ((FrogWorld) getWorld()).countHit(this);
        getWorld().removeObject(this);
    }
} 
danpost danpost

2017/5/5

#
Remove lines 35 and 67.
suge5 suge5

2017/5/6

#
Now I am getting a NullPointerException on lines 7 and this new class line that says Fhit.FrogHitter(); //removes the objects once pressed
public class FrogHunter extends Actor
{
    public void act(){
        if(Greenfoot.mouseMoved(null)) { //whenever the mouse is moved
            MouseInfo mouse = Greenfoot.getMouseInfo(); // obtains information from the mouse
            setLocation(mouse.getX(), mouse.getY()); // sets the locaion of the bat to where the user places the mouse in the world
        }
        if(Greenfoot.mouseClicked(null)) { //finds all ibjects
            int x = -getImage().getWidth()/2; //sets location of bat you can hit animal with
            int y = getImage().getHeight()/2; //sets location of bat you can hit animal with
            FrogsHit Fhit = (FrogsHit) getOneObjectAtOffset(x , y, FrogsHit.class); //return one object that is located at the specified cell
            if(Fhit != null) { //finds the objects
                Fhit.FrogHitter(); //removes the objects once pressed
            }
        }
    }    
}
danpost danpost

2017/5/6

#
Removes lines 48 and 49. Remove lines 77 and 78.
suge5 suge5

2017/5/7

#
Thank you
You need to login to post a reply.