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

2012/2/10

Sound

Tomc84 Tomc84

2012/2/10

#
Is there a way to add a sound based on an object class that you land on? and that it will only play every 5 seconds ? so far I got it to play when I'm on the object but it plays so fast. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Man here. * * @author (your name) * @version (a version number or a date) */ public class Man extends Actor { private GreenfootImage red; private GreenfootImage black; public Man() { red = new GreenfootImage("man2.png"); black = new GreenfootImage ("man1.png"); setImage(black); } /** * Act - do whatever the Man wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. checkKeyPress(); // call this method to work changeImage(); } public void checkKeyPress() { if (Greenfoot.isKeyDown("left")) { setLocation(getX()-1,getY()); // get loaction of x and y, then move -1 on x move Left } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+1,getY()); // move Right } if (Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+1); // move down } if (Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-1); // move up } } public boolean onFire() { Actor Fire = getOneObjectAtOffset( 0 , 0, Fire.class); return Fire != null; } public boolean onWall() // test to see if the man class is on the wall class { Actor Wall = getOneObjectAtOffset( 0 , 0, Wall.class); return Wall != null; } public boolean onDoor() { Actor Door = getOneObjectAtOffset( 0 , 0, Door.class); return Door != null; } public void changeImage() // use method to change image of man { if( onWall() ) { setImage( black ); } if( onFire() ) { setImage( red ); Greenfoot.playSound("au.wav"); } if( onDoor() ) { Greenfoot.playSound("hooray.wav"); Greenfoot.stop(); } } }
danpost danpost

2012/2/10

#
Check out the documentation: there is a method called 'isPlaying()'. Use it to see if the particular sound is playing, and if it is NOT, then play the sound.
Morran Morran

2012/2/10

#
Tomc84, you've already got it half right. It plays the sound when the player's on the fire. All you need to do now is limit how often it plays. To do that, just add one more variable, an "int" called "timeTillCanPlaySoundEffectAgain" (Or come up with a shorter name if you're a little more creative). And in your "changeImage()" method, do this:
 public void changeImage() // use method to change image of man
    { 
        if( onWall() )  
        {
            setImage( black ); 
        }
        if( onFire() ) 
        {
            setImage( red );
            if(timeTillCanPlaySoundEffectAgain < 0) {
               Greenfoot.playSound("au.wav");
               timeTillCanPlaySoundEffectAgain = 40 //or whatever 5 seconds is in act() cycles.
            }
            else
               timeTillCanPlaySoundEffectAgain--; //wait until you can play the sound effect again.
        } //the rest of the code stays the exact same.
        if( onDoor() ) 
        {
            Greenfoot.playSound("hooray.wav");
            Greenfoot.stop();
        }
    }
danpost danpost

2012/2/10

#
@Morran, I think what Tomc84 was having a problem with was that the sound was being started on every act cycle. After looking at the code, you will find that would be what happens.
Morran Morran

2012/2/10

#
@Danpost That's true. My method plays the sound repeatedly, with short intervals in between. Your method loops the sound, repeating it immediately after it finishes playing. Either one is good, it just depends on what he's looking for.
danpost danpost

2012/2/10

#
@Morran, very true!
Tomc84 Tomc84

2012/2/11

#
That looks like I'd be looking for it, haha. I put it in and when I compile it tells me ... cannot find symbol - variable timeTillCanPlaySoundEffectAgain
Builderboy2005 Builderboy2005

2012/2/11

#
Did you create that variable?
Tomc84 Tomc84

2012/2/11

#
No
danpost danpost

2012/2/11

#
Then you need to set it up with
private int timeTillCanPlaySoundEffectAgain = 0;
outside the method.
Tomc84 Tomc84

2012/2/12

#
oh ok. Got it to work. Thank you all so much!!
You need to login to post a reply.