#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Using a method from a different class


    Hello, i'm a beginner Java programmer (only done 2 semesters in java programming in high school) and new to this forum, and I'm trying to make a basic game, half because I'm doing it for personal pleasure and half because the college which im applying at need me to send in a portfolio of some work I've done.

    So i'm attempting to use a method from a different class, however I can't get it to work.
    I'm using slick2D game development and lwjgl libraries.
    I have class (Sound.java) which handles the music playlists, the class has an array with 3 songs, in string format, and uses a switch statement to randomly select a song. there is a MainMenu.java class, which will pick the randomly selected song and use it, now I think the problem is that the song is in a string type and for it to be used properly it has to be in a Music type from the slick2D library. Any help with this would be much appreciated, also here is the code:

    MainMenu:

    package ippo.game.main;
    import org.newdawn.slick.Music;

    public class MainMenu extends BasicGameState
    {
    public MainMenu(int MainMenu)
    {
    }
    Music song;
    Sound playlist = new Sound();
    String playSong = playlist;
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
    {
    gc.setShowFPS(false);
    menu = new Image("Resources/Textures/menu.jpg");
    song = new Music(playSong);
    song.loop(1f, 1f);

    }
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
    {
    }
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
    }
    public int getID()
    {
    return 0;
    }

    }

    Sound class:
    package ippo.game.main;

    public class Sound
    {
    private String song = "Resources/Sound/Song1.wav";
    public String setSong()
    {
    String[] playlist = {"Resources/Sound/Song1.wav", "/Resources/Sound/Song2.wav"};
    int player = 0 + (int) Math.random() * 2;
    switch (player)
    {
    case 0:
    song = playlist[0];

    case 1:
    song = playlist[1];
    case 2:
    song = playlist[2];
    default:
    song = playlist[0];

    }
    return song;
    }

    public void getSong()
    {
    song = setSong();
    }
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    Please use code tags when you post you code, this will keep the code format for better reading.

    First, you convert a Sound object to String, but Sound doesn't know how to do this conversion. Do you know the content of playSong?
    You will have to implement the toString() method.

    Second, you don't call any of you Sound function.

IMN logo majestic logo threadwatch logo seochat tools logo