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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public Man() { rain = new GreenfootSound( "Rain.wav" ); } public void sound() { if (soundCount == 1 ) { rain.playLoop(); soundCount++; } } |
1 2 3 4 5 6 7 8 9 | 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(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 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 ); } } } |
1 2 3 4 5 6 7 8 9 | public void sound() { if (soundCount == 1 && rain != null ) { rain.playLoop(); soundCount++; } } |