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

2015/12/12

help on animation

ArchMageGaming ArchMageGaming

2015/12/12

#
ok so I am trying to animate my character "shank", and when It sets the image it stays that image instead of going back to the original which it is supposed to do, here is my code:
 public void act() 
    {
        int time = 0;
       if(Greenfoot.isKeyDown("a")){
           move(-2);
          
           time = time + 1;
           if(time == 1){
               setImage("Shankmove2.jpg");
    }   
    if(time == 2){
     time = 0;   
}
}
       if(Greenfoot.isKeyDown("d")){
           move(2);
    }
}
I believe it is because the variable "time" Is not continuously adding 1 like I want it to be, any suggestions on how to fix?
ArchMageGaming ArchMageGaming

2015/12/12

#
also I think it is because the code in the if statement running the code to reset it isn't working properly
Super_Hippo Super_Hippo

2015/12/12

#
Place line 3 outside the act method.
danpost danpost

2015/12/12

#
ArchMageGaming wrote...
ok so I am trying to animate my character "shank", and when It sets the image it stays that image instead of going back to the original which it is supposed to do
There is no 'setImage' line to set the image back to the original one. As is, the code above is equivalent to this:
public void act()
{
    if (Greenfoot.isKeyDown("a"))
    {
        move(-2);
        setImage("Shankmove2.jpg");
    }
    if (Greenfoot.isKeyDown("d"))
    {
        move(2);
    }
}
The condition to set the image to the alternate is that the actor is moving backward. I am not quite sure that is what you are wanting. If the image is to continuously alternate between the two images, then the time and conditions to control this should not be inside other 'if' blocks. Also, the image will be "hyperactive" unless you slow down the animation -- let the timer run a little before changing the image (maybe 10 to 20 acts between changes).
ArchMageGaming ArchMageGaming

2015/12/14

#
ok thanks for helping ill see if it works
You need to login to post a reply.