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

2019/10/25

Flip a GIF image depending on which way you are going

Hundmat Hundmat

2019/10/25

#
I use this code* inorder to flip the image but it don't work, either it doesn't flip or it keeps fliping when i walk one direction. Could someone tell me what I'm doing wrong :) *
1
2
3
4
5
6
7
8
9
10
if((getRotation()==180 && Greenfoot.isKeyDown("D"))
||
 (getRotation()==0 &&
Greenfoot.isKeyDown("A")))
{
          
         getImage().mirrorVertically();
           
         
         }
Hundmat Hundmat

2019/10/25

#
I also setRotation to 0 and 180 when I switch walking direction
danpost danpost

2019/10/25

#
Hundmat wrote...
I also setRotation to 0 and 180 when I switch walking direction
Where? It needs to be done right along with the code given (not outside the block of given code).
Hundmat Hundmat

2019/10/25

#
This is my code for the moment
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
public void Animation(){
        int x = getX();
        int y = getY();
         
        if((getRotation()==180 && Greenfoot.isKeyDown("D") ) || (getRotation()==0 &&Greenfoot.isKeyDown("A") )){
          
         getImage().mirrorVertically();
           
         
         }
     
          
        if(Greenfoot.isKeyDown("E")){
             
            setImage(Attackgif.getCurrentImage());
            
        }
         
        else if(Greenfoot.isKeyDown("D")){
            setImage(Walkgif.getCurrentImage());
             
            x+=3;
        }
         
        else if(Greenfoot.isKeyDown("A")){
             
            setImage(Walkgif.getCurrentImage());
      
             
             
              
            x-=3;
        }
         
        else
        {
         setImage(Stancegif.getCurrentImage());
        }
         
       
        setLocation(x,y);
 
    }
danpost danpost

2019/10/25

#
Hundmat wrote...
I also setRotation to 0 and 180 when I switch walking direction << Later Code Omitted >>
Apparently, you are NOT setting any rotations (as you stated you were). Also, before, it seemed like you were using only one still image that needed mirrored. Apparently, you have at least 3 possible gif image sets, all of which will probably require mirroring. In addition, it may be necessary to keep track of which set is currently being used; and, there may be some additional difficulties in having your attack gif image set start at its first image and run through only once. The GifImage class does not have implementation to do so. However, it is not a big deal if you do not mind it starting anywhere (at any image in its set) and continue looping through the images until the attack key is released.
Hundmat Hundmat

2019/10/26

#
My bad, I setRotation when I change x value (x-=3) for example, so you believe that the problem occurs because I have several different images?
Hundmat Hundmat

2019/10/26

#
I have no problem with holding E to loop through the attack gif , i just need to time the hit box right.
danpost danpost

2019/10/26

#
Hundmat wrote...
I setRotation when I change x value (x-=3) for example,
No. You only assign x a new position coordinate value to place the actor at with that. The rotation of your actor is always zero; so, getRotation() == 0 will always be true and getRotation() == 180 will always be false. So, though you thought you had control over the mirroring, you actually did not; and the images would flip continuously while the "A" key was pressed.
so you believe that the problem occurs because I have several different images?
I did not say that; although, that makes things so much more difficult (not impossible) to produce image control code. Your current code is bunk and you need to start afresh. I would suggest you start with a GifImage array to contain all your gifs. Include one for each: * walking right * walking left * standing * attacking right * attacking left I am not sure what your standing animation looks like; it may need to be right and left, also. The left facing images can be created from the right facing ones by creating to GifImage object from the same image set and either mirroring one set of images or by coding a double for loop to iterate over the rows of pixels in one and reversing the order on the other for all images in the set. Next, I would suggest a simple int field to contain the array index of the animation set currently being used. It might also help to arrange the animation sets in the array in such a way as to make their position in the array hint as to what it does. For example: { left-walk, null, right-walk, null, front-stance, null, left-attack, front-attack, right-attack } Note that I have them implicitly groups in threes (first 3 for walk, next for stance and last for attack) with order for each being left, front and right. Do not check on keys multiple times like you did in your previous code (see "D" on lines 5 and 19; also, "A" on lines 5 and 25). You can eliminate the double checking by using a local variable for direction:
1
2
3
int dx = 0;
if (Greenfoot.isKeyDown("A")) dx--;
if (Greenfoot.isKeyDown("D")) dx++;
You can later, re-position (move) with:
1
setLocation(getX()+3*dx, getY());
The dx variable can be used along with the array index of current animation to determine any change in direction or if an attack is currently being executed.
Hundmat Hundmat

2019/10/26

#
Thank you so much, will try to make it work :D
Hundmat Hundmat

2019/10/29

#
how do you create a gifimage array?
danpost danpost

2019/10/29

#
Hundmat wrote...
how do you create a gifimage array?
1
GifImage[] gifs = new GifImage[9];
You need to login to post a reply.