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

2015/6/21

how to add sound on start menu and stop it when the button clicked

vessaliuzz vessaliuzz

2015/6/21

#
import greenfoot.*; /** * Write a description of class mulai here. * * @author Anjar * @version 1.1 */ public class mulai extends World { public mulai() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1,false); Greenfoot.start(); prepare(); } private void prepare(){ //fungsi untuk menyiapkan objek untuk ditampilkan BtnMulai btnMulai = new BtnMulai(); BtnPetunjuk btnPetunjuk = new BtnPetunjuk(); BtnKeluar btnKeluar = new BtnKeluar(); addObject(btnMulai, 322, 215);//menambahkan objek btnMulai pada world addObject(btnPetunjuk, 322, 280);//menambahkan objek btnMulai pada world addObject(btnKeluar, 322,345); } } i tried many code to playing sound when the game is start up, but i found error. anyone can help me to complete the code in above to playing sound? Help me guys :)
miningmanna miningmanna

2015/6/21

#
Add this to your mulai class:
public GreenfootSound music;

public class mulai extends World
{
    public mulai()
    {
        music = new GreenfootSound("fileName");
        music.playLoop();
    }
}
one thing i noticed: try to start class names with a capital letter like (mulai -> Mulai) just to make it easier to understand for others. Its a standard for java programming (but not nescecery)
miningmanna miningmanna

2015/6/21

#
and if you want to stop from a button add this to the buttons act() method:
public void act()
{
    mulai world = (mulai) getWorld();
    world.music.stop();
}
vessaliuzz vessaliuzz

2015/6/21

#
thank you sir.. really helpful. i'll try it. thanks..
vessaliuzz vessaliuzz

2015/6/21

#
this is class of btnMulai public class BtnMulai extends Actor { /** * Act - do whatever the BtnMulai wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.mouseClicked(this)){//jika objek ini di klik maka Greenfoot.setWorld(new Board());//world akan beralih ke Board mulai world = (mulai) getWorld(); world.music.stop(); } }
vessaliuzz vessaliuzz

2015/6/21

#
after i tried what u said before, i found error, so i try to make it on class button with name BtnMulai, no error found but music still playing
vessaliuzz vessaliuzz

2015/6/21

#
i got it sir, sorry, i can.. thank you sirrrrr yeay :)
miningmanna miningmanna

2015/6/21

#
no problem :)
You need to login to post a reply.