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

2016/12/6

Movement and Direction

1
2
danpost danpost

2016/12/11

#
Laurence wrote...
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?
That very well could be since gif files have extra information included (such as duration time for each image to be displayed).
Laurence Laurence

2016/12/11

#
So how would I change the code to work if the Idle gifs were instead pngs?
danpost danpost

2016/12/11

#
Laurence wrote...
So how would I change the code to work if the Idle gifs were instead pngs?
I think this might work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// change fields
GreenfootImage gifR0 = new GreenfootImage("Idle.png"); // adjust filename as needed
GreenfootImage gifL0= new GreenfootImage("IdleBack.png"); // adjust filename as needed
 
// in act
if (dx != 0) // not idle
{
    if (dx == 1) gif = gifR1; else gif = gifL1;
    setImage(gif.getCurrentImage());
}
else // idle
{
    if (gif == gifR1) setImage(gifR0);
    if (gif == gifL1) setImage(gifL0);
}
You might want to change the name of the idle image fields as 'gifR0' and 'gifL0' are not appropriate names anymore.
Laurence Laurence

2016/12/11

#
Alright, everything is fixed as I simply made the Idle.gif actual gifs by making it a two-frame still image. This piece of the code won't actually do anything, it does not give errors but just rather does not do anything.
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/11

#
Laurence wrote...
This piece of the code won't actually do anything, it does not give errors but just rather does not do anything. < Code Omitted >
Are you saying that when commented out, the images are not 5 times larger than when not? This should not change anything, however; but, I would first add a method to scale the images of a gif:
1
2
3
4
5
6
7
8
9
private void scaleGif(GifImage gif, int scalePct)
{
    for (GreenfootImage image : gif.getImages())
    {
        int wide = image.getWidth()*scalePct/100;
        int high = image.getHeight()*scalePct/100;
        image.scale(wide, high);
    }
}
Then you constructor can look like this:
1
2
3
4
5
6
7
8
public Kasu_Marie()
{
    int scalePercent = 20;
    scaleGif(gifR1, scalePercent);
    scaleGif(gifR0, scalePercent);
    scaleGif(gifL1, scalePercent);
    scaleGif(gifL0, scalePercent);
}
Laurence Laurence

2016/12/11

#
The size code is kinda weird, it sometimes adjusts to the "int scalePercent = ;" part while other times it doesn't. Is there a reason behind this or is it just Greenfoot being what it is?
danpost danpost

2016/12/11

#
Laurence wrote...
The size code is kinda weird, it sometimes adjusts to the "int scalePercent = ;" part while other times it doesn't. Is there a reason behind this or is it just Greenfoot being what it is?
Is it consistent in what it does? Are the same images being scaled and not scaled each time? Which are being scaled?
Laurence Laurence

2016/12/11

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        public Kasu_Marie()
{
    int scalePercent = 40;   
    scaleGif(gifR1, scalePercent);
    scaleGif(gifR0, scalePercent);
    scaleGif(gifL1, scalePercent);
    scaleGif(gifL0, scalePercent);
}
private void scaleGif(GifImage gif, int scalePct)
{
    for (GreenfootImage image : gif.getImages())
    {
        int wide = image.getWidth()*scalePct/100;
        int high = image.getHeight()*scalePct/100;
        image.scale(wide, high);
    }
}
I adjusted it to scalePercent 10 and then 40 (as shown) but it is staying at 10
danpost danpost

2016/12/11

#
Laurence wrote...
< Code Omitted > I adjusted it to scalePercent 10 and then 40 (as shown) but it is staying at 10
I do not see how that is possible. How come you get all the really strange issues? The image file data is not being changed and the GifImage class creates the GreenfootImages to the same size every time (I am quite sure). Therefore, it must be something in your code that is causing it; but, it does not appear to be in the code given. Since the Kasu_Marie class extends the Actor class, it would have to be in the Kasu_Marie class (if it is there at all).
Laurence Laurence

2016/12/11

#
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
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;
        setImage(gif.getCurrentImage());
    }
    else
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
        public Kasu_Marie()
{
    int scalePercent = 40;   
    scaleGif(gifR1, scalePercent);
    scaleGif(gifR0, scalePercent);
    scaleGif(gifL1, scalePercent);
    scaleGif(gifL0, scalePercent);
}
private void scaleGif(GifImage gif, int scalePct)
{
    for (GreenfootImage image : gif.getImages())
    {
        int wide = image.getWidth()*scalePercent/100;
        int high = image.getHeight()*scalePercent/100;
        image.scale(wide, high);
    }
}
}
Here is my code as of now.
Laurence Laurence

2016/12/11

#
Nevermind, I realized that the variable I wanted to change was 'scalePct' as it was 'scalePercent'
Corvus Corvus

2016/12/12

#
Say, would there be a way in which movement code (like the one presented by you two) could be toggled on and off by a boolean variable from another object? Such as one actor makes a boolean 'true' which enables some other movement code on another actor?
danpost danpost

2016/12/12

#
Corvus wrote...
Say, would there be a way in which movement code (like the one presented by you two) could be toggled on and off by a boolean variable from another object? Such as one actor makes a boolean 'true' which enables some other movement code on another actor?
Yes. If you need help with this, please start a new discussion thread for it. Give details as to what you want to accomplish and show some attempted code.
You need to login to post a reply.
1
2