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

2012/12/22

Problems with Movement Animation

1
2
danpost danpost

2012/12/24

#
Actually, it does not create a new image every cycle, but only when the image changes. However, if you would rather not create any new images during the main running of the scenario, you could go with Vonmeth's second code-set. If you wanted to give more structure to your code while not creating any new images during execution, you could use something like the following
// static and instance fields
static final int LEFT=1, RIGHT=0;
int facing=RIGHT;
GreenfootImage[] sImg={ new GreenfootImage("sr.png"),
      new GreenfootImage("sr.png").mirrorHorizontally()) };
GreenfootImage[][] wImg={ { new GreenfootImage("wr1.png"),
      new GreenfootImage("wr1.png").mirrorHorizontally()) },
    { new GreenfootImage("wr2.png"),
      new GreenfootImage("wr2.png").mirrorHorizontally()) },
    { new GreenfootImage("wr3.png"),
      new GreenfootImage("wr3.png").mirrorHorizontally()) },
    { new GreenfootImage("wr4.png"),
      new GreenfootImage("wr4.png").mirrorHorizontally()) },
    { new GreenfootImage("wr5.png"),
      new GreenfootImage("wr5.png").mirrorHoriztonally()) },
    { new GreenfootImage("wr6.png"),
      new GreenfootImage("wr6.png").mirrorHorizontally()) } };
// movement method
public void movement() // Hero's movement
{
    if(Greenfoot.isKeyDown("up")) // up
    {
        setLocation(getX(),getY()- mspeed/2);
    } 
    if(Greenfoot.isKeyDown("down")) // down
    {
        setLocation(getX(),getY()+ mspeed/2);
    } 
    if(Greenfoot.isKeyDown("left")) // left
    {
        setLocation(getX()- mspeed,getY());
        facing = LEFT;
    }
    if(Greenfoot.isKeyDown("right")) // right
    {
        setLocation(getX()+ mspeed,getY());
        facing = RIGHT;
    }
    if(!Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down")) 
    {
        moveIndex = -1;
    }
    else
    {
        moveIndex++;
    }
    moveAnimation();
}
// moveAnimation method
public void moveAnimation()
{
    if (moveIndex >= 60) moveIndex=0;
    if (moveIndex % 10==0)
    {  
        setImage(wImg[moveIndex/10][facing]);
    }
    if (moveIndex== -1) 
    {
        setImage(sImg[facing]);
    }
}
JimmyFake JimmyFake

2012/12/24

#
Oh, you're right, not every cycle, but everytime the image resets ^^ Thank you for the reply and the structuring of the code. I've got a last question: you declared the former boolean facingLeft as "static" "final" "int". What does static and final effect? And why did you chose int over boolean? Thank you ^^
JimmyFake JimmyFake

2012/12/24

#
Oh, uhm the code you posted doesn't work. I guess it's because you made an error in mirrowing the Images in the array. For example line 4-5:
GreenfootImage[] sImg={ new GreenfootImage("sr.png"),  
      new GreenfootImage("sr.png").mirrorHorizontally()) }; 
mirrorHorizontally() does simply mirror the referenced Image and returns void, not the mirrowed picture. I guess i found the error, but I'm still stuck at finding a nice and structured solution to this.
JimmyFake JimmyFake

2012/12/24

#
GreenfootImage[] sImg={ new GreenfootImage("sr.png"),  
        new GreenfootImage( sImg[0].mirrorHorizontally()) }; 
is not working :(
danpost danpost

2012/12/24

#
Apparently the field must be set before using 'mirror...' methods. Adjust the following:
GreenfootImage[][] wImg={ { new GreenfootImage("wr1.png"),
      new GreenfootImage("wr1.png") },
    { new GreenfootImage("wr2.png"),
      new GreenfootImage("wr2.png") },
    { new GreenfootImage("wr3.png"),
      new GreenfootImage("wr3.png") },
    { new GreenfootImage("wr4.png"),
      new GreenfootImage("wr4.png") },
    { new GreenfootImage("wr5.png"),
      new GreenfootImage("wr5.png") },
    { new GreenfootImage("wr6.png"),
      new GreenfootImage("wr6.png") } };
 // add the following constructor     
public Hero()
{
    for (int i=0; i<wImg.length; i++) wImg[i][1].mirrorHorizontally();
}
JimmyFake JimmyFake

2012/12/25

#
and GreenfootImage sImg={ new GreenfootImage("sr.png"), new GreenfootImage("sr.png")} + public Hero() { ... sImg.mirrorHorizontally(); ... } ^^ and it's working ^^ thank you really much :)
JimmyFake JimmyFake

2012/12/26

#
Uhm, I've noticed another problem. The code is working fine. And playing the game in my Greenfoot Application is fine too. But if I export the game into a java file, or upload it to Greenfoot the mirrowed Images are colored slightly red. Walking right is fine, walking left works, too, but with reddish Images. Why's that O.o
danpost danpost

2012/12/26

#
You need to login to post a reply.
1
2