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

2012/12/26

animation stopping problems

KierTulp KierTulp

2012/12/26

#
hello everybody! I have made an animation for my robot, but when i stop walking it will stay on the picture last used, meaning that half of the time he will not move but the walking image is still showing. I have tried to make it that if the right key is not pressed he will set the image to stand, but that will make it do that all the time, even when i walk left.
 public int sequence = 0;  
    public GreenfootImage stand = new GreenfootImage("Player_groot_rechts.png");  
    public GreenfootImage walk1 = new GreenfootImage("Player_groot1.png");  
    public GreenfootImage walk2 = new GreenfootImage("Player_groot2.png");
    
    public GreenfootImage standL = new GreenfootImage("Player_groot_links.png");  
    public GreenfootImage walk3 = new GreenfootImage("Player_groot3.png");  
    public GreenfootImage walk4 = new GreenfootImage("Player_groot4.png");  
private void animator()
    {
        if (Greenfoot.isKeyDown("right"))
        { 
                sequence++;
                 switch (sequence)
                {
                 case 1: setImage(walk1); break;
                 case 6: setImage(stand); break;
                 case 11: setImage(walk2); break;
                 case 16: sequence = 0; break;
                }
            }
            
        if (Greenfoot.isKeyDown("left"))
        { 
                sequence++;
                 switch (sequence)
                {
                 case 1: setImage(walk3); break;
                 case 6: setImage(standL); break;
                 case 11: setImage(walk4); break;
                 case 16: sequence = 0; break;
                }
            }
        }
these are the codes I used to animate him. http://www.greenfoot.org/scenarios/7135 thats the link to the scenario if you want to check it out :D. thanks in advance.
MatheMagician MatheMagician

2012/12/26

#
just just change the animator method to
private void animator()  
    {  
        if (Greenfoot.isKeyDown("right"))  
        {   
                sequence++;  
                 switch (sequence)  
                {  
                 case 1: setImage(walk1); break;  
                 case 6: setImage(stand); break;  
                 case 11: setImage(walk2); break;  
                 case 16: sequence = 0; break;  
                }  
            }  
              
        else if (Greenfoot.isKeyDown("left"))  
        {   
                sequence++;  
                 switch (sequence)  
                {  
                 case 1: setImage(walk3); break;  
                 case 6: setImage(standL); break;  
                 case 11: setImage(walk4); break;  
                 case 16: sequence = 0; break;  
                }  
            } 
         else
                  setImage(stand);
        }  
KierTulp KierTulp

2012/12/27

#
thanks, it works when i walk to the right, but when walking to the left, he will turn and look to the right again. do you know a way of fixing that too?
MatheMagician MatheMagician

2012/12/28

#
Ahh, I see you have two standing images, and the program can't figure out which one to use so it just goes with the one on the right. So you need to store which direction you were going in some kind of variable and then use that variable to decide where to face. There are two ways to do this: using a boolean and using a greenfootImage. If you want to use a greenfootImage, put this code with the rest of your variables:
public GreenfootImage standWhere = new GreenfootImage("Player_groot_rechts.png");    
Then, change your animator code to:
private void animator()    
    {    
        if (Greenfoot.isKeyDown("right"))    
        {     
                sequence++;    
                standWhere = stand;
                 switch (sequence)    
                {    
                 case 1: setImage(walk1); break;    
                 case 6: setImage(stand); break;    
                 case 11: setImage(walk2); break;    
                 case 16: sequence = 0; break;    
                }    
            }    
                
        else if (Greenfoot.isKeyDown("left"))    
        {     
                sequence++;    
                standWhere =  standL;
                 switch (sequence)
                {    
                 case 1: setImage(walk3); break;    
                 case 6: setImage(standL); break;    
                 case 11: setImage(walk4); break;    
                 case 16: sequence = 0; break;    
                }    
            }   
         else  
                  setImage(standWhere);  
        }    
What this code does is save which image to use in the standWhere variable whenever a key is pressed and then sets its image to that variable when no key is pressed. This way, you will always be facing the right direction. The second method is essentially the same accept we use a different variable. If you want to use this method, instantiate a new boolean with the rest of your variables:
boolean standRight = true;
then change your animator code to:
private void animator()    
    {    
        if (Greenfoot.isKeyDown("right"))    
        {     
                sequence++;    
                standRight = true;
                 switch (sequence)    
                {    
                 case 1: setImage(walk1); break;    
                 case 6: setImage(stand); break;    
                 case 11: setImage(walk2); break;    
                 case 16: sequence = 0; break;    
                }    
            }    
                
        else if (Greenfoot.isKeyDown("left"))    
        {     
                sequence++;    
                standRight = false;
                 switch (sequence)    
                {    
                 case 1: setImage(walk3); break;    
                 case 6: setImage(standL); break;    
                 case 11: setImage(walk4); break;    
                 case 16: sequence = 0; break;    
                }    
            }   
         else {
                if(standRight)
                        setImage(stand);  
                else 
                        setImage(standL);
          }
  }    
KierTulp KierTulp

2012/12/28

#
wow thanks! thanks for showing two ways of fixing it too, this way I actually know what your doing :D I used the first one btw.
danpost danpost

2012/12/28

#
Is your 'Uman the amazing walking robot' scenario somehow linked to the 'Save It' scenario by SPower? I think some unwanted thread issues are occuring because of it.
MatheMagician MatheMagician

2012/12/28

#
Glad I could help!
KierTulp KierTulp

2012/12/28

#
nope.. what kind of thread issues are occuring? maybe I can help.
danpost danpost

2012/12/28

#
I cannot say, exactly. All I know is each time I have downloaded that scenario, things (links, files, or whatever) are not closing properly and I continually get the terminal window showing some internal error of the 'Save It' scenario. I may not be explaining exactly what is happening properly, but it is something like that. That scenario is very buggy for some reason, and I have to reformat my drive twice because of it. Even after closing your scenario, if I load and exit a different one, I still get the same terminal window (it is continually trying to do something and erroring). The reason I had to reformat is because I could not (even after closing everything out) delete the scenario (system says it is still in use).
KierTulp KierTulp

2012/12/28

#
Okay, It might be the place you saved the files or whatever, i've had the same problems when one of my friends edited my scenario and i downloaded it and got the same error. I fixed it by deleting my old version of that game, so i guess you should delete one of the older versions of my game? I am not sure if that will help with the Save Me thing tho...
danpost danpost

2012/12/28

#
I did not have any versions of your scenario saved when I initially downloaded it both times. You had updated the scenario between both attempts at downloading and both times I had to reformat my drive to 'delete' the scenario (would not delete while my system thought the scenario was still open).
davemib123 davemib123

2012/12/28

#
danpost wrote...
I cannot say, exactly. All I know is each time I have downloaded that scenario, things (links, files, or whatever) are not closing properly and I continually get the terminal window showing some internal error of the 'Save It' scenario. I may not be explaining exactly what is happening properly, but it is something like that. That scenario is very buggy for some reason, and I have to reformat my drive twice because of it. Even after closing your scenario, if I load and exit a different one, I still get the same terminal window (it is continually trying to do something and erroring). The reason I had to reformat is because I could not (even after closing everything out) delete the scenario (system says it is still in use).
would it not have been easier to force shutdown of computer / laptop and then delete the scenario once the laptop / computer has booted back up?
danpost danpost

2012/12/28

#
I am using USB drives, so it was not too difficult to transfer everything from one to another, reformat, and then transfer back.
You need to login to post a reply.