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

2016/11/29

Game works while in Greenfoot but not uploaded or as .jar

1
2
MJBelmer MJBelmer

2016/11/29

#
I just finished making my first game fully functional (needs obvious gameplay tweaks) but when I uploaded it as well as converted it to a .jar and ran it, it doesn't load the world as it's supposed to and plays the wrong music, making it unplayable. Is this a common thing for programs to not work once made into an executable?
danpost danpost

2016/11/29

#
MJBelmer wrote...
it doesn't load the world as it's supposed to
How is it different from the way it should? Any and all details can help in determining the causes of issues.
plays the wrong music, making it unplayable
Again, what music is it playing? where is that music supposed to be playing? what music should be playing and from where? "unplayable"? -- what are you capable of doing? are you seeing something you shouldn't? if so, what?
Is this a common thing for programs to not work once made into an executable?
It can happen; but usually do to improper coding or placement of files (even though it may compile).
MJBelmer MJBelmer

2016/11/29

#
So in Greenfoot the game loads, asks the player how many points for a match and then runs the Intro screen and plays the Intro music. You click the Play button and the world is switched to the main game where 1 - 5 random walls are generated and a red and blue dragon in either corner. After one player reaches the set point value the game shows the win/replay world and clicking the Yes button will set the world to a New game world. When I upload it or compile to a .jar the game doesn't ask how many rounds just shows the intro screen but plays the BGM instead of the intro music. When you click Play it switches to the main game world but there are no objects loaded into it therefore unplayable. I think my problem lies in the DragonArena world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * DragonArena is a two player game of fire-breathing dragons.
 * 
 * @author (Mitch Belmer) 
 * @version (v1.0)
 */
public class DragonArena extends World
{
    private int redScore;
    private int blueScore;
    private Dragon redDragon, blueDragon;
    private int totalPoints;
    private GreenfootSound music = new GreenfootSound("intro.mp3");
    private GreenfootSound bgm = new GreenfootSound("bgm.mp3");
    /**
     * Constructor for objects of class DragonArena.
     * 
     */
    public DragonArena()
    {    
        // Create a new world with 768x768 cells with a cell size of 1x1 pixels.
        super(768, 768, 1); 
        prepare();
        redScore = 0;
        blueScore = 0;
        showScore();
    }//end DragonArena

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Greenfoot.setWorld(new Intro(this));
        getInput();
        for (int i = 0; i < Greenfoot.getRandomNumber(4) + 1; i++)
        {
            addObject(new Wall(), Greenfoot.getRandomNumber(550) + 100, Greenfoot.getRandomNumber(550) + 100);
        }//end for
        redDragon = new Dragon("RedDragon.png", "w", "s", "a", "d", "space");
        redDragon.turn(-90);
        addObject(redDragon,634,627);
        blueDragon = new Dragon("BlueDragon.png", "up", "down", "left", "right", "0");
        blueDragon.turn(90);
        addObject(blueDragon, 107, 184);
        playMusic();
    }//end prepare
    
    /**
     * Show Red and Blue Player score
     */
    public void showScore()
    {
        showText("Red Player: " + redScore, 80, 25);
        showText("Blue Player: " + blueScore, 675,25);
    }//end showScore
    
    /**
     * Accessor methods for each of the player objects
     */
    public Dragon getRedDragon() { return redDragon; }
    public Dragon getBlueDragon() { return blueDragon; }
    public GreenfootSound getMusic() { return music; }
    
    /**
     * roundOver method allows for the world to start a new round
     */
    public void roundOver(Dragon dragon)
    {
        Greenfoot.playSound("roar.wav");
        if (dragon == redDragon) {
            //redDragon was hit so increase blue dragon score
            blueScore++;
            Greenfoot.setWorld(new PointMessage(this, blueDragon));
            if (blueScore == totalPoints)
            {
                showText("Blue Wins", 380, 380);
                stopBGM();
                fanfare();
                Greenfoot.setWorld(new ReplayMessage("blueWins.png"));
            }//end if
        }//end if
        else {
            redScore++;
            Greenfoot.setWorld(new PointMessage(this, redDragon));
            if (redScore == totalPoints)
            {
                showText("Red Wins", 380, 380);
                stopBGM();
                Greenfoot.delay(80);
                fanfare();
                Greenfoot.setWorld(new ReplayMessage("redWins.png"));
            }//end if
        }//end else
        showScore();
        locReset();
        remFire();
    }//end roundOver
    
    /**
     * Reset Dragons to start
     */
    public void locReset()
    {
        redDragon.setLocation(634,627);
        blueDragon.setLocation(107,184);
    }//end locReset
    
    /**
     * Search for Fireballs and remove them
     */
    public void remFire()
    {
        removeObjects(getObjects(Fireball.class));
        redDragon.fireReset();
        blueDragon.fireReset();
    }//end remFire
    
    /**
     * Get stating values from the players
     */
    private void getInput()
    {
        String input = Greenfoot.ask("How many points for match?");
        totalPoints = Integer.parseInt(input);
    }//end getInput
    
    /**
     * Play music
     */
    public void playMusic()
    {
        music.playLoop();
    }//end playMusic
    
    /**
     * Stop music
     */
    public void stopMusic()
    {
        music.stop();
    }//end stopMusic
    
    /**
     * Start bgm
     */
    public void playBGM()
    {
        bgm.playLoop();
    }//end playBGM
    
    /**
     * Stop bgm
     */
    public void stopBGM()
    {
        bgm.stop();
    }//end stopBGM
    
    /**
     * Play Fanfare
     */
    public void fanfare()
    {
        Greenfoot.playSound("fanfare.wav");
    }//end fanfare
}//end MyWorld
danpost danpost

