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

2017/9/6

Trouble setting world class' image as .gif

Mr.Strangerous Mr.Strangerous

2017/9/6

#
After having dabbled a little and trying to import the GIF class, I have still not been able to succesfully get my program to run with a .gif as the "intro screen" (meaning to add the gif to the world class) to my tiny creation. What I currently have: Created a new gifImage and linked to it, named correctly and put as text (as follows):
GifImage gifImg = new GifImage("NameOfGif.gif");
Created a method that does the following:
setBackground(gifImg.getCurrentImage());
and called this in my act method Referring to this: https://www.greenfoot.org/topics/59192/0
Super_Hippo Super_Hippo

2017/9/6

#
This doesn't look wrong yet. Maybe there is something else wrong in your world class code.
Mr.Strangerous Mr.Strangerous

2017/9/6

#
Right, so i have 2 worlds; one which functions as an "intro world" and another which is where the game will supposedly play - and I can get that to work flawlessly. The only problem I have, is that after replacing the functioning code, cosisting of really basic code like: background.setColor(Color.COLOR); and background.drawString("text",int,int); to instead be a picture (in this case a GIF) which displays the exact same information, then my problem arises. The intro world I have is built as follows: I have 2 private static final integers which determines my width and height of the game, I have a gifImage set to a name and I refer it to the correct path, I then call my integers to size the world in my IntroWorld method I then created a method called gifAnimation() and call it in my act method as well as swap over to my World on a certain key press and make my game world boolean come true, so the new playable world launches. In code:
public class IntroWorld extends World
{
    private static final int WORLD_WIDTH = 600;
    private static final int WORLD_HEIGHT = 800;
    
    GifImage gifImg = new GifImage("IntroScreen.gif");

    public IntroWorld()
    {
        super(WORLD_WIDTH, WORLD_HEIGHT, 1);
        
        /*background.setColor(Color.BLACK);
        background.drawString("Intro world. Press ENTER to start game...", WORLD_WIDTH / 2 - 100, WORLD_HEIGHT / 2);
*/
    }
    
    public void gifAnimation()
    {
        setBackground(gifImg.getCurrentImage());
    }
    
    public void act()
    {
        gifAnimation();
        
        String key = Greenfoot.getKey();
        if (key != null && key.equals("enter"))
        {
            Greenfoot.setWorld(new GameWorld(true));
        }
    }
}
*EDIT* I forgot to include; Of course at the very top I have the "import greenfoot.*" to allow all types of references.
danpost danpost

2017/9/6

#
I do not see any possible issues with the code given.
Mr.Strangerous wrote...
I have still not been able to succesfully get my program to run with a .gif as the "intro screen" (meaning to add the gif to the world class) to my tiny creation.
This is rather vague. What exactly do you get, if anything? What testing have you done to see if the GifImage object was loaded properly? Can you upload what you have so that someone can possibly find out what is wrong by working directly with it?
Mr.Strangerous Mr.Strangerous

2017/9/6

#
Well, being rather new with greenfoot I wasnt even aware that it had a debugger button, after having given it a looksie I think I have a reason why it supposedly isn't working. It feels as if the Greenfoot client simply either times out or is busy doing something. No buttons function (until some time has passed) after I run my program In the debugger it points me to this section
public GreenfootImage getCurrentImage()
    {
        long delta = System.currentTimeMillis() - time;

        while (delta >= delay[currentIndex] && !pause) {
            delta -= delay[currentIndex];
            time += delay[currentIndex];
            currentIndex = (currentIndex+1) % images.length;
        }
        return images[currentIndex];
    }
and specifically points towards currentIndex = (currentIndex+1) % images.length; In the debugger it says "Local variables Long delta" Is the reason that the .gif is too big ?? Or what am I to make of this information.
danpost danpost

2017/9/6

#
There should not be anything wrong with the code to the GifImage class. If anything is wrong, it is with the gif file you are providing it. Maybe it is corrupted or not in a valid format. If too big, you would get a terminal error message. It seems strange that you say there is a delay after running the program. What world is active at the time you stopped it? If the game world, avoid going there until the animation issue is resolved. The delay is another issue -- work on one at a time. Rather than using the debugging feature, I would use the inspect feature and System.out.printtln statements to narrow down problems
Mr.Strangerous Mr.Strangerous

2017/9/6

#
The active world would be the intro screen, and it is as if the GIF is the one stopping the program from continuing. Also the hotkey "enter" which should force the game into the other world, is not able to be executed, so clearly there must be something wrong, in connection with the used Gif. I'll try to recreate the whole thing with a new gif, to test if it's my own gif which is faulty. Update: I tested it with a GIF I loaded in, which was significantly shorter - but more importantly it WORKED. Perfectly. Or well, well enough. My question now though, is how a functional .gif could be faulty in the eyes of Greenfoot (I am BEYOND rookie stage at this point) Thank you guys for the help with trouble shooting my immediate problem!
danpost danpost

2017/9/6

#
Again, it is difficult for us here to help without having the actual gif file to work with and test. However, if you wish to proceed to slow way -- we can start with the following test (so that we here can be sure and know what we are working with). Add the following code to the IntroWorld constructor:
java.util.List<GreenfootImage> gifs = gifImg.getImages();
System.out.println("Number of images in animation: "+gifs.size());
Compile ard report back the terminal output.
You need to login to post a reply.