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

2020/8/31

Background sound fades when another sound is played

Libraoct7 Libraoct7

2020/8/31

#
I have created a simple game in which an object (boy) eats all the cherries avoiding snakes and bombs. When the game starts, a background sound is played. When the boy eats a cherry, a pop sound is played. But when the pop sound is played, the background music is faded where you can't even hear it until about 7 seconds later that would the background sound is heard. Please help me to fix this problem. This is my MyWorld class:
public class MyWorld extends World
{
    GreenfootSound bksound = new GreenfootSound("background.mp3");
    public static int score;
    public void act()
    {
        bksound.play();
        showText("Score: " + score, 550, 25);
        if(score == 10)
        {
            addObject(new YouWin(), 300, 200);
            Greenfoot.playSound("Cheering.wav");
            bksound.stop();
            Greenfoot.stop();
        }
        
    }
This is the Cherries class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cherry here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cherries extends Actor
{
    /**
     * Act - do whatever the Cherry wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //int counter=0;
    public void act() 
    {
        // Add your action code here.
    }
    public void Cherries()
    {
        getImage().scale(getImage().getWidth()/2, getImage().getHeight()/2);
    }

}
And this the boy class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class boy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class boy extends Actor
{
    MyWorld total;
    /**
     * Act - do whatever the boy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move();
        touching();
    }
    public void move()
    {
       if (Greenfoot.isKeyDown("up"))  
       {
           setLocation(getX(), getY()-2);
       }
       if (Greenfoot.isKeyDown("down"))
       {
           setLocation(getX(), getY()+2);
       }
       if (Greenfoot.isKeyDown("right"))  
       {
           setLocation(getX()+2, getY());
       }
       if (Greenfoot.isKeyDown("left"))  
       {
           setLocation(getX()-2, getY());
       }    
    }
    public void touching()
    {
        if (isTouching (bomb.class))
        {
            Greenfoot.playSound("explosions.wav");
            getWorld().addObject(new YouLose(), 300, 200);
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
        else if (isTouching (snake.class))
        {
            Greenfoot.playSound("snakehit.wav");
            getWorld().addObject(new YouLose(), 300, 200);
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
        else if (isTouching (Cherries.class))
        {
            Greenfoot.playSound("Pop sound effect.wav");
            total.score++;
            removeTouching(Cherries.class);
        }
    }
}
You need to login to post a reply.