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

2019/1/19

Animating an Attack

iminblue iminblue

2019/1/19

#
Hey guys I'm trying to make a game but I can't find a way to animate an attack after I press 'X' . I already have a set of sprites and all but I don't know how to delay any other animations until the attack animation is finished. Help ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private final GreenfootImage[] idleLeftAnimation =
    {
        new GreenfootImage("yeethmm.png"),
        new GreenfootImage("yeethmm2.png"),
        new GreenfootImage("yeethmm3.png")
    };
    
    private final GreenfootImage[] idleRightAnimation =
    {
        new GreenfootImage("left1.png"),
        new GreenfootImage("left2.png"),
        new GreenfootImage("left3.png")
    };
    
    private final GreenfootImage[] RunRightAnimation =
    {
        new GreenfootImage("run_right_0.png"),
        new GreenfootImage("run_right_1.png"),
        new GreenfootImage("run_right_2.png"),
        new GreenfootImage("run_right_3.png"),
        new GreenfootImage("run_right_4.png"),
        new GreenfootImage("run_right_5.png")    
    };
    
    private final GreenfootImage[] RunLeftAnimation =
    {
        new GreenfootImage("run_left_0.png"),
        new GreenfootImage("run_left_1.png"),
        new GreenfootImage("run_left_2.png"),
        new GreenfootImage("run_left_3.png"),
        new GreenfootImage("run_left_4.png"),
        new GreenfootImage("run_left_5.png")    
    };
    
    private final GreenfootImage[] AttackRightAnimation =
    { 
      new GreenfootImage("adventurer-attack1-00.png"),
      new GreenfootImage("adventurer-attack1-00.png"),
      new GreenfootImage("adventurer-attack1-00.png"),
      new GreenfootImage("adventurer-attack1-00.png"),
      new GreenfootImage("adventurer-attack1-00.png")
    };
    
    public int[] ok ={0,0,0,0};
    public int frame=0;
    public char direction='l';
    public int base_speed=5;
    
    public void act()
    {
       // if (attack()) {animateAttackRight();}
        
         if(move()) {if (direction=='l') 
                 animateRunLeft();
                 else animateRunRight();
    }
        else if (direction=='r') 
             animateIdleLeft();
        else animateIdleRight();
    }
    
    public boolean move() 
    {
        // Add your action code here.
        moveLeft();
        moveRight();
        moveUp();
        moveDown();
        if (ok[0]==ok[2] && ok[1]==ok[3]) return false;
        return true;
       
    }   
    
    public boolean attack()
    {
        if (Greenfoot.isKeyDown("x")) //algoritm de combat
        return true;
        return false;
    }
    
    public void moveLeft()
    {
        if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a"))
        {setLocation(getX()-base_speed,getY());
            direction='l';
            ok[3]=1;}
            else ok[3]=0;}
            
    public void moveRight()
    {
        if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d"))
        {setLocation(getX()+base_speed,getY());
            direction='r'; 
         ok[1]=1;}
           else ok[1]=0;}
    
    public void moveUp()
    {
        if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w"))
        {setLocation(getX(),getY()-base_speed);
         ok[0]=1;}
        else  ok[0]=0;}    
         
    public void moveDown()
    {
        if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s"))
        {setLocation(getX(),getY()+base_speed);
        ok[2]=1;}
        else ok[2]=0;}      
         
    public void animateIdleLeft()
    {
        frame++;
        setImage(idleLeftAnimation[frame/20]);
        if (frame==59) frame=-1;
    
    }
    
     public void animateIdleRight()
    {
        frame++;
        setImage(idleRightAnimation[frame/20]);
        if (frame==59) frame=-1;
    
    }
    
    public void animateRunRight()
    {
        frame++;
        setImage(RunRightAnimation[frame/10]);
        if (frame==59) frame=-1;
    }
    
    public void animateRunLeft()
    {
        frame++;
        setImage(RunLeftAnimation[frame/10]);
        if (frame==59) frame=-1;
    }
    
    public void animateAttackRight()
    {
        frame++;
        setImage(AttackRightAnimation[frame/3]);
        if (frame==8) frame = -1;
    }
}
danpost danpost

2019/1/20

#
The last reply in this. discussion thread might be useful to you.
iminblue iminblue

2019/1/23

#
danpost wrote...
The last reply in this. discussion thread might be useful to you.
Thank you ! I also have another question : How can I change the code in order to 'normalize' the movement speed ? I'm talking about the character going faster in diagonal
danpost danpost

2019/1/23

#
iminblue wrote...
How can I change the code in order to 'normalize' the movement speed ? I'm talking about the character going faster in diagonal
You could use a smooth mover support class.
iminblue iminblue

2019/1/23

#
danpost wrote...
iminblue wrote...
How can I change the code in order to 'normalize' the movement speed ? I'm talking about the character going faster in diagonal
You could use a smooth mover support class.
I don't know what that is but I somehow solved the problem anyways . Thank you very much
You need to login to post a reply.