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

2014/12/15

Stuck!

1
2
3
danpost danpost

2014/12/15

#
Again, where is the 'buttons' array defined in the class? and where are you even creating any buttons? one MUST use the 'new' keyword to create an object (like you did for the StopButton object on line 21). After creating the music buttons, they need to be assigned to the array (which needs declared and initialized) and added to the world (which you were attempting on lines 15 through 19). However, I am not quite sure if you actually need that array. What exactly is the 'stop' method in this class supposed to be doing? I mean, I see it is being called from the 'stop' method of the PlayMusicButton class (which may not be necessary); but, is it being called from the StopButton class also?
gyoshon gyoshon

2014/12/15

#
yes i have a class call, StopButton. and I've tried to make the buttons here's what i had before but gotten an error
gyoshon gyoshon

2014/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class PlayerWorld extends World
{
    private PlayMusicButton[] buttons = ( new PlayMusicButton("Hair.mp3", new Color(170, 200, 0)));
   
    public PlayerWorld()
    {    
        super(600, 400, 1); 
        
        int x = 110;
        int y = 70;
        int s = 50;
        addObject(this.buttons[0], x, y);
        addObject(this.buttons[1], x, y + s);
        addObject(this.buttons[2], x, y + 2 * s);
        addObject(this.buttons[3], x, y + 3 * s);
        addObject(this.buttons[4], x, y + 4 * s);
        
        addObject(new StopButton(), 315, 78);
    }
    
    public void stop()
    {
        for (PlayMusicButton button : this.buttons) {
            if (button.stop()) {
                break;
            }
        }
    }
    
}
gyoshon gyoshon

2014/12/15

#
it tells me "incompatible types" should it be private PlayMusicButton buttons = new PlayMusicButton ;
danpost danpost

2014/12/15

#
The way you declared the array is fine; the assignment is what is causing problems. You are using round brackets, where you should be using curly brackets. The list of new buttons should be separated by commas and enclosed in curly brackets (with a semi-colon at the end).
gyoshon gyoshon

2014/12/15

#
i'm sorry but i dont understand :( you mean private PlayMusicButton buttons = ( new PlayMusicButton{"Hair.mp3", new Color(170, 200, 0))};
danpost danpost

2014/12/15

#
No. I mean '= { new PlayMusicButton(...), new PlayMusicButton(...), ... };'.
gyoshon gyoshon

2014/12/15

#
fixed it now i dont know why it's bringing me to the "Terminal Window" the first on is in my World 1) java.lang.NullPointerException , at PlayerWorld.<init>(PlayerWorld.java:15) so line 15 is addObject(this.buttons, x, y); 2) Caused by: java.io.FileNotFoundException: Could not find file: Hair.mp3.mp3 (but i have the mp3 "hair" in the sound file)
gyoshon gyoshon

2014/12/15

#
3) java.lang.ArrayIndexOutOfBoundsException: 1 ( in my world) at PlayerWorld.<init>(PlayerWorld.java:16) addObject(this.buttons, x, y + s);
danpost danpost

2014/12/15

#
You are supplying the suffix of the filename BOTH when calling the constructor AND within the constructor. Remove ".mp3" from the names in the array in your PlayerWorld class.
danpost danpost

2014/12/15

#
Post the updated code of your PlayerWorld class.
gyoshon gyoshon

2014/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class PlayerWorld extends World
{

   private PlayMusicButton[] buttons = { new PlayMusicButton("Drive Me Home", new Color(170, 200, 0))};
    public PlayerWorld()
    {    
        super(600, 400, 1); 
        
        int x = 110;
        int y = 70;
        int s = 50;
        addObject(this.buttons[0], x, y);
        addObject(this.buttons[1], x, y + s);
        addObject(this.buttons[2], x, y + 2 * s);
        addObject(this.buttons[3], x, y + 3 * s);
        addObject(this.buttons[4], x, y + 4 * s);
        
        addObject(new StopButton(), 315, 78);
    }
    
    public void stop()
    {
        for (PlayMusicButton button : this.buttons) {
            if (button.stop()) {
                break;
            }
        }
    }
    
}
danpost danpost

2014/12/15

#
You have only created one music button, which makes your array of length one. Only the first 'addObject' line will execute before throwing an IndexOutOfBounds exception on the next.
gyoshon gyoshon

2014/12/15

#
i have to put all 5 songs in the array?
danpost danpost

2014/12/15

#
Until you add more new buttons to your array at line 7, lines 16 through 19 will throw exceptions. For testing purposes, you can comment them out for now.
There are more replies on the next page.
1
2
3