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

2018/5/9

Out of Memory error when adding sound to character

Bevmo831 Bevmo831

2018/5/9

#
I am attempting to add background music to an easter egg character to a game I am helping create as part of a group, but when I add the sound I am given an out of memory error. I have little other code running in the background other than the easter egg character and a text box, so I am wondering what could be causing my memory dump. My code is as such. The point is for him to be a moon walking character. public class Mj extends Actor { private int moveCount = 0; private int rnd; private int speed = 1; private int walkCount = 0; private GreenfootImage w; private GreenfootImage a; /** * Act - do whatever the Mj wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Mj() { w = new GreenfootImage; a = new GreenfootImage; w = new GreenfootImage("mj1.png"); w = new GreenfootImage("mj2.png"); w = new GreenfootImage("mj3.png"); w = new GreenfootImage("mj4.png"); w = new GreenfootImage("mj5.png"); a = new GreenfootImage("mjb1.png"); a = new GreenfootImage("mjb2.png"); a = new GreenfootImage("mjb3.png"); a = new GreenfootImage("mjb4.png"); a = new GreenfootImage("mjb5.png"); } public void act() { checkWall(); randomMove(); playSounds(); } public void playSounds() { GreenfootSound smoothCriminal = new GreenfootSound("smoothloop.wav"); smoothCriminal.playLoop(); } public void moveUp() { setLocation(getX(), getY() -speed); } public void moveDown() { setLocation(getX(), getY() +speed); } public void moveLeft() { setLocation(getX() -speed, getY()); } public void randomMove() { if (Greenfoot.getRandomNumber(100) < 3) { rnd = Greenfoot.getRandomNumber(3); } if (rnd == 1) { moveUp(); walkUp(); } else if (rnd == 2) { walkRight(); } else if (rnd == 3) { walkLeft(); } else { setLocation(getX(), getY()); } } public void checkWall() { if (isAtEdge()) { speed = -speed; } } public void walkUp() { walkCount++; if (walkCount >= w.length * 3) { walkCount = 0; //reset the count } setImage(w); } public void walkDown() { walkCount++; if (walkCount >= w.length * 3) { walkCount = 0; //reset the count } setImage(w); } public void walkRight() { walkCount++; if (walkCount >= w.length * 3) { walkCount = 0; //reset the count } setImage(w); } public void walkLeft() { walkCount++; if (walkCount >= a.length * 3) { walkCount = 0; //reset the count } setImage(a); } }
davmac davmac

2018/5/10

#
You're creating a new GreenfootSound every act cycle. Create the sound in the constructor, assign it to an instance variable, and call play() (not playLoop()) on it from the act() method. I think that should work fine.
You need to login to post a reply.