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

2016/12/6

Movement and Direction

1
2
Laurence Laurence

2016/12/6

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import greenfoot.*;
 
 
public class Kasu_Marie extends Actor
{
    private GifImage gif = new GifImage("Run.gif");
    private GifImage gif2 = new GifImage("RunBack.gif");
    private GifImage image = new GifImage("Idle.gif");
    private GifImage image2 = new GifImage("IdleBack.gif");
    private boolean keyPressed = false;
    private boolean keyPressed2 = false;
public void act()
    {
    if (Greenfoot.isKeyDown("d"))
    {
        setImage(gif.getCurrentImage());
        move(8);
        keyPressed2 = true;
        keyPressed = false;
    }
    else if (keyPressed2 == true)
    {
        move(0);
        setImage(image.getCurrentImage());
    }
    if (Greenfoot.isKeyDown("a"))
    {
        setImage(gif2.getCurrentImage());
        move(-8);
        keyPressed = true;
        keyPressed2 = false;
    }
    else if (keyPressed == true)
    {
        move(0);
        setImage(image2.getCurrentImage());
    }
    }
        public Kasu_Marie()
{
    int scalePercent = 15;
    for (GreenfootImage gif : gif.getImages())
    {
        int wide = gif.getWidth()*scalePercent/100;
        int high = gif.getHeight()*scalePercent/100;
        gif.scale(wide, high);
    }
    for (GreenfootImage gif2 : gif2.getImages())
    {
        int wide = gif2.getWidth()*scalePercent/100;
        int high = gif2.getHeight()*scalePercent/100;
        gif2.scale(wide, high);
    }
    for (GreenfootImage image : image.getImages())
    {
        int wide = image.getWidth()*scalePercent/100;
        int high = image.getHeight()*scalePercent/100;
        image.scale(wide, high);
    }
    for (GreenfootImage image2 : image2.getImages())
    {
        int wide = image2.getWidth()*scalePercent/100;
        int high = image2.getHeight()*scalePercent/100;
        image2.scale(wide, high);
    }
}
}
I am trying to make a character face a direction, play a GIF, move in the direction, and stay facing the direction when the key is released. The idle.gif images are the images placed once the key is released to make sure the character is facing the direction they were left in, although, this isn't going as planned. Any assistance would be highly appreciated and thanks in advance!
danpost danpost

2016/12/7

#
It might be easier if you remove the booleans for keys pressed and add one object reference for the current gif being used. To aid in keeping things in order, renaming the GifImage object references you already have would help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// these object reference fields
private GifImage gifR1 = new GifImage("Run.gif");
private GifImage gifL1 = new GifImage("RunBack.gif");
private GifImage gifR0 = new GifImage("Idle.gif");
private GifImage gifL0 = new GifImage("IdleBack.gif");
private GifImage gif = gifR0; // current gif
 
// with this act method
public void act()
{
    // movement
    int dx = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    move(8*dx);
    // determine gif to use and get current image
    if (dx != 0) // not idle
    {
        if (dx == 1) gif = gifR1; else gif = gifL1;
    }
    else // idle
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
Laurence Laurence

2016/12/7

#
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 This has popped up unexpectedly, I've been scanning through my sizing code (located at the bottom).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        public Kasu_Marie()
{
    int scalePercent = 20;
    for (GreenfootImage gif : gifR1.getImages())
    {
        int wide = gif.getWidth()*scalePercent/100;
        int high = gif.getHeight()*scalePercent/100;
        gif.scale(wide, high);
    }
    for (GreenfootImage gif2 : gifL1.getImages())
    {
        int wide = gif2.getWidth()*scalePercent/100;
        int high = gif2.getHeight()*scalePercent/100;
        gif2.scale(wide, high);
    }
    for (GreenfootImage image : gifR0.getImages())
    {
        int wide = image.getWidth()*scalePercent/100;
        int high = image.getHeight()*scalePercent/100;
        image.scale(wide, high);
    }
    for (GreenfootImage image2 : gifL0.getImages())
    {
        int wide = image2.getWidth()*scalePercent/100;
        int high = image2.getHeight()*scalePercent/100;
        image2.scale(wide, high);
    }
}
danpost danpost

2016/12/7

#
What is the rest of the error message? -- copy/paste the entire printout here.
Laurence Laurence

2016/12/7

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
    at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186)
    at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:189)
    at greenfoot.GreenfootImage.scale(GreenfootImage.java:390)
    at Kasu_Marie.<init>(Kasu_Marie.java:48)
    at Level1.prepare(Level1.java:133)
    at Level1.<init>(Level1.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at greenfoot.core.Simulation.newInstance(Simulation.java:607)
    at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:445)
    at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494)
    at greenfoot.core.Simulation.maybePause(Simulation.java:299)
    at greenfoot.core.Simulation.runContent(Simulation.java:212)
    at greenfoot.core.Simulation.run(Simulation.java:205)
The Level1 world is simply where the character is placed within.
danpost danpost

2016/12/8

#
The only thing that I believe would cause that error is if one of the images in one of the gif files had a width or height less than 5. I do not think that your naming of variables is causing the problem; but, to make sure, do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// replace lines 39 through 66 above with the following
public Kasu_Marie()
{
    int scalePercent = 20;
    scaleGif(gifR0, scalePercent);
    scaleGif(gifR1, scalePercent);
    scaleGif(gifL0, scalePercent);
    scaleGif(gifL1, scalePercent);
}
 
