Is there a way to make a sound file loop without getting a null pointer error?
public Man() { rain = new GreenfootSound("Rain.wav"); } public void sound() { if (soundCount == 1) { rain.playLoop(); soundCount++; } }
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(); }
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); } } }
public void sound() { if (soundCount == 1 && rain != null) { rain.playLoop(); soundCount++; } }