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

2015/1/8

Greenfoot sound problems

Kirachan Kirachan

2015/1/8

#
Hello, I created a new project yesterday to create a virtual guitar trainer. However, it seems like Greenfoot cannot play 2 sound in a row. An error will pop out saying "Exception in thread "Thread-1182" java.lang.NullPointerException at greenfoot.sound.SoundClip.processState(SoundClip.java:276) at greenfoot.sound.ClipProcessThread.run(ClipProcessThread.java:74) at java.lang.Thread.run(Thread.java:744)" So far i have each "key" as an new subclass of guitar and they all look like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * plays the low E key on the guitar
 */
public class OpenLowE extends Open
{
    private static GreenfootSound tone = new GreenfootSound("2e.wav");
    /**
     * plays the lowE key
     */
    public void act() 
    {
        checkKey();
    }    
    public void checkKey()
    {
        if(Greenfoot.mousePressed(this))
        {
            guitar.play(tone);
        }
        }
}
Another issue that arised is when i tried to have keys to play certian sounds, and there seems to be a lag time in the playing of the sound IDK if this is a issue with greenfoot sound system or my settings, but can someone help me out? cheers
danpost danpost

2015/1/8

#
Try replacing line 20 with this:
tone.play();
Kirachan Kirachan

2015/1/8

#
yay that worked, thanks, so now i have a next question, my guitar.play(GreenfootSound x) method is as follow
public static void play(GreenfootSound x)
    {
        x.stop();
        x.play();
    }
So why does the error occur if i do this? Just interested at this point
danpost danpost

2015/1/8

#
It might need some time to reset after the 'stop' call. Try adding a 'Greenfoot.delay(2);' statement in between (of course, this delay is not what you want to use).
Kirachan Kirachan

2015/1/8

#
i see, thanks a lot man
danpost danpost

2015/1/8

#
Wow. I did not see the Open class code you had edited in before my first post. You have um-teen classes, one for each tone? Why? You only need one class! You just need um-teen different objects from the one class. The only difference between each object will be the filename for the sound it is to play. You can add change the 'static' fields to 'non-static' ones so each note can hold a different key (just remove 'static' from the field declaration). Then add a constructor to the class that receives the filename ( 'public OpenNote(String filename)' ) and within its code block create the sound object and save it in the field.
Kirachan Kirachan

2015/1/9

#
oh that is more effective, thanks for all the help man
You need to login to post a reply.