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

2016/5/19

Sound Out-of-Bounds and Mobspawning

ZacBuzzsaw ZacBuzzsaw

2016/5/19

#
So after multiple unsuccessful attempts a debugging, I finally figured out that I'm too incompetent to figure this out on my own. Question one of two-- the scenario I'm working on utilizes a shotgun fire-and-reload sound effect. But the zeptosecond it stops playing, Greenfoot throws an out-of-bounds exception, exactly like this: Exception in thread "Soundstream:file:/Z:/MyProject/sounds/mossberg.mp3" java.lang.ArrayIndexOutOfBoundsException: 580." I've tried to fix this quite a few times, but to no avail. It just refuses to go away. How do I make it go away? Question two of two-- I've also had extreme difficulty getting opponents to spawn in this scenario. I have the code that I think is correct; I haven't used Greenfoot since August of 2015, so I'm not only incompetent, but I'm also out of practice. Here's the code I'm pseudocertain about:
 private int count = 0; //initialized in the Background class as opposed to the Background method, which is below

 public Background() //This is the Background method that is added to the code by default
    {    
        count++;
        int position = (int)(Math.random()*3);
        if(count % 10000 == 0)
        {
            if(position == 0)
                this.addObject(new Grunt(), 125, 175);
            else if(position == 1)
                this.addObject(new Grunt(), 300, 175);
            else
                this.addObject(new Grunt(), 475, 175);
         }
}
What is it exactly that I'm doing wrong? And if I'm doing everything wrong, I wouldn't be surprised.
SPower SPower

2016/5/19

#
In answer to your first question, if that is all that the error message says, then I'm not surprised you couldn't solve it: that's nog something caused by your code. I've had some issues like that with sound in Greenfoot before, so I think it's just a bug. In answer to your second one, it's easier to use the Greenfoot.getRandomNumber method, which takes the limit as argument (limit is not included in the result, 0 is included):
int position = Greenfoot.getRandomNumber(3); // will return either 0, 1 or 2 (never 3)
// the rest
Though, you could also shorten the code you currently have to:
int position = Greenfoot.getRandomNumber(3);
addObject(new Grunt(), 125 +position*125, 175);
But the problem with your code currently is that it is placed in the world's constructor, which only gets executed once (when the world is created). You need to move the spawning code (that includes the count++ lines and such) to the act method in Background:
public void act()
{
    count++;
    // and all the rest of your code
}
SPower SPower

2016/5/19

#
Also, a performance thing I feel like I have to include, is that the modulo-operator (the % sign) is slow compared to other operations like addition or subtraction. In a simple scenario like this you wouldn't really notice it I would think, but it's better practice to do something simpler:
if (count >= 10000) {
    count = 0;
    // code
}
danpost danpost

2016/5/20

#
I think that is because you have information tags in your sound file. Try resaving the files without them.
You need to login to post a reply.