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

2015/1/11

Changing an Actor's Image

Nokomanida Nokomanida

2015/1/11

#
Hey guys, Im trying to switch between a series of images to show someone walking. I have the pictures, and they are in Libraries > Pictures, and are named Front, Back, Left, and Right, and are numbered from 1-9 as in Front1, Front2, etc.. The code so far that I have is:
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
public class Person extends Actor
{
    public void act()
    {
        move();
    }   
    public void move()
    {
        int y = getY();
        int x = getX();
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(x, y-3);
            
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(x, y+3);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(x-3, y);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(x+3, y);
        }
    }
}
I have tried some things that have already been discussed on this website about image switching, but an error message always comes up. Any help or suggestions are very appreciated. Thanks!
danpost danpost

2015/1/12

#
First thing is you want to move (or copy) the image files into the 'images' folder within the scenario folder ('greenfoot/scenarios/projectname/images/', where 'projectname' is the name of your current project).
Nokomanida Nokomanida

2015/1/12

#
Alright I did that, and then I tried a very basic
1
2
3
4
if (getImage() == new GreenfootImage("Front1.png"))
            {
                setImage(new GreenfootImage("Back1.png"));
            }
for when the "up" key is being pressed.
Nokomanida Nokomanida

2015/1/12

#
Not sure how I forgot to mention this in my last comment but it doesn't work at all xD
Greg.Hutchinson Greg.Hutchinson

2015/1/12

#
This sample snippet should do something similar to what you would like I think:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Person extends Actor
{
    GreenfootImage up = new GreenfootImage("Back1.png");
    GreenfootImage down = new GreenfootImage("Front1.png");
    GreenfootImage left = new GreenfootImage("Left.png");
    GreenfootImage right = new GreenfootImage("Right.png");
    public void act()
    {
        if (Greenfoot.isKeyDown ("left"))
            setImage(left);
        if (Greenfoot.isKeyDown ("right"))
            setImage(right);       
    }  
}
danpost danpost

2015/1/12

#
Nokomanida wrote...
Alright I did that, and then I tried a very basic
1
2
3
4
if (getImage() == new GreenfootImage("Front1.png"))
            {
                setImage(new GreenfootImage("Back1.png"));
            }
for when the "up" key is being pressed.
Not sure how I forgot to mention this in my last comment but it doesn't work at all xD
Well, the code does execute as written. It just does not execute the way you thought it might. The condition 'getImage() == new GreenfootImage("Front1.png")' will always return 'false'. The image returned by 'getImage' is not the same object as the new one created. They are two distinct instances. When using the conditional equality operator, '==', on objects, a 'true' value is only returned if the memory location of both objects are the same. To compare the images for the same content (whether the same object or different objects), use the 'equals' method of comparison:
1
if (getImage().equals(new GreenfootImage("Front1.png")))
By using fields to hold the images, as Greg.Hutchinson suggests, you can set the image to one and then be guaranteed that it will be the same as one of the images held (and you can then use '==' for comparison):
1
if (getImage() == up)
because you may have set it with:
1
setImage(up);
davmac davmac

2015/1/12

#
When using the conditional equality operator, '==', on objects, a 'true' value is only returned if the memory location of both objects are the same.
This is true as an implementation detail. I prefer to say that the value will be true only if the expressions on either side yield references to the same object instance, as you don't then need to understand the concept of 'memory location' to program in Java.
Nokomanida Nokomanida

2015/1/12

#
Thanks for all the responses, I ended up sitting down last night and truly thought about it, and what I ended up doing was
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
public class Person extends Actor
{
    // Declare variables.
    int picIndex = 1; // Keep track of the picture index.
    int picFrequency = 3;
    int movementCounter = 1;
    String picImage = "";
     
    public void act()
    {
        move();
    }
     
    public void move()
    {
        int y = getY();
        int x = getX();
         
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(x, y-1);
            movementCounter++;
            picImage = "Back";
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(x, y+1);
            movementCounter++;
            picImage = "Front";
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(x-1, y);
            movementCounter++;
            picImage = "Left";
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(x+1, y);
            movementCounter++;
            picImage = "Right";
        }
        if (picImage != "" & movementCounter > picFrequency)
        {
            setImage(new GreenfootImage(picImage + picIndex + ".PNG"));
            movementCounter = 1;
            if (picIndex == 9)
                picIndex = 1;
            else
                picIndex++;
        }
    }
}
and it works perfectly :D
You need to login to post a reply.