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

2017/2/9

Attack animation problem

1
2
Hondrok Hondrok

2017/2/9

#
When I press space I want the actor to animate the shooting but is not working, I need to attack then move around then attack then move around again and attack to do the whole animation line 226-301
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 6;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
    
    private int frame = 0;
    private int attFrame = 0;
    private int attFrameM = 0;
    
    private int animationCounter = 0;
    private int attackCounter = 0;
    
    private GreenfootImage fr0 = new GreenfootImage("Mario_fr0.png");
    private GreenfootImage fr1 = new GreenfootImage("Mario_fr1.png");
    private GreenfootImage fr2 = new GreenfootImage("Mario_fr2.png");
    private GreenfootImage fr3 = new GreenfootImage("Mario_fr3.png");
    private GreenfootImage fr4 = new GreenfootImage("Mario_fr4.png");
    private GreenfootImage fr5 = new GreenfootImage("Mario_fr5.png");
    private GreenfootImage fr6 = new GreenfootImage("Mario_fr6.png");
    
    private GreenfootImage fr0m = new GreenfootImage("Mario_fr0m.png");
    private GreenfootImage fr1m = new GreenfootImage("Mario_fr1m.png");
    private GreenfootImage fr2m = new GreenfootImage("Mario_fr2m.png");
    private GreenfootImage fr3m = new GreenfootImage("Mario_fr3m.png");
    private GreenfootImage fr4m = new GreenfootImage("Mario_fr4m.png");
    private GreenfootImage fr5m = new GreenfootImage("Mario_fr5m.png");
    private GreenfootImage fr6m = new GreenfootImage("Mario_fr6m.png");
    
    private GreenfootImage att_fr0m = new GreenfootImage("Mario_att_fr0m.png");
    private GreenfootImage att_fr1m = new GreenfootImage("Mario_att_fr1m.png");
    private GreenfootImage att_fr2m = new GreenfootImage("Mario_att_fr2m.png");
    
    private GreenfootImage att_fr0 = new GreenfootImage("Mario_att_fr0.png");
    private GreenfootImage att_fr1 = new GreenfootImage("Mario_att_fr1.png");
    private GreenfootImage att_fr2 = new GreenfootImage("Mario_att_fr2.png");
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
       animationCounter ++;
       attackCounter ++;
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            fall();
            jumpAnimation();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
         
    public void jumpAnimation()
    {
        if (getImage().equals(fr0m) ||
             getImage().equals(fr1m) || 
             getImage().equals(fr2m) || 
             getImage().equals(fr3m) || 
             getImage().equals(fr4m) || 
             getImage().equals(fr5m) || 
             getImage().equals(fr6m))
        {
            setImage(fr2m);
        }
        else if(getImage().equals(fr0m) ||
             getImage().equals(fr1m) || 
             getImage().equals(fr2m) || 
             getImage().equals(fr3m) || 
             getImage().equals(fr4m) || 
             getImage().equals(fr5m) || 
             getImage().equals(fr6m))
        {
            setImage(fr2);
        }
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(fr0);
        }
        else if(frame == 1) 
        {
            setImage(fr1);
        }
        else if(frame == 2) 
        {
            setImage(fr2);
        }
        else if(frame == 3) 
        {
            setImage(fr3);
        }
        else if(frame == 4) 
        {
            setImage(fr4);
        }
        else if(frame == 5) 
        {
            setImage(fr5);
        }
        else if(frame == 6) 
        {
            setImage(fr6);
            frame = 1;
            return;
        }
        
        frame++;
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateLeft();
        }
    }
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(fr0m);
        }
        else if(frame == 1) 
        {
            setImage(fr1m);
        }
        else if(frame == 2) 
        {
            setImage(fr2m);
        }
        else if(frame == 3) 
        {
            setImage(fr3m);
        }
        else if(frame == 4) 
        {
            setImage(fr4m);
        }
        else if(frame == 5) 
        {
            setImage(fr5m);
        }
        else if(frame == 6) 
        {
            setImage(fr6m);
            frame = 1;
            return;
        }

        frame++;
    }

    public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            if(attackCounter % 4 == 0)
            {
                checkAttackDirection();
            }
        }
    }
        
    public void checkAttackDirection()
    {
        if (getImage().equals(fr0m) ||
             getImage().equals(fr1m) || 
             getImage().equals(fr2m) || 
             getImage().equals(fr3m) || 
             getImage().equals(fr4m) || 
             getImage().equals(fr5m) || 
             getImage().equals(fr6m))
        {
            animateAttackLeft();
        }
        else if(getImage().equals(fr0) ||
             getImage().equals(fr1) || 
             getImage().equals(fr2) || 
             getImage().equals(fr3) || 
             getImage().equals(fr4) || 
             getImage().equals(fr5) || 
             getImage().equals(fr6))
        {
            animateAttackRight();
        }
    }
    
    public void animateAttackRight()
    {
        if(attFrame == 0)
        {
            setImage(att_fr0);
        }
        else if(attFrame == 1) 
        {
            setImage(att_fr1);
            getWorld().addObject(new Sphere_right(), getX() + 6, getY()-50);
        }
        else if(attFrame == 2) 
        {
            setImage(att_fr2);
            attFrame = 0;
            return;
        }
        
        attFrame++;
    }
    
    public void animateAttackLeft()
    {
        if(attFrameM == 0)
        {
            setImage(att_fr0m);
        }
        else if(attFrameM == 1) 
        {
            setImage(att_fr1m);
            getWorld().addObject(new Sphere_left_Mario(), getX() + 6, getY()-50);
        }
        else if(attFrameM == 2) 
        {
            setImage(att_fr2m);
            attFrameM = 0;
            return;
        }
        
        attFrameM++;
    }

    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
}
danpost danpost