2016/11/29

#
Please post the code of your Intro world class. I think we can resolve the issue by rearranging things a little bit.
MJBelmer MJBelmer

2016/11/29

#
Sure!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Display Game Intro and button to start game
 * 
 * @author (Mitch Belmer) 
 * @version (v1.0)
 */
public class Intro extends World
{
    private DragonArena dragonArena;
    private GreenfootSound music = new GreenfootSound("intro.mp3");
    private GreenfootSound bgm = new GreenfootSound("bgm.mp3");
    /**
     * Constructor for objects of class Intro.
     * 
     */
    public Intro(DragonArena da)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        dragonArena = da;
        addObject(new Play("button.png", "buttonClick.png", this), 416, 436);
    }//end Intro
    
    /**
     * Play music
     */
    public void playMusic()
    {
        dragonArena.playMusic();
    }//end playMusic
    
    /**
     * Stop music
     */
    public void stopMusic()
    {
        dragonArena.stopMusic();
    }//end stopMusic
    
    /**
     * Play bgm
     */
    public void playBGM()
    {
        dragonArena.playBGM();
    }//end playBGM
    
    /**
     * Stop bgm
     */
    public void stopBGM()
    {
        dragonArena.stopBGM();
    }//end stopBGM
    
    /**
     * Change active world to DragonArena to begin game
     */
    public void beginGame()
    {
        Greenfoot.setWorld(dragonArena);
    }//end beginGame
}//end Intro World
MJBelmer MJBelmer

2016/11/30

#
Hope this is enough to sort this out :)
danpost danpost

2016/11/30

#
I think it would be more logical to start with the Intro world (without a DragonArena world already created). To make the Intro world your initial world, do the following: * remove line 11 and 22 of the Intro world code * change line 18 to:
public Intro()
* remove all lines between 26 and 60 and also line 13 * add the following methods to control the intro music:
public void act()
{
    if (!music.isPlaying()) music.playLoop();
}

public void stopped()
{
    music.stop();
}
* change your beginGame method to this:
public void beginGame()
{
    music.stop();
    Greenfoot.setWorld(new DragonArena());
}
In your DragonArena class code above, remove line 38 and change line 50 to:
playBGM();
Finally, right click on the Intro class and select from the drop-down menu 'new Intro()' to establish it as your initial world. I think that should work for you.
MJBelmer MJBelmer

2016/11/30

#
Thank you so much! I'll be attempting this tonight after work :)
MJBelmer MJBelmer

2016/12/1

#
It worked flawlessly after a few minor changes to some references that were removed there but now working great after converting to a .jar but one slight issue... Even though the intro calls the constructor of DragonArena now, when in a .jar after the Intro screen the new world's resolution is not changed. How do I fix this?
danpost danpost

2016/12/1

#
MJBelmer wrote...
Even though the intro calls the constructor of DragonArena now, when in a .jar after the Intro screen the new world's resolution is not changed. How do I fix this?
It would be easy to fix had one of your worlds fit inside the other; but, because it is impossible to do that with worlds of (768, 768) and (800, 600), you may need to create another world of (800, 768). Unfortunately, I do not have time to test the intricacies of this right now as it would take a bit of time and I am already late to bed. Maybe just adding a second constructor to your Intro world might be enough:
public Intro(boolean sizer)
{
    super(800, 768, 1);
}
Then, right click on the Intro class and select 'new Intro(boolean)', key in 'true' or 'false' and press enter. I think once the frame is that size, you can create the jar file at that point and it may work as wanted (like I said -- not tested at all).
danpost danpost

2016/12/1

#
I finally got around to testing it. Apparently, your initial world must have the largest value of both the width and height of any world in your project or you will end up with a scrollbar or two when any world with a larger dimension is activated. So, if you can make your initial world, the Intro world, of size (800, 768), instead of (800, 600), then you will not have world size issues with your jar file (other than the improper framing around the edges of worlds of different sizes).
MJBelmer MJBelmer

2016/12/1

#
Excellent, I'll try adding this fix after class today. I worry about the Intro Screen being tiled with that world size but we'll see!
danpost danpost

2016/12/1

#
MJBelmer wrote...
Excellent, I'll try adding this fix after class today. I worry about the Intro Screen being tiled with that world size but we'll see!
You could always adjust the world size as long as both dimensions are equal to or larger than any other world (I am thinking maybe (816, 640) might work). or, you could scale the image used for tiling so that it "fits" (to (50, 50) is my guess). What are the dimensions of the image used for tiling?
MJBelmer MJBelmer

2016/12/1

#
Oh I'm not tiling images intentionally, just Greenfoot naturally will tile a world's image if the resolution is larger than the image. I'm really surprised there isn't a java function for resizing the world window to fit the new world.
danpost danpost

2016/12/1

#
MJBelmer wrote...
Oh I'm not tiling images intentionally, just Greenfoot naturally will tile a world's image if the resolution is larger than the image. I'm really surprised there isn't a java function for resizing the world window to fit the new world.
Then, your options are to scale the image to the world size, or center draw the image with borders filling in the extra space. Neither is hard to do.
String filename = "background.jpg"; // change as needed

// scaling
GreenfootImage bg = new GreenfootImage(filename);
bg.scale(getWidth(), getHeight());
setBackground(bg);

// center drawing
GreenfootImage bg = new GreenfootImage(getWidth(), getHeight());
bg.setColor(new java.awt.Color(0, 0, 0)); // change color values as needed (for border color)
bg.fill();
GreenfootImage img = new GreenfootImage(filename);
bg.drawImage(img, (getWidth()-img.getWidth())/2, (getHeight()-img.getHeight())/2);
setBackground(bg);
There are more replies on the next page.
1
2