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

2021/12/11

my shooting animation not fully animated

greenwood greenwood

2021/12/11

#
why my shooting animation only show until frames 6-7. My shooting animation supposed to have 18 frames.
import greenfoot.*;  


public class Wraith extends Mover
{
    private int shotTimer = 15;
    private int SpaceTimer = 15;
    private int AnyPressed = 0;
    
    private int ShootFrames = 18;
    private int ShootTime;
    private int ShootAnimate;
    
    private int LeftFrames = 9;
    private int LeftTime;
    
    private int RightFrames = 9;
    private int RightTime;
    
    Wraith()
    {
        
    }
    
    
    public void act()
    {
        shotTimer++;
        AnyPressed --;
        SpaceTimer --;
        ShootAnimate --;
        checkKeys();
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            moveUp();
        }
        if(Greenfoot.isKeyDown("s"))
        {
            moveDown();
        }
        if (Greenfoot.isKeyDown("a"))
        {
            animateLeft();
            moveLeft();
            AnyPressed = 50;
        }
        
        else{
            AnyPressed = 0;
        }
        if (Greenfoot.isKeyDown("d"))
        {
            animateRight();
            moveRight();
            AnyPressed =50;
        }
        
        else {
            AnyPressed = 0;
        }
        
        if(Greenfoot.isKeyDown("space") && SpaceTimer <=0)
        {
            ShootAnimate = 1;
            
            if(shotTimer > 25)
            {
                ShootTime =0;
                this.shoot();
                shotTimer = 0;
                AnyPressed = 50;
            }
            
            SpaceTimer = 25;
        }
        
        else{
            AnyPressed = 0;
        }
        
        if(ShootAnimate >=1){
            animateShoot();
        }
        
        
    }
    
    public void animateShoot(){
        setImage(animateShootx()[ShootTime]);
        if(ShootFrames < 18){
            ShootTime++;
        }
    }
    
    public void animateLeft(){
        setImage(animateLeftx()[LeftTime]);
        LeftTime++;
        if(LeftTime== LeftFrames){
            LeftTime=0;
        }
    }
    
    public void animateRight(){
        setImage(animateRightx()[RightTime]);
        RightTime ++;
        if(RightTime == RightFrames){
            RightTime = 0;
        }
    }
    
    public void shoot()
    {
        this.getWorld().addObject(new Fireball(), this.getX()+30, this.getY()-45);
    }
    
    
    public GreenfootImage[] animateShootx(){
        
        GreenfootImage[] images = new GreenfootImage[ShootFrames];
        
        for(int i=0; i<18; i++){
            
            if(ShootAnimate >=0){
                
                images[i] = new GreenfootImage("S_Right" +i+ ".png");
            }
        }
        
        return images;
    }
    
    public GreenfootImage[] animateLeftx(){
        
        GreenfootImage[] images = new GreenfootImage[LeftFrames];
        
        for(int i=0; i<9; i++){
            
            images[i] = new GreenfootImage("W_Left" + i + ".png");
        }
        
        return images;
    }
    
    public GreenfootImage[] animateRightx(){
        
        GreenfootImage[] images = new GreenfootImage[RightFrames];
        
        for(int i=0; i<9; i++){
            
            images[i] = new GreenfootImage("W_Right" + i + ".png");
        }
        
        return images;
    }
    
}
danpost danpost

2021/12/11

#
Line 94 seems quite suspicious. ShootFrames, I believe, should always be 18, so ShootTime will never increment on line 95.
danpost danpost

2021/12/11

#
There is a lot that can be improved upon in your code. The biggest concern I have is that you are loading a bunch of images every frame when moving or shooting, where you can have the images pre-loaded to use when needed.
You need to login to post a reply.