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

2017/5/25

How to click menu to have sound

Archimedes007 Archimedes007

2017/5/25

#
Hello Everyone.. How can i make every click on menu there sound?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*; 
 
 
public class MenuUtama2 extends Actor
{
 
    public void act()
    {
        if(Greenfoot.mouseClicked(this))
         
     {
       Greenfoot.setWorld(new MenuUtama());
       
     }
      
    }   
}
Thats the menu code i have, what shoul i do ?
danpost danpost

2017/5/25

#
Play the sound at line 13. You need the sound file in the 'sounds' folder of the scenario -- I will call it "click.wav":
1
Greenfoot.playSound("click.wav");
Archimedes007 Archimedes007

2017/5/25

#
danpost wrote...
Play the sound at line 13. You need the sound file in the 'sounds' folder of the scenario -- I will call it "click.wav":
1
Greenfoot.playSound("click.wav");
How about when we move from menu to credit and the music contained in the menu it stops automatically ?
danpost danpost

2017/5/25

#
Archimedes007 wrote...
How about when we move from menu to credit and the music contained in the menu it stops automatically ?
Need to see the menu world class codes.
Archimedes007 Archimedes007

2017/5/25

#
danpost wrote...
Archimedes007 wrote...
How about when we move from menu to credit and the music contained in the menu it stops automatically ?
Need to see the menu world class codes.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MenuUtama here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MenuUtama extends World
{
 
    GreenfootSound myMusic = new GreenfootSound("Musik_Game_Suparman.mp3");
    public MenuUtama()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(889, 554, 1);
        prepare();
    }
     
    public void act ()
    {
        myMusic.playLoop();;
    }
     
}
Thats the code menu world i have buddy
Super_Hippo Super_Hippo

2017/5/25

#
The playLoop method shouldn't be called every act cycle. → change 'act' in line 20 to 'started' if you don't want to put it in the constructor You need a method to stop the music that you can call before you change the world.
Archimedes007 Archimedes007

2017/5/25

#
Super_Hippo wrote...
The playLoop method shouldn't be called every act cycle. → change 'act' in line 20 to 'started' if you don't want to put it in the constructor You need a method to stop the music that you can call before you change the world.
can you give me the example code ? Please buddy
danpost danpost

2017/5/25

#
Archimedes007 wrote...
can you give me the example code ? Please buddy
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;
  
public class MenuUtama extends World
{
    GreenfootSound myMusic = new GreenfootSound("Musik_Game_Suparman.mp3");
     
    public MenuUtama()
    {   
        super(889, 554, 1);
        myMusic.playLoop();
        prepare();
    }
}
However, there is some other code still missing from the class (like the prepare method). In the credit button class act method, you can put something like this:
1
2
3
4
5
if (Greenfoot.mouseClicked(this))
{
    ((MenuUtama)getWorld()).myMusic.stop();
    Greenfoot.setWorld(new Credits());
}
Super_Hippo Super_Hippo

2017/5/25

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void started()
{
    myMusic.playLoop();
}
 
public void stopped()
{
    myMusic.pause();
}
 
public void stopMusic()
{
    myMusic.stop();
}
Then, when changing the world from an actor in the world:
1
2
((MenuUtama) getWorld()).stopMusic();
Greenfoot.setWorld(new WorldName());
You need to login to post a reply.