2017/2/9

#
Hondrok wrote...
When I press space I want the actor to animate the shooting but is not working, I need to attack then move around then attack then move around again and attack to do the whole animation
This is getting quite complex. I think you need to gain some organization with the animations to simplify the code a bit and make it easier to work with. Maybe you can employ my Animation Support Class to control them. Then you can switch between sets of images instead of trying to set each individual one as needed.
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Hondrok wrote...
When I press space I want the actor to animate the shooting but is not working, I need to attack then move around then attack then move around again and attack to do the whole animation
This is getting quite complex. I think you need to gain some organization with the animations to simplify the code a bit and make it easier to work with. Maybe you can employ my Animation Support Class to control them. Then you can switch between sets of images instead of trying to set each individual one as needed.
I don't think it's complex as the only thing that doesn't work is the attack animation. Maybe I'll reorganize everything after fixing this problem because if I do it now it will all be a mess; if I can fix this problem
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Hondrok wrote...
When I press space I want the actor to animate the shooting but is not working, I need to attack then move around then attack then move around again and attack to do the whole animation
This is getting quite complex. I think you need to gain some organization with the animations to simplify the code a bit and make it easier to work with. Maybe you can employ my Animation Support Class to control them. Then you can switch between sets of images instead of trying to set each individual one as needed.
    private int attFrame = 0;
    private int attFrameM = 0;
    
    private int animationCounter = 0;
    private int attackCounter = 0;
    
    public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            if(attackCounter % 4 == 0)
            {
                checkAttackDirection();
            }
        }
    }
        
    public void checkAttackDirection()
    {
        if (getImage().equals(fr0m) ||
             getImage().equals(fr1m) || 
             getImage().equals(fr2m) || 
             getImage().equals(fr3m) || 
             getImage().equals(fr4m) || 
             getImage().equals(fr5m) || 
             getImage().equals(fr6m))
        {
            animateAttackLeft();
        }
        else if(getImage().equals(fr0) ||
             getImage().equals(fr1) || 
             getImage().equals(fr2) || 
             getImage().equals(fr3) || 
             getImage().equals(fr4) || 
             getImage().equals(fr5) || 
             getImage().equals(fr6))
        {
            animateAttackRight();
        }
    }
    
    public void animateAttackRight()
    {
        if(attFrame == 0)
        {
            setImage(att_fr0);
        }
        else if(attFrame == 1) 
        {
            setImage(att_fr1);
            getWorld().addObject(new Sphere_right(), getX() + 6, getY()-50);
        }
        else if(attFrame == 2) 
        {
            setImage(att_fr2);
            attFrame = 0;
            return;
        }
        
        attFrame++;
    }
    
    public void animateAttackLeft()
    {
        if(attFrameM == 0)
        {
            setImage(att_fr0m);
        }
        else if(attFrameM == 1) 
        {
            setImage(att_fr1m);
            getWorld().addObject(new Sphere_left_Mario(), getX() + 6, getY()-50);
        }
        else if(attFrameM == 2) 
        {
            setImage(att_fr2m);
            attFrameM = 0;
            return;
        }
        
        attFrameM++;
    }
maybe this is more simplified
danpost danpost

2017/2/9