private void scaleGif(GifImage gifImage; int percentage)
{
    for (GreenfootImage image : gifImage.getImages())
    {
        int wide = image.getWidth()*percentage/100;
        if (wide == 0) System.out.println("An image width is "+image.getWidth()+" which when scaled by "+percentage+" would becomes "+wide+" wide.");
        int high = image.getHeight()*percentage/100;
        if (high == 0) System.out.println("An image height is "+image.getHeight()+" which when scaled by "+percentage+"  would becomes "+high+" high.");
        image.scale(wide, high);
    }
}
Laurence Laurence

2016/12/8

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public Kasu_Marie()
{
    int scalePercent = 20;
    scaleGif(gifR0, scalePercent);
    scaleGif(gifR1, scalePercent);
    scaleGif(gifL0, scalePercent);
    scaleGif(gifL1, scalePercent);
}
  
private void scaleGif(GifImage gifImage; int percentage)
{
    for (GreenfootImage image : gifImage.getImages())
    {
        int wide = image.getWidth()*percentage/100;
        if (wide == 0) System.out.println("An image width is "+image.getWidth()+" which when scaled by "+percentage+" would becomes "+wide+" wide.");
        int high = image.getHeight()*percentage/100;
        if (high == 0) System.out.println("An image height is "+image.getHeight()+" which when scaled by "+percentage+"  would becomes "+high+" high.");
        image.scale(wide, high);
    }
}
private void scaleGif(GifImage gifImage; int percentage) - This line keeps getting errors at the semi-colon and at the end.
danpost danpost

2016/12/8

#
Laurence wrote...
private void scaleGif(GifImage gifImage; int percentage) - This line keeps getting errors at the semi-colon and at the end.
Sorry, the semi-colon should be a comma.
Laurence Laurence

2016/12/8

#
https://gyazo.com/c1860c3e91abbb0396b915ae4844ce1b I seem to get the same error, as shown by the image.
danpost danpost

2016/12/8

#
Laurence wrote...
I seem to get the same error, as shown by the image.
Okay, change the 'System.out.println(...);' parts to 'continue;'. I cannot guarantee that the gifs will be properly displayed, however.
Laurence Laurence

2016/12/8

#
1
2
3
4
5
6
7
java.lang.NullPointerException
    at GifImage.getCurrentImage(GifImage.java:101)
    at Kasu_Marie.act(Kasu_Marie.java:26)
    at greenfoot.core.Simulation.actActor(Simulation.java:594)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
    at greenfoot.core.Simulation.runContent(Simulation.java:215)
    at greenfoot.core.Simulation.run(Simulation.java:205)
I've been trying to get rid of this error my own, but, unfortunately this hasn't ceased to stop. I do, however, know it is from this part of the code (movement).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Kasu_Marie extends Actor
{
private GifImage gifR1 = new GifImage("Run.gif");
private GifImage gifL1 = new GifImage("RunBack.gif");
private GifImage gifR0 = new GifImage("Idle.gif");
private GifImage gifL0 = new GifImage("IdleBack.gif");
private GifImage gif = gifR0;
public void act()
{
    int dx = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    move(8*dx);
 
    if (dx != 0)
    {
        if (dx == 1) gif = gifR1; else gif = gifL1;
    }
    else
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
danpost danpost

2016/12/8

#
It sounds like one of your gif files is missing an image. To test this theory, do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// at beginning of constructor
public Kasu_Marie()
{
    // add these lines here
    GifImage[] gifs = { gifR0, gifR1, gifL0, gifL1 };
    String[] names = { "gifR0", "gifR1", "gifL0", "gifL1" };
    for (int i=0; i<gifs.length; i++)
    {
        GreenfootImage[] images = gif.getImages();
        System.out.println("\n\n"+names[i]+":  image count = "+images.length);
        for (int j=0; j<images.length; j++)
        {
            if (images[j] == null) System.out.println("images[ "+j+" ] = null");
        }
    }
Then copy/paste the terminal printout here after compiling.
Corvus Corvus

2016/12/9

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void act()
{
    // movement
    int dx = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    move(8*dx);
    // determine gif to use and get current image
    if (dx != 0) // not idle
    {
        if (dx == 1) gif = gifR1; else gif = gifL1;
    }
    else // idle
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
Would this not result in a java.lang.NullPointerException error?
danpost danpost

2016/12/9

#
Corvus wrote...
< Code Omitted > Would this not result in a java.lang.NullPointerException error?
Only if the value of 'gif' was 'null'. With the code given above, all four GifImage object references are given a GifImage object; then 'gif' is assigned to one of them (see line 6 of my first code post) and is never assigned anything other than one of the four GifImage objects. So, it should never be 'null' -- unless somewhere else in the project it is being set to 'null'.
danpost wrote...
It sounds like one of your gif files is missing an image. To test this theory, do this: < Code Omitted > Then copy/paste the terminal printout here after compiling.
This cannot be the case. It is possible to use '(GreenfootImage)null' as the parameter in a setImage method call. It would not throw a nullPointerException. I have the strong suspicion, now, that it is the case that somewhere else in the code 'gif' is being set to 'null'. @Laurence, please post the entire class code. And also post the 'getCurrentImage' method in the GifImage class. It is around line 100. Indicate which line is 101 in the class.
Laurence Laurence

2016/12/10

#
I think I've figured out the problem. The 'Idle.gif' and 'IdleBack.gif' are both single-framed PNGs but I used them to make coding it as a whole easier (very lazy on my part). Would this be the issue?
There are more replies on the next page.
1
2