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

2017/12/6

Can I have help flipping my Immage

Bandito Bandito

2017/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.*;
 
/**
 * This is the human, the last survivor on earth
 *
 * @author Eric Faltermeier
 * @version 12/6/2017
 */
public class Human extends Actor
{
    private boolean isDown;
    private GreenfootImage rightImage;
    public Human()
    // This makes the human more to scale with the rest of the world.
    {
        //myImage = new GreenfootImage("Human.gif");
        GreenfootImage myImage = getImage();
        int myNewWidth = (int)myImage.getWidth()/8;
        int myNewHeight = (int)myImage.getHeight()/8;
        myImage.scale(myNewWidth,myNewHeight);
        rightImage = myImage;
 
    }
 
    public void act()
    {
        move();
    }
 
    public void move()
    // This is where the character is controlled it makes it so the character face the right way
    {
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() +2);
        }
 
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() -2);
        }
 
        if (Greenfoot.isKeyDown("a"))
        {
            move(-2);
            if(Greenfoot.isKeyDown("a") && !isDown)
            {
                getImage().mirrorHorizontally();
                isDown = true;
            }
            if(!Greenfoot.isKeyDown("a") && isDown)
            {
                isDown = false;
            }
        }
 
        if (Greenfoot.isKeyDown("d"))
        {
            setImage("Human2.gif");
            GreenfootImage myImage = getImage();
            int myNewWidth = (int)myImage.getWidth()/8;
            int myNewHeight = (int)myImage.getHeight()/8;
            myImage.scale(myNewWidth,myNewHeight);
            move(2);           
        }
    }
}
Bandito Bandito

2017/12/6

#
It flips to the left when I press "a" like it is suposed to then it fips back with d but it doesnt flip back again with a i need help.
danpost danpost

2017/12/6

#
Instead of mirroring the image repeatedly, keep a reference to images facing both ways and set them to the actor when needed.
Bandito Bandito

2017/12/8

#
So would it be something more like this
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
private GreenfootImage rightImage;
   private GreenfootImage leftImage;
   public Human()
   // This just difines what rightImage and leftImage are so they can be used later in the code
   {
       rightImage = new GreenfootImage ("HumanRight.gif");
       leftImage = new GreenfootImage ("HumanLeft.gif");
   }
 
   public void act()
   {
       move();
   }
 
   public void move()
   // This is where the character is controlled it makes it so the character face the right way
   {
       if (Greenfoot.isKeyDown("s"))
       {
           setLocation(getX(), getY() +2);
       }
 
       if (Greenfoot.isKeyDown("w"))
       {
           setLocation(getX(), getY() -2);
       }
 
       if (Greenfoot.isKeyDown("a"))
       {
           setImage("leftImage");
           move(-2);
       }
 
       if (Greenfoot.isKeyDown("d"))
       {
           setImage("rightImage");          
           move(2);           
       }
   }
(Sorry it took me so long to reply)
danpost danpost

2017/12/8

#
Remove the quotes in lines 30 and 36.
Bandito Bandito

2017/12/9

#
That worked. Thanks for your help!
You need to login to post a reply.