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

2022/6/6

casting fishing rod animation only plays 1 frame every time i click the key

1
2
KCee KCee

2022/6/10

#
is there a built in method for displaying an image from a file? or should i make an actor class for each type of fish? because i only know of one way to put an image onto the screen which is through putting the actor on screen
danpost danpost

2022/6/10

#
KCee wrote...
is there a built in method for displaying an image from a file? or should i make an actor class for each type of fish? because i only know of one way to put an image onto the screen which is through putting the actor on screen
A class for each fish is a bit much. A single class, maybe called TempImage would be sufficient. You can give it the behavior of removing itself after a short period of time ( a second or two). You can either set its image after you create one, or you can pass the image to its constructor (using a GreenfootImage parameter). Line 54 would then be:
addObject(new TempImage(image), getWidth()/2, getHeight()/2);
KCee KCee

2022/6/11

#
im getting a few errors that im not sure how to fix. For example when I try to set the TempImage classes image to fishImages it tells me there are no suitable methods for setImage
public void getFish()
    {
        int[] fishes = new int[100];
        int n = 0;
        //making an array for each type of fish using the percentages in the
        //fishCount array so that when it chooses a random number, depending on 
        //which fish's array the number is in,it will choose that type of fish
        for (int t=0; t<typeCount; t++)
        {
            for (int i=0; i<fishCount[t]; i++) 
            fishes[n+i] = t;
            n += fishCount[t];
        }
         
        // choose a caught fish
        int caughtFish = Greenfoot.getRandomNumber(100);
        int type = fishes[caughtFish]; // type of fish caught
        GreenfootImage image = fishImages[type];
        TempImage.setImage(fishImages);
        addObject(new TempImage(image), getWidth()/2, getHeight()/2);
    }
Furthermore i don't really understand how to use the removeObject method correctly i my TempImage class
public class TempImage extends Actor
{
    /**
     * Act - do whatever the cFish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    GreenfootImage fishType = getImage();
    GreenfootImage[] fishImages;
    private final int typeCount = 3;
    private int timer = 120;
    private boolean TimerStart = false;
    public TempImage()
    {
        fishImages = new GreenfootImage[typeCount];
        fishType.scale(80, 50);
        for (int i = 0; i < typeCount; i++)
        {
            fishImages[i] = new GreenfootImage("Images/fishTypes/fish" + i + ".png");
        }
    }
    
    
    public void act()
    {
        TimerStart = true;
        if (TimerStart == true)
        {
            timer--;
            if (timer <= 0)
            {
                ((MyWorld)getWorld()).removeObject(TempImage);
            }
        }
        
    }
}
danpost danpost

2022/6/11

#
KCee wrote...
when I try to set the TempImage classes image to fishImages it tells me there are no suitable methods for setImage << Code Omitted >>
Replace lines 19 and 20 with the following:
Actor tempActor = new TempImage();
tempActor.setImage(image);
addObject(tempActor, getWidth()/2, getHeight()/2);
i don't really understand how to use the removeObject method correctly i my TempImage class << Code Omitted >>
Replace your TempImage class with the following:
import greenfoot.*;

public class TempImage extends Actor
{
    int timer;
    
    public void act()
    {
        if (++timer == 120) getWorld().removeObject(this);
    }
}
You need to login to post a reply.
1
2