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

2014/10/8

Help With Turning/Flipping!

tReks tReks

2014/10/8

#
So when I turn to the left, the objects image is upside down. And that really bothers me. Especially when nyan cat is there. Can someone find a way so when the image is to the left, the image flips vertically (I know how to flip an image vertically but I don't know how to set up the code for it) Thanks in advance! -tReks Code For Lobster Class:
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Lobster extends Animal
{
    private int timer = 200;
    private int pic = 1;
    int nyan = 0;
    int dir = 0
;
    public void act()
    {
        move();
         
        if ( Greenfoot.isKeyDown("up") )
        {
            move(20);
        }
 
        if ( Greenfoot.isKeyDown("down") )
        {
            move(-25);
        }
 
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(10);
             
             
             
 
        }
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-10);
 
        }
 
        if (canSee(Crab.class))
        {
            eat(Crab.class);
            Greenfoot.playSound("au.wav");
        }
        if ( atWorldEdge() )
        {
            setRotation( getRotation() - 180 );
            Worm willy = new Worm();
            getWorld().addObject(willy,0,0);
 
        }
 
        if ( Greenfoot.isKeyDown("space") && nyan == 1 )
        {
            Rb rb = new Rb();
            getWorld().addObject(rb,getX(),getY());
            rb.setRotation(getRotation());
        }
        else
        {
            if ( Greenfoot.isKeyDown("space"))
            {   
                Worm wilma = new Worm();
                getWorld().addObject(wilma,getX(),getY());
                wilma.setRotation(getRotation());
            }
        }  
 
         
         
        if ( timer > 0 )
        {
            timer -=1;
 
        }
        else
        {
 
            if ( pic == 1 )
            {
                setImage("lobster.png");
                pic +=1;
                nyan = 0;
                timer=100;
 
            }
            else if (( pic == 2 ))
            {
                setImage("frog.png");
                pic +=1;
                nyan = 0;
                timer=100;
            }
            else if (( pic == 3 ))
            {
                setImage("frog.png");
                pic = 4;
                nyan = 0;
                timer=100;
            }
            else
            {
                setImage("nyan.png");
                nyan = 1;
                pic = 1;
                timer=300;
            }
 
        }
 
    }
 
}
NikZ NikZ

2014/10/8

#
getImage().mirrorVertically() should do it.
tReks tReks

2014/10/8

#
NikZ wrote...
getImage().mirrorVertically() should do it.
ya i know how to do that but when the cat is turned to the left, itll keep flipping. I need to know how to make it flip only once
danpost danpost

2014/10/8

#
Well, where is the code you have that makes it flip (at all)? That is, how are you trying to use it? Also, this is quite confusing. Are you using this one class for an actor that starts out as a lobster, turns into a frog and then finally, into a cat?
NikZ NikZ

2014/10/8

#
tReks wrote...
NikZ wrote...
getImage().mirrorVertically() should do it.
ya i know how to do that but when the cat is turned to the left, itll keep flipping. I need to know how to make it flip only once
Why would you want your cat to flip when turned? The only way it could look like a smooth turn is if the cat is turning towards you, which would require several images.
danpost danpost

2014/10/8

#
I think you just need to add another condition (other than whether it is going left or right) for mirroring the image. That is, something like this for adjusting the image:
1
2
3
4
5
6
if ((getRotation() == 180 && /* moving right */) ||
    (getRotation() == 0 && /* moving left */))
{
    turn(180);
    getImage().mirrorVertically();
}
You need to login to post a reply.