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

2023/6/11

Image doesn't switch

Hitsheep Hitsheep

2023/6/11

#
I'm trying to make a bar that switches to 3 other images as time goes while the button C is held down. However, the image doesn't switch. Can someone explain what went wrong? Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class fishingBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class fishingBar extends Actor
{
    public static String[] bars = {"fishingBar0.png", "fishingBar1.png", "fishingBar2.png", "fishingBar3.png"};
    private boolean isC = false;
    /**
     * Act - do whatever the fishingBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int x, y, maxT = 3000;
    
    SimpleTimer timer = new SimpleTimer();    
    
    public void act()
    {
        while(Greenfoot.isKeyDown("c")){
            timer.mark();
            isC = true;
            //long prev = 0;
            //long cur = System.currentTimeMillis()/1000;
            if(timer.millisElapsed() > maxT + 50) {
                setImage(new GreenfootImage(bars[0]));
                timer.mark();
            }
            else if(timer.millisElapsed() >= 1000 && timer.millisElapsed() < 2000) setImage(bars[1]);
            else if(timer.millisElapsed() >= 2000 && timer.millisElapsed() < 3000) setImage(bars[2]);
            else if(timer.millisElapsed() >= 3000 && timer.millisElapsed() <= 3050) setImage(bars[3]);
            
            
        }
        getWorld().removeObject(this);
    }
}
danpost danpost

2023/6/12

#
Hitsheep wrote...
I'm trying to make a bar that switches to 3 other images as time goes while the button C is held down. However, the image doesn't switch. Can someone explain what went wrong? << Code Omitted >>
Using a SimpleTimer is making things more difficult than they need to be. You can just count act steps to time the fishingBar:
import greenfoot.*;

public class fishingBar extends Actor
{
    private GreenfootImage[] images; = new GreenfootImage[4];
    private int timer;
    
    public fishingBar() {
        for (int i=0; i<images.length; i++) {
            images[i] = new GreenfootImage("fishingBar"+i+".png");
        }
        setImage(images[0]);
    }
    
    public void act() {
        if ( ! Greenfoot.isKeyDown("c")) {
            getWorld().removeObject(this);
            return;
        }
        timer = (timer+1)%(60*images.length);
        setImage(images[timer/60]);
    }
}
Hitsheep Hitsheep

2023/6/12

#
danpost wrote...
Hitsheep wrote...
I'm trying to make a bar that switches to 3 other images as time goes while the button C is held down. However, the image doesn't switch. Can someone explain what went wrong? << Code Omitted >>
Using a SimpleTimer is making things more difficult than they need to be. You can just count act steps to time the fishingBar:
import greenfoot.*;

public class fishingBar extends Actor
{
    private GreenfootImage[] images; = new GreenfootImage[4];
    private int timer;
    
    public fishingBar() {
        for (int i=0; i<images.length; i++) {
            images[i] = new GreenfootImage("fishingBar"+i+".png");
        }
        setImage(images[0]);
    }
    
    public void act() {
        if ( ! Greenfoot.isKeyDown("c")) {
            getWorld().removeObject(this);
            return;
        }
        timer = (timer+1)%(60*images.length);
        setImage(images[timer/60]);
    }
}
I see. Thank you!
You need to login to post a reply.