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

2012/4/1

Playing sound if not playing?

nooby123 nooby123

2012/4/1

#
I'm trying to play a sound if the sound isn't playing so they don't sound overlapped. Here's my code:
public class Asteroids extends Objects {

    private int move, size, turn;
    public static boolean blow_up = false;
    static int luck = 10; 
    GreenfootSound split = new GreenfootSound("break.mp3");
    
    public Asteroids(int size, int move, int direction, int rotation) {   
        setRotation(direction);        
        this.move = move;        
        this.size = size;
        this.turn = turn;
        GreenfootImage image = getImage();
        image.scale(size, size);
        image.rotate(rotation);
        setImage(image);
    }
    
    public void act() {      
        if(Space.start) {
            if(Greenfoot.isKeyDown("left")) setLocation(getX() + Space.speed, getY());
            else if(Greenfoot.isKeyDown("right")) setLocation(getX() - Space.speed, getY());                 
        }
        if(Space.start) move(move);       
        if(Space.shake) move(Space.move);   
        check_hit();      
    }   
    
    private void check_hit() {
        if(size < 5 || getY() > getWorld().getHeight() - 10) {
            getWorld().removeObject(this); 
            return;         
        }
        Actor bullet = getOneIntersectingObject(Bullet.class);
        Actor ship = getOneIntersectingObject(Ship.class);     
        Actor laser = getOneIntersectingObject(Laser.class);    
        if(bullet!=null) {              
            if(Greenfoot.getRandomNumber(100) < luck && size < 15) getWorld().addObject(new Bonus(Greenfoot.getRandomNumber(2) + 1), getX(), getY());
            Ship.score += size/10;
            split(); 
            return;
        } 
        else if(laser!=null && Laser.transparency > 250) {            
            Ship.score += size/2;
            split();  
            return;
        }
        else if(ship!=null) {
            Ship.health -= 2;  //subtracts health
            Stat.show = true;      
            split();
            return;
        }        
        if(blow_up) {split(); return;}
        if(getX() < 2 || getX() > getWorld().getWidth() - 2) {getWorld().removeObject(this); return;}
    } 
    
    private void split() {        
        if(split.isPlaying()) Greenfoot.playSound("" + split);
        getWorld().addObject(new Asteroids(size/2, Greenfoot.getRandomNumber(3) + 1, Greenfoot.getRandomNumber(45) + 45, Greenfoot.getRandomNumber(360)), getX(), getY());
        getWorld().addObject(new Asteroids(size/2, Greenfoot.getRandomNumber(3) + 1, Greenfoot.getRandomNumber(45) + 45, Greenfoot.getRandomNumber(360)), getX(), getY());
        blow_up = false;
        getWorld().removeObject(this);          
    }
}
But it keeps saying: java.lang.IllegalArgumentException: Could not open sound file: greenfoot.GreenfootSound@1157edf file: break.mp3 . Is playing: false at greenfoot.sound.SoundExceptionHandler.handleIOException(SoundExceptionHandler.java:66) at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:115) at greenfoot.sound.SoundFactory.getCachedSound(SoundFactory.java:130) at greenfoot.Greenfoot.playSound(Greenfoot.java:163) at Asteroids.split(Asteroids.java:67) at Asteroids.check_hit(Asteroids.java:48) at Asteroids.act(Asteroids.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) Caused by: java.io.FileNotFoundException: Could not find file: greenfoot.GreenfootSound@1157edf file: break.mp3 . Is playing: false at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:532) at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:95) ... 9 more Help!
danpost danpost

2012/4/1

#
You already have 'split' as a GreenfootSound object; just use 'split.play();'. Also, your check should be 'if NOT playing' (not 'if playing').
nooby123 nooby123

2012/4/1

#
Thanks!
You need to login to post a reply.