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

2015/10/21

How do I get my sound to play when i want it to play and stop when i want it to stop?

Myopunk119 Myopunk119

2015/10/21

#
Im modifying the little crab scenario for my school's comp sci class. On my 3rd level i want it to play certain sounds and music at certain points when conditions are met in the world. When i get to that level it won't work. Help. Code for lvl not working is pasted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import greenfoot.*;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Digdug here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Digdug extends World
{
    /*
     * DD = Intro
     * DDOM= One left
     * DDEOG = End
     * DDW = Win
     */
    private GreenfootSound DD;
    private GreenfootSound DDOM;
    private GreenfootSound DDEOG;
    private GreenfootSound DDW;
    public void sounds()
    {
        GreenfootSound DD = new GreenfootSound ("DD.wav");
        GreenfootSound DDOM = new GreenfootSound ("DD_OneMore.wav");
        GreenfootSound DDEOG = new GreenfootSound ("DD_EOG.wav");
        GreenfootSound DDW = new GreenfootSound ("DD_Win.wav");
        DD.play();
    }
    public void LvlChange()
    {
        int actors = numberOfObjects();
        if(actors == 3)
        {
            DD.stop();
            DDOM.play();
        }
             
        if(actors == 2)
        {
            DDOM.stop();
            DDW.play();
            Greenfoot.setWorld(new Pacman());
        }
    }
    /**
     * Constructor for objects of class Pacman.
     *
     */
    public Digdug()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1150, 600, 1);
        addObject(new Digdugman(),Greenfoot.getRandomNumber(1150),Greenfoot.getRandomNumber(600));
        addObject(new Frygar(),Greenfoot.getRandomNumber(1150),Greenfoot.getRandomNumber(600));
        populate();
    }
    public void populate()
    {
        for(int i=0;i < 2; i++)
        {
            addObject(new Digdugmon(),Greenfoot.getRandomNumber(1150),Greenfoot.getRandomNumber(600));
        }
    }
    public void act()
    {
        LvlChange();
    }
}
davmac davmac

2015/10/21

#
Well, you initialise all your sound objects in a method called 'sounds()', but it's not clear if you ever call that method from anywhere. Other than that, because you aren't clear about what you mean by "won't work", It's too difficult to help you. Many things could be wrong. If you put more effort into explaining the problem then someone might be able to help you. To start with, what do you mean by "won't work"? - what happens?
danpost danpost

2015/10/21

#
The sounds initialized in the 'sounds()' method are assigned to local variables and are NOT set to the fields declared in lines 18 through 21. Do not declare new variables in the method, just assign the fields you already have. That is, for example:
1
2
3
4
// in the method, instead of
GreenfootSound DD = new GreenfootSound ("DD.wav");
// use
DD = new GreenfootSound ("DD.wav");
You need to login to post a reply.