#
Maybe if you used the animationCounter field for the attack counter as well (remove the attackCounter field. Then the images will be set during the same act cycles and the moving animation will not conflict with the attack animation.
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Maybe if you used the animationCounter field for the attack counter as well (remove the attackCounter field. Then the images will be set during the same act cycles and the moving animation will not conflict with the attack animation.
nope still doens't work
danpost danpost

2017/2/9

#
Hondrok wrote...
nope still doens't work
Show revised code.
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Hondrok wrote...
nope still doens't work
Show revised code.
private int attFrame = 0;
private int attFrameM = 0;
 
private int animationCounter = 0;
private int attackCounter = 0;
 
public void attack()
{
    if(Greenfoot.isKeyDown("space"))
    {
        if(attackCounter % 4 == 0)
        {
            checkAttackDirection();
        }
    }
}
     
public void checkAttackDirection()
{
    if (getImage().equals(fr0m) ||
         getImage().equals(fr1m) || 
         getImage().equals(fr2m) || 
         getImage().equals(fr3m) || 
         getImage().equals(fr4m) || 
         getImage().equals(fr5m) || 
         getImage().equals(fr6m))
    {
        animateAttackLeft();
    }
    else if(getImage().equals(fr0) ||
         getImage().equals(fr1) || 
         getImage().equals(fr2) || 
         getImage().equals(fr3) || 
         getImage().equals(fr4) || 
         getImage().equals(fr5) || 
         getImage().equals(fr6))
    {
        animateAttackRight();
    }
}
 
public void animateAttackRight()
{
    if(attFrame == 0)
    {
        setImage(att_fr0);
    }
    else if(attFrame == 1) 
    {
        setImage(att_fr1);
        getWorld().addObject(new Sphere_right(), getX() + 6, getY()-50);
    }
    else if(attFrame == 2) 
    {
        setImage(att_fr2);
        attFrame = 0;
        return;
    }
     
    attFrame++;
}
 
public void animateAttackLeft()
{
    if(attFrameM == 0)
    {
        setImage(att_fr0m);
    }
    else if(attFrameM == 1) 
    {
        setImage(att_fr1m);
        getWorld().addObject(new Sphere_left_Mario(), getX() + 6, getY()-50);
    }
    else if(attFrameM == 2) 
    {
        setImage(att_fr2m);
        attFrameM = 0;
        return;
    }
     
    attFrameM++;
}
and the animationCounter is incremented in the act method
danpost danpost

2017/2/9

#
Line 11 should be checking the animationCounter -- not the attackCounter.
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Line 11 should be checking the animationCounter -- not the attackCounter.
private int attFrame = 0;
private int attFrameM = 0;
 
private int animationCounter = 0;
private int attackCounter = 0;
 
public void attack()
{
    if(Greenfoot.isKeyDown("space"))
    {
        if(animationCounter % 4 == 0)
        {
            checkAttackDirection();
        }
    }
}
     
public void checkAttackDirection()
{
    if (getImage().equals(fr0m) ||
         getImage().equals(fr1m) || 
         getImage().equals(fr2m) || 
         getImage().equals(fr3m) || 
         getImage().equals(fr4m) || 
         getImage().equals(fr5m) || 
         getImage().equals(fr6m))
    {
        animateAttackLeft();
    }
    else if(getImage().equals(fr0) ||
         getImage().equals(fr1) || 
         getImage().equals(fr2) || 
         getImage().equals(fr3) || 
         getImage().equals(fr4) || 
         getImage().equals(fr5) || 
         getImage().equals(fr6))
    {
        animateAttackRight();
    }
}
 
public void animateAttackRight()
{
    if(attFrame == 0)
    {
        setImage(att_fr0);
    }
    else if(attFrame == 1) 
    {
        setImage(att_fr1);
        getWorld().addObject(new Sphere_right(), getX() + 6, getY()-50);
    }
    else if(attFrame == 2) 
    {
        setImage(att_fr2);
        attFrame = 0;
        return;
    }
     
    attFrame++;
}
 
public void animateAttackLeft()
{
    if(attFrameM == 0)
    {
        setImage(att_fr0m);
    }
    else if(attFrameM == 1) 
    {
        setImage(att_fr1m);
        getWorld().addObject(new Sphere_left_Mario(), getX() + 6, getY()-50);
    }
    else if(attFrameM == 2) 
    {
        setImage(att_fr2m);
        attFrameM = 0;
        return;
    }
     
    attFrameM++;
}
and the animationCounter is incremented in the act method
danpost danpost

2017/2/9

#
Any progress at all? Your last post did not supply any new information (other than showing the change I mentioned).
Hondrok Hondrok

2017/2/9

#
danpost wrote...
Any progress at all? Your last post did not supply any new information (other than showing the change I mentioned).
nope still the same
danpost danpost

2017/2/9

#
I think if you add a boolean field to track the direction Mario is facing that things will be a lot easier. Add the following field:
private boolean facingLeft;
Then, in your 'moveLeft' and 'moveRight' methods, set the value of the field accordingly. Finally, use value of the field instead of what image is set to the actor in the 'checkAttackDirection' method.
Hondrok Hondrok

2017/2/9

#
danpost wrote...
I think if you add a boolean field to track the direction Mario is facing that things will be a lot easier. Add the following field:
private boolean facingLeft;
Then, in your 'moveLeft' and 'moveRight' methods, set the value of the field accordingly. Finally, use value of the field instead of what image is set to the actor in the 'checkAttackDirection' method.
public void checkAttackDirection()
    {
        if (facingLeft = true && facingRight = false)
        {
            animateAttackLeft();
        }
        else if(facingLeft = false && facingRight = true)
        {
            animateAttackRight();
        }
    }
why is this giving me an error?
danpost danpost

2017/2/9

#
Hondrok wrote...
< Code Omitted > why is this giving me an error?
Because it thinks you are trying to set (true && facingRight) to false and then assign that (false again) to facingLeft. Use '==' to compare for equality. The single equal sign, '=', is for assigning values, not for comparing them. You only need one of them, 'facingLeft' and 'facingRight' as the value (true or false) is sufficient to determine the state the other one is tracking. I usually use the 'facingLeft' one as by default its value is initially false and the actor is normally facing right to begin with. The method above can simply be this:
public void checkAttackDirection()
{
    if (facingLeft) animateAttackLeft(); else animateAttackRight();
}
There are more replies on the next page.
1
2