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

2022/6/8

I need my fisherman to reel in fish when i press e while in casting animation

KCee KCee

2022/6/8

#
So far my fisherman can cast his fishing rod and it'll stay in the fishing position until I press any movement key. I want my fisherman to also reel in his fishing line when I press the e key. I already have the images loaded in but the e key isnt making my fisherman play the reeling in animation. here's my code for the fisherman so far:
public class Fisherman extends Actor
{    
    GreenfootImage[] walkRight;
    GreenfootImage[] walkLeft;
    GreenfootImage[] castRight;
    GreenfootImage[] castLeft;
    GreenfootImage[] typeFish;
    GreenfootImage[] reelRight;
    GreenfootImage[] reelLeft;
    GreenfootImage[] currAnim;
    int animTimer;
    public Fisherman()
    {
        walkRight = new GreenfootImage[5];
        walkLeft = new GreenfootImage[5];
        castRight = new GreenfootImage[5];
        castLeft = new GreenfootImage[5];
        reelRight = new GreenfootImage[5];
        reelLeft = new GreenfootImage[5];
        
        for (int i = 0; i < 5; i++)
        {
            walkRight[i] = new GreenfootImage("images/walkingAni/walking" + i + ".png");
            walkRight[i].scale(100,80);
            walkLeft[i] = new GreenfootImage("images/walkingAni/walking" + i + ".png");
            walkLeft[i].scale(100,80);
            walkLeft[i].mirrorHorizontally();
            castRight[i] = new GreenfootImage("images/castAni/cast" + i + ".png");
            castRight[i].scale(100,80);
            castLeft[i] = new GreenfootImage("images/castAni/cast" + i + ".png");
            castLeft[i].scale(100,80);
            castLeft[i].mirrorHorizontally();
            reelRight[i] = new GreenfootImage("images/reelingAni/reel" + i + ".png");
            reelRight[i].scale(100,80);
            reelLeft[i] = new GreenfootImage("images/reelingAni/reel" + i + ".png");
            reelLeft[i].scale(100,80);
            reelLeft[i].mirrorHorizontally();
        }
        
        setAnimation(walkRight);
    }
    
    private void setAnimation(GreenfootImage[] anim)
    {
        currAnim = anim;
        animTimer = -1;
        setImage();
    }
    
    private void setImage()
    {
        animTimer = (animTimer+1)%(10*currAnim.length);
        if(animTimer%10 == 0) 
        {
            setImage(currAnim[animTimer/10]);
        }
    }
    
    public void act()
    {
        if(!casting())
        {
            move();
        }
    }
    
    boolean reeling()
    {
        if (currAnim == reelLeft || currAnim == reelRight)
        {
            setImage();
            if(animTimer == 0)
            {
                setAnimation(currAnim == reelLeft ? walkLeft : walkRight);
            }
            else
            {
                return true;
            }
        }
        
        if(Greenfoot.isKeyDown("e"))
        {
            setAnimation(currAnim == castLeft ? reelLeft : reelRight);
            return true;
        }
        
        return false;
    }

    private boolean casting()
    {
        if (currAnim == castLeft || currAnim == castRight)
        {
            setImage();
            if(animTimer == 0)
            {
                setAnimation(currAnim == castLeft ? reelLeft : reelRight);
            }
            else 
            {
                return true;
            }
        }
        
        if (Greenfoot.isKeyDown("f"))
        {
            setAnimation(currAnim == walkLeft ? castLeft : castRight);
            return true;
        }
        return false;
    }
    
    private void move()
    {
        int dx = 0;
        if (Greenfoot.isKeyDown ("a"))
        {
            dx--;
        }
        
        if (Greenfoot.isKeyDown("d"))
        {
            dx++;
        }
        
        if(dx == 0)
        {
            return;
        }
        
        setLocation(getX()+dx, getY());
        if (isTouching(Boundary.class))
        {
            setLocation(getX()-dx, getY());
        }
        
        if (dx < 0 && currAnim != walkLeft)
        {
            setAnimation(walkLeft);
        }
        
        else if (dx > 0 && currAnim != walkRight)
        {
            setAnimation(walkRight);
        }
        
        else
        {
            setImage();
        }
    }
}
Spock47 Spock47

2022/6/8

#
The method reeling is not called from anywhere. Therefore, it is never executed. It has to be called from act method or a method that itself is called from act method.
danpost danpost

2022/6/9

#
Spock47 wrote...
The method reeling is not called from anywhere.
The act method will end up having:
if ( ! reeling() && ! casting() )
KCee KCee

2022/6/9

#
danpost wrote...
Spock47 wrote...
The method reeling is not called from anywhere.
The act method will end up having:
if ( ! reeling() && ! casting() )
when i put that code in it automatically plays the reeling in animation right after the casting animation. what i need it to do is only play the reeling in animation after I press e. here's code for reeling animation:
    boolean reeling()
    {
        if (currAnim == reelLeft || currAnim == reelRight)
        {
            setImage();
            if(animTimer == 0)
            {
                setAnimation(currAnim == reelLeft ? walkLeft : walkRight);
            }
            else
            {
                return true;
            }
        }
        
        if(Greenfoot.isKeyDown("e"))
        {
            setAnimation(currAnim == castLeft ? reelLeft : reelRight);
            return true;
        }
        
        return false;
    }
You need to login to post a reply.