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

2013/6/9

Sound loops

Gingervitis Gingervitis

2013/6/9

#
Is there a way to make a sound file loop without getting a null pointer error?
Zamoht Zamoht

2013/6/9

#
Use the GreenfootSound class. Here is the API . You should use the playLoop() method.
Gingervitis Gingervitis

2013/6/9

#
I used that but i keep getting a null pointer error
Zamoht Zamoht

2013/6/9

#
Please show me the code that makes your game/program crash.
Gingervitis Gingervitis

2013/6/9

#
public Man()  
    {  
        rain = new GreenfootSound("Rain.wav");  
    } 
    
    public void sound()
    {
        if (soundCount == 1)
        {          
            
                rain.playLoop();
                soundCount++;
            
        }
    }
it plays fine when I just have Greenfoot.playSound(...)
Zamoht Zamoht

2013/6/9

#
Your code looks fine from what I can see. I tried using this myself and it works fine, so it has to be somewhere else in your code the problem is. This is my short code for using it.
public SoundWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        GreenfootSound sound = new GreenfootSound("sound.mp3");
        
        sound.playLoop();
    }
danpost danpost

2013/6/9

#
Please show the entire class. It could be that 'soundCount' never becomes one or 'rain' is being set to something else before this code executes.
Gingervitis Gingervitis

2013/6/9

#
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 Animal
{
    private HealthBar health; 
    private HealthBar charge;
    private GreenfootSound rain;    
    int count;
    int hpAdd = 125;
    int beamCount = 1;       
    int soundCount = 1;
    /**
     * 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() 
    {
        moving();
        ifCanSee();
        beam();
        sound();
        end();
        count++;
        hpAdd++;
        beamCount++;
    } 

    

    public Man(HealthBar healthBar, HealthBar healthbar2)
    {
        health = healthBar;
        charge = healthbar2;
    }

    public void moving()
    {   
        if (Greenfoot.getMouseInfo() != null) 
        {
            if(  getX() <= Greenfoot.getMouseInfo().getX()  )
            {
                move(-1);
            }
        }
        if (Greenfoot.getMouseInfo() != null) 
        {
            if ( getX() >= Greenfoot.getMouseInfo().getX() )
            {
                move(1);
            }   
        }
    }
    
    public Man()  
    {  
        rain = new GreenfootSound("Rain.wav");  
    } 
    
    public void sound()
    {
        if (soundCount == 1)
        {          
                Greenfoot.playSound("Rain.wav");
                //rain.playLoop();
                soundCount++;
            
        }
    }

    public void ifCanSee()
    {
        Actor rd = getOneIntersectingObject(Raindrop.class);  
        if (rd != null) getWorld().removeObject(rd);
        if (rd != null ) health.subtract(1);        
        if (rd != null)  hpAdd = 100;

        if (hpAdd >= 125)
        {
            health.add(1);
            hpAdd = 90; 
        }

        Actor lightning1 = getOneIntersectingObject(Lightning.class);
        if (lightning1 != null) health.subtract(5);
        if (lightning1 != null) getWorld().removeObject(lightning1);

        Actor lightning2 = getOneIntersectingObject(Lightning2.class);
        if (lightning2 != null) health.subtract(5);
        if (lightning2 != null) getWorld().removeObject(lightning2);
    }

    public void end()
    {
        if (health.getValue() == 0)
        {
            Greenfoot.stop();            
            Label label = new Label("Game Over");
            getWorld().addObject(label, 326, 229);
        }
    }

    public void beam()
    {
        if (Greenfoot.isKeyDown("space") && beamCount >=1)
        {
            getWorld().addObject(new Beam(), Greenfoot.getMouseInfo().getX()-4,  Greenfoot.getMouseInfo().getY()+28);
            charge.subtract(2);            
        }
        else
        { 
            charge.add(1);           
            getWorld().removeObjects(getWorld().getObjects(Beam.class));
        }

        if (charge.getValue() == 0)
        {
            beamCount = -499;
        }

        if (health.getValue() <=50 && canSee(Beam.class) )
        {
            if (hpAdd >= 95)
            {
                health.add(1);
                hpAdd = 90; 
            }
        }

        if (canSee(Beam.class) && canSee(Flower.class) && charge.getValue() <= 100)
        {
            charge.add(300);
            health.setMaximumValue(health.getMaximumValue()+5);
            eat(Flower.class);
        }
    }
}
danpost danpost

2013/6/9

#
If you create a Man object by using the constructor that takes the HealthBar objects, the field 'rain' is never set.
Zamoht Zamoht

2013/6/9

#
I see! The sound is never defined and therefore it is null. You have two contructors for the man. public Man(HealthBar healthBar, HealthBar healthbar2) { health = healthBar; charge = healthbar2; } and public Man() { rain = new GreenfootSound("Rain.wav"); } If you create a new man using the first contructor your sound is never created. Put this(); in the first contructor or move the sound code.
Zamoht Zamoht

2013/6/9

#
To secure that the program doesn't crash you can change sound() as well.
public void sound()  
    {  
        if (soundCount == 1 && rain != null)  
        {            
                rain.playLoop();  
                soundCount++;  
              
        }  
    }
This makes sure that the sound is not null.
Gingervitis Gingervitis

2013/6/9

#
I think that did it! Thank you!
You need to login to post a reply.