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

2020/9/5

How to start music if the scenario runs?

1
2
TerminatorHD TerminatorHD

2020/9/5

#
Hello! I would like to know how to put background music in that starts if you press the "run" button. I'm also trying to figure out how to make it stop if they world gets resettet with "Greenfoot.setWorld(new MeineWelt());"
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MeineWelt extends World
{
    //fields
    public int Timer = 2000; 
    public boolean Hintergrundmusik = true;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MeineWelt()
    {    
       super(1280,720,1);
       this.addObject(new Rakete(),100,100);
       this.addObject(new Benzinkanister(),240,540);
       this.addObject(new Benzinkanister(),595,376);
       this.addObject(new Benzinkanister(),946,625);
       this.addObject(new Benzinkanister(),878,89);
       this.addObject(new Benzinkanister(),1172,254);
       this.addObject(new Asteriods(),650,180);
       this.addObject(new Asteriods(),264,272);
       this.addObject(new Asteriods(),665,512);
       this.addObject(new BjörnGegner(), 1111,201);
       Musik();
    }
    //
    public void Musik(){
        String Musik = "Spacemusic.mp3"+this.Hintergrundmusik;
        Greenfoot.playSound("Spacemusic.mp3");
        if(getObjects(Rakete.class).isEmpty()){
            Greenfoot.stop();
        }
    }
    //
     public void act(){ //Timer
     String Zeitanzeige="Zeit:"+this.Timer;
     showText(Zeitanzeige, 60, 25); 
     //
        if(Timer > 0){
     Timer--;
        if(Timer==0){
         this.addObject(new BjörnBoss(), 1278, 348);
     } 
     }
     //
    }
}    




danpost danpost

2020/9/5

#
I am not sure I understand the logic here. You want the music to stop, even though a newly set world of the same type is running? I mean, I would understand if you stop the scenario when you set a new world. Is that what you are doing (both at the same time)?
TerminatorHD TerminatorHD

2020/9/5

#
I've the command on my Actor classes "Rakete" and "Asteriods". If the "Rakete" or "Asteriods" are touching each other, the command "Greenfoot.setWorld(new MeineWelt());" is being activated. That's how I'm resetting my world. Basicly I want the music to restart each time the World is being reset (The music stops and plays from the start). Sorry If this is confusing, I'm very new and confused.
TerminatorHD TerminatorHD

2020/9/5

#
I wanted my game to instantly restart If my classes collide with each other.
danpost danpost

2020/9/5

#
TerminatorHD wrote...
I wanted my game to instantly restart If my classes collide with each other.
Okay, you are going to have to stop and restart the music when you reset the world. Before you can do that, however, you will need to have access to the sound object. Change line 13 above to:
static GreenfootSound hintergrundMusik = new GreenfootSound("Spacemusic.mp3");
and remove the following lines: 31; 33 thru 40. Note that lines 37 thru 39 were misplaced and should be in your act method. Now you can control the music from your actor classes with:
Greenfoot.setWorld(new MeineWelt());
MeineWelt.hintergrundMusik.stop();
MeineWelt.hintergrundMusik.playLoop();
You might try moving line 2, here (the stop line), to where line 31 was above in MeineWelt. You may also want to add the following to your MeineWelt class:
public void started()
{
    hintergrundMusik.playLoop();
}

public void stopped()
{
    hintergrundMusik.pause();
}
This will pause and continue the music if the scenario is paused and restarted via Run/Pause button.
TerminatorHD TerminatorHD

2020/9/5

#
Thank you a lot! It went through hours of discussions and never got it right. I would like to ask you one more question. How do I access different int values from one classes to another? Do I have to use "static" ?
Kostya20052011 Kostya20052011

2020/9/5

#
Not necessarily, you can get an object and use its variables. But static is the most (as for me) simple method. If, of course, it does not interfere with the game.
danpost danpost

2020/9/5

#
TerminatorHD wrote...
Thank you a lot! It went through hours of discussions and never got it right. I would like to ask you one more question. How do I access different int values from one classes to another? Do I have to use "static" ?
No, that is not the purpose of static. It is allowed for the background music because ANY and ALL worlds can share (at different times, of course) that same instance of GreenfootSound. The int values of the dimensions of your worlds may also be made static as ALL worlds of that type would be of the same size (at least, in most cases). Most variables have values that are specific to the instance, That is two instances of the same type may have different values for what the variable represents (this could be pretty much anything -- size, speed, location, rotation, color index ... whatever). These must not be static variables. They each represent the state of a specific instance of a class. You must get a reference to the specific object you want the state of. In order to help further, you will need to be more specific and provide codes for review.
Kostya20052011 Kostya20052011

2020/9/5

#
Well, if the music is in the world, you can just use the getWorld() method, for example: getWorld().Music.play Loop();
Kostya20052011 Kostya20052011

2020/9/5

#
He wants to make music without static, but it still works, as I understand it.
Kostya20052011 Kostya20052011

2020/9/5

#
You can still just declare music in all worlds and make it play, but then there may be overlays of melodies, music from the previous world will need to be turned off if you change the world.
danpost danpost

2020/9/5

#
Kostya20052011 wrote...
He wants to make music without static, but it still works, as I understand it.
His general question about static:
Do I have to use "static" ?
was in reference to int values:
How do I access different int values from one classes to another? Do I have to use "static" ?
It had nothing to do with the music, specifically.
TerminatorHD TerminatorHD

2020/9/5

#
Thanks for all your responses. I've one more issue. I would like to add more music for my scenario. But..it crashes. It is probably because of the two static lines and I don't really know what they do and I still don't know with which command I can interact with different classes import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Musik here. * * @author (your name) * @version (a version number or a date) */ public class Musik extends Actor { //fields public static GreenfootSound hintergrundMusik = new GreenfootSound("Spacemusic.mp3");//Hintergrundmusik public static GreenfootSound Bjoernmusik = new GreenfootSound("BossMusic.mp3");//Bossmusik /** * Act - do whatever the Musik wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { } }
TerminatorHD TerminatorHD

2020/9/5

#
Ok for some reason I can't properly copy my code into here sorry..
danpost danpost

2020/9/5

#
"It crashes" is vague. Do you get any error message? if so, what is given? if not, can you provide any information on how or when?
There are more replies on the next page.
1
2