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

2019/11/16

mirrorVertically problem

Hundmat Hundmat

2019/11/16

#
So the Actor gets mirrored vertically when I press "A" for example, the problem is that the doesn't stop mirror vertically for some reason, could it be because the code is switching image (animation)?
public class SoldierBoy extends Actor
{
    
    GreenfootImage[] Idle ={new GreenfootImage("Idle1.png"),new GreenfootImage("Idle2.png"),new GreenfootImage("Idle3.png"),new GreenfootImage("Idle4.png"), new GreenfootImage("Idle5.png"), new GreenfootImage("Idle6.png"),new GreenfootImage("Idle7.png"),new GreenfootImage("Idle8.png"),new GreenfootImage("Idle9.png"),new GreenfootImage("Idle10.png")};
    GreenfootImage[] Walk ={new GreenfootImage("Walk (1).png"),new GreenfootImage("Walk (2).png"),new GreenfootImage("Walk (3).png"),new GreenfootImage("Walk (4).png"),new GreenfootImage("Walk (5).png"),new GreenfootImage("Walk (6).png"),new GreenfootImage("Walk (7).png"),new GreenfootImage("Walk (8).png"),new GreenfootImage("Walk (9).png"),new GreenfootImage("Walk (10).png")};
    GreenfootImage[] Attack ={new GreenfootImage("Attack (1).png"), new GreenfootImage("Attack (2).png"), new GreenfootImage("Attack (3).png"), new GreenfootImage("Attack (4).png"), new GreenfootImage("Attack (5).png"), new GreenfootImage("Attack (6).png"), new GreenfootImage("Attack (7).png"), new GreenfootImage("Attack (8).png"), new GreenfootImage("Attack (9).png"),new GreenfootImage("Attack (10).png")};
    GreenfootImage[] Dead ={new GreenfootImage("Dead (1).png"), new GreenfootImage("Dead (2).png"), new GreenfootImage("Dead (3).png"), new GreenfootImage("Dead (4).png"),new GreenfootImage("Dead (5).png"),new GreenfootImage("Dead (6).png"), new GreenfootImage("Dead (7).png"), new GreenfootImage("Dead (8).png"), new GreenfootImage("Dead (9).png"), new GreenfootImage("Dead (10).png")};
    
    int animationIndex = 0;
    
    
    private final int GRAVITY = 5;  
    private int velocity;
    private boolean ready =false;
    
    public void Size(){
       GreenfootImage image = getImage();
       image.scale(150, 150);
       setImage(image);
    }
    
    
    public void act() 
    {
        animationIndex = (animationIndex+1)%50;
        velocity = 0;
        
      
        fall();
        if(Greenfoot.isKeyDown("D"))
        {
            
            if(getRotation()==180)
         {
          getImage().mirrorVertically();
          turn(180);
         }
         MoveRight();
         
        }
        else if(Greenfoot.isKeyDown("A"))
        {
         
         if(getRotation()==0)
         {
          getImage().mirrorVertically();
          turn(180);
         }
         MoveLeft();
        }
        
        else if(Greenfoot.isKeyDown("k"))
        {
         Attack();
         
        }
        
        else
        {
         if (animationIndex%5 == 0) setImage(Idle[animationIndex/5]);
        }
        
        
        
        
        
        
        Size();
    }   
    
    public void Attack()
    {
      if(Greenfoot.isKeyDown("k"))
      {
        if (animationIndex%5 == 0) setImage(Attack[animationIndex/5]);
        }
    
    
    }
    
    public void MoveLeft(){
        int dx = 0;
        
        if (Greenfoot.isKeyDown("A")) {
            dx--; 
            animationIndex = (animationIndex+1)%50;
            if (animationIndex%5 == 0) setImage(Walk[animationIndex/5]);
        }
          
        
        setLocation(getX()+5*dx, getY());

    }
    
    public void MoveRight(){
        int dx = 0;
        
         if (Greenfoot.isKeyDown("D")) {
            dx++; 
            animationIndex = (animationIndex+1)%50;
            if (animationIndex%5 == 0) setImage(Walk[animationIndex/5]);
        }
        
        setLocation(getX()+5*dx, getY());

    }

    
    public void fall(){
     
       if(onground()==true){
        velocity = 0;
        
        while(onground())
        {
         setLocation(getX(),getY()-1);
        }
        
        setLocation(getX(),getY()+1);
        }
        
        else{
         velocity +=GRAVITY;
        }
        setLocation(getX(),getY()+velocity);
    }
    
       public boolean onground(){
       boolean onground = false;
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
      
       if(getOneObjectAtOffset(imageWidth/-2, imageHeight/3, Ground.class) !=null || 
         getOneObjectAtOffset(imageWidth/2, imageHeight/3, Ground.class) !=null )
       {
          onground = true;
         }
        
    
         return onground;
     }
    
}
danpost danpost

2019/11/16

#
The line:
getImage().mirrorVertically();
mirrors the current image. When executed again, it mirrors that mirrored image. Etc. You will either have to keep track of whether each image in each array is already mirrored or not OR keep and use the mirrored images in separate arrays.
You need to login to post a reply.