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

2017/10/31

Error while using object from other class

faldiega faldiega

2017/10/31

#
i've watched the greenfoot tutorial on youtube to play and stop the music. but it was just possible in one class. How do i control the music from Worldy class to StartScreen class? i need to play different music in different world. here is the code. Worldy class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Worldy extends World
{
    GreenfootSound bgsound = new GreenfootSound("Backsound.mp3");
    HealthBar healthbar = new HealthBar();
    HealthBarAI healthbarai = new HealthBarAI();

    public Worldy()
    {    
        super(900, 600, 1);
        setBackground("City.png");
        addObject(new Ground(900, 60), 450, 600);
        addObject(new Ground(200, 60), 300, 420);
        addObject(new Ground(400, 60), 700, 300);
        addObject(healthbar,700,125);
        addObject(healthbarai,200,125);
        
        menuSound.stop(); //<- stop the menusound when in game.
        bgsound.playLoop();
        
        PlayerBot player = new PlayerBot();
        addObject(player, 450, 525);
        addObject(new EnemyBot(), 200, 525);
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    public HealthBarAI getHealthBarAI()
    {
        return healthbarai;
    }
}
StartScreen class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class StartScreen extends World
{
    GreenfootSound menuSound = new GreenfootSound("Menusound.mp3");

    public StartScreen()
    {   
        super(900, 600, 1);
        prepare();
        bgsound.stop(); //<- stop the backsound music when in menu.
        menuSound.play();
    }

    private void prepare()
    {
        TitleMulai titlemulai = new TitleMulai();
        addObject(titlemulai,769,408);
        TitleBantuan titlebantuan = new TitleBantuan();
        addObject(titlebantuan,726,512);
    }
}
danpost danpost

2017/10/31

#
You could try using my BgMusic Actor class in my BgMusic Actor Class Demo scenario. As long as you use the class to start the background music, you can control it from anywhere. You do not even have to add any extra actors in the world. Just create another object from the class to regain control.
faldiega faldiega

2017/10/31

#
is that work without pressing any button? i mean when the world is change, the music will change automatically
danpost danpost

2017/10/31

#
faldiega wrote...
is that work without pressing any button? i mean when the world is change, the music will change automatically
As long as you start the new background music in the new world through the same class (can be a new object of the class), the old music will stop playing when the new music starts.
faldiega faldiega

2017/10/31

#
i play the menusound in StartScreen class. when i change to Worldy class, the backsound is playing and the menusound is playing too. those sounds are playing together. i get confuse now...
danpost danpost

2017/10/31

#
faldiega wrote...
i play the menusound in StartScreen class
using a BgMusic object?
when i change to Worldy class, the backsound is playing and the menusound is playing too. those sounds are playing together.
This will not happen if all background music is started by way of a BgMusic object. Show what you currently have for background music codes in both world classes.
faldiega faldiega

2017/10/31

#
Yes with that object i use it. Worldy class:
import greenfoot.*; 

public class Worldy extends World
{
    GreenfootSound bgsound = new GreenfootSound("Backsound.mp3");
    HealthBar healthbar = new HealthBar();
    HealthBarAI healthbarai = new HealthBarAI();

    public Worldy()
    {    
        bgsound.playLoop(); //playing the Backsound in Worldy class
   
    }
}
StartScreen class:
import greenfoot.*; 

public class StartScreen extends World
{
    GreenfootSound menuSound = new GreenfootSound("Menusound.mp3");

    public StartScreen()
    {   
        menuSound.play(); //playing the Menusound in StartScreen class.
    }
}
i thought the music will play only if i run the world. but it isn't. it's playing together
danpost danpost

2017/10/31

#
No -- you are still creating GreenfootSound objects. Use lines like this to start the music in each world:
new BgMusic("Backsound.mp3").play();
You do not need to call 'play' a second time if the current background music is playing. In other words:
new BgMusic("Menusound.mp3");
// or
new BgMusic().setBackgroundMusic("Menusound.mp3");
would work in the secondary world. You will not need either line 5 above; and you will be replacing both line 11 in first posting and 9 in the other.
faldiega faldiega

2017/11/1

#
okay i will try it. am i correct by creating the object in each class like that? or should i create a new class?
danpost danpost

2017/11/1

#
faldiega wrote...
am i correct by creating the object in each class like that?
Yes. That should work just fine.
You need to login to post a reply.