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

2021/2/19

Button Sound Array

1
2
3
4
danpost danpost

2021/2/24

#
ronald wrote...
I have no code of the progress bar
Refer to my Value Display Tutorial for coding.
ronald ronald

2021/2/24

#
int lengthCurrentSong;
public void act()
    {      
        for(int i = 0; i<buttons.length; i++)
        {
        if(Greenfoot.mouseClicked(buttons[i]))
            {           
            if(currentSong!=null) currentSong.stop();
            currentSong = new GreenfootSound(songs[i]);
            currentSong.play();            
            }
        if(lengthCurrentSong==0)
           lengthCurrentSong++;
           
        }
        
    }
I don't know if I did the right thing for the code thank you for giving me your answer
danpost danpost

2021/2/25

#
These are ALL the required fields:
String[] songs = { ... }; // list of song file names
int[] songFrames = { ... }; // list of total frames per song
Actor[] buttons; // list of song buttons
GreenfootSound currentSong; // song last chosen to play
int songIndex; // index of last song chosen
Actor bar; // the progress bar
int barValue; // current length of colored portion of progress bar in pixels
int songProgress; // number of frames the song has been playing for
And the act method would be as follows:
public void act()
{
    for (int i=0; i<buttons.length; i++)
    {
        if (Greenfoot.mouseClicked(buttons[i]))
        {
            songIndex = i;
            songProgress = 0;
            if (currentSong != null) currentSong.stop();
            currentSong = new GreenfootSound(songs[i]);
            currentSong.play();
            break;
        }
    }
    if (currentSong != null && currentSong.isPlaying() && songProgress < songFrames[songIndex]) songProgress++;
    int newValue = barLength*songProgress/songFramexs[songIndex];
    if (newValue != barValue) updateBarValue(newValue);
}
"barLength" should be a hard-coded literal value for maximum number of pixels for full bar.
ronald ronald

2021/2/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
        
    static final Color TRANS = new Color(0,0,0,0);
     
    String[] songs = {
                     "60s rock beat.wav",
                     "Blues style piano.wav",
                     "Electronic.wav",
                     "Jingle bells.mp3",
                     "Mystic melody synth.wav",
                     "Nostalgic piano.mp3",
                     "Psycho rhodes.wav",
                     "Tech.mp3"
                     };                  
    Actor[] buttons = new Actor[songs.length];
    int[] songFrames = {960,1020,1500,900,900,4800,1200,1920};
    int songIndex;
    Actor Bar;
    int barValue;
    int songProgress;
    int barLength;    
    private GreenfootSound currentSong;
        
    private Actor valueBar;
    private int value = 0;
    private int maxValue = 100;
        
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
         
        GreenfootImage bg = getBackground();
         
        for(int i = 0; i<buttons.length; i++)
            {
            buttons[i] = getNewButton("BUTTON 0"+(i+1));
            addObject(buttons[i],500,i*50+100);
            
            GreenfootImage img = new GreenfootImage(songs[i],30,Color.BLUE,TRANS);
            bg.drawImage(img,50,85+i*50);
            
            valueBar = new SimpleActor();
            updateValueDisplay();           
            addObject(valueBar,750,i*50+100);
            }
    }
     
    public void act()
    {      
        for(int i = 0; i<buttons.length; i++)
        {
        if(Greenfoot.mouseClicked(buttons[i]))
            {
            songIndex=i;
            songProgress = 0;
            if(currentSong!=null) currentSong.stop();
            currentSong = new GreenfootSound(songs[i]);
            currentSong.play();
            break;
            }        
        }
        if(currentSong!= null && currentSong.isPlaying() && songProgress<songFrames[songIndex]) songProgress++;
        int newValue = barLength*songProgress/songFrames[songIndex];
        if (newValue!=barValue) updateBarValue(newValue);
        
    }
    
    public void adjustValue(int amount)
    {
        value+=amount;
        if(value<0) value = 0;
        if(value>maxValue) value = maxValue;
        updateValueDisplay();
    }
 
    private void updateValueDisplay()
    {
        int wide = 200;
        int high = 20;
 
        GreenfootImage fullImg = new GreenfootImage(wide,high);
        fullImg.setColor(Color.GREEN);
        fullImg.fill();
 
        GreenfootImage colorBar = new GreenfootImage(wide,high);
        int percentage = wide*value/maxValue;
        colorBar.drawImage(fullImg,percentage-wide,0);
 
        GreenfootImage img = new GreenfootImage(wide+4,high+4);
        img.setColor(Color.WHITE);
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0,0,wide+3,high+3);
        img.drawImage(colorBar,2,2);
        valueBar.setImage(img);
    }    
            
    public Actor getNewButton(String caption) 
    {        
     GreenfootImage base = new GreenfootImage(200,30);
     base.fill();
     base.setColor(Color.BLUE);
     base.fillRect(3,3,194,24);
        
     GreenfootImage text = new GreenfootImage(caption, 24, Color.WHITE, TRANS);
     base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
     base.setTransparency(128);
      
     Actor button = new SimpleActor();        
     button.setImage(base);
     return button;       
    } 
}

I give all the code otherwise this code would never end updateBarValue is in error cannot find symbol = method updateBarValue (int) thanks again for the code I who thought that there was only a single field int
danpost danpost

2021/2/25

#
ronald wrote...
I who thought that there was only a single field int
That was with dealing with song duration (my apologies, if that was not clear). Try this:
import greenfoot.*;

public class Background extends World
{
    static final Color TRANS = new Color(0,0,0,0);
    
    String[] songs =
    {
        "60s rock beat.wav",
        "Blues style piano.wav",
        "Electronic.wav",
        "Jingle bells.mp3",
        "Mystic melody synth.wav",
        "Nostalgic piano.mp3",
        "Psycho rhodes.wav",
        "Tech.mp3"
    };
    int songIndex;
    private GreenfootSound currentSong;
    Actor[] buttons = new Actor[songs.length];
    int[] songFrames = { 960, 1020, 1500, 900, 900, 4800, 1200, 1920 };
    
    int songProgress;
    private Actor valueBar;
    private int value = 0;
    private int maxValue = 100;
    
    public Background()
    {
        super(900, 600, 1);
        
        GreenfootImage bg = getBackground();
        
        for (int i=0; i<buttons.length; i++)
        {
            buttons[i] = getNewButton("BUTTON 0"+(i+1));
            addObject(buttons[i], 500, 100+i*50);
            
            GreenfootImage img = new GreenfootImage(songs[i], 30, Color.BLUE, TRANS);
            bg.drawImage(img, 50, 85+i*50);
        }
        valueBar = new SimpleActor();
        updateValueDisplay();
        addObject(valueBar, 750, 100);
    }
    
    public void act()
    {
        for (int i=0; i<buttons.length; i++)
        {
            if (Greenfoot.mouseClicked(buttons[i]))
            {
                songIndex = i;
                songProgress = 0;
                if (currentSong != null) currentSong.stop();
                currentSong = new GreenfootSound(songs[i]);
                currentSong.play();
                valueBar.setLocation(750, 100+i*50)
                break;
            }
        }
        if (currentSong != null && currentSong.isPlaying()) songProgress++;
        setValue(maxValue*songProgress/songFrames[songIndex]);
         
    }
    
    public void setValue(int amount)
    {
        value = amount;
        if (amount < 0) amount = 0;
        if (amount > maxValue) amount = maxValue;
        if (amount != value)
        {
            value = amount;
            updateValueDisplay();
        }
    }
    
    private void updateValueDisplay()
    {
        int wide = 200;
        int high = 20;
        
        GreenfootImage fullImg = new GreenfootImage(wide, high);
        fullImg.setColor(Color.GREEN);
        fullImg.fill();
        
        GreenfootImage colorBar = new GreenfootImage(wide, high);
        int percentage = wide*value/maxValue;
        colorBar.drawImage(fullImg, percentage-wide, 0);
        
        GreenfootImage img = new GreenfootImage(wide+4, high+4);
        img.setColor(Color.WHITE);
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0, 0, wide+3, high+3);
        img.drawImage(colorBar, 2, 2);
        valueBar.setImage(img);
    }
    
    public Actor getNewButton(String caption) 
    {
        GreenfootImage base = new GreenfootImage(200,30);
        base.fill();
        base.setColor(Color.BLUE);
        base.fillRect(3,3,194,24);
        
        GreenfootImage text = new GreenfootImage(caption, 24, Color.WHITE, TRANS);
        base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
        base.setTransparency(128);
        
        Actor button = new SimpleActor();
        button.setImage(base);
        return button;
    } 
}
ronald ronald

2021/2/25

#
again thanks for the code this code works and improves a question, why is it the last progress bar that works and not the first ??? it happened to me too if I understand it is that the progress bar is not a array
danpost danpost

2021/2/25

#
ronald wrote...
why is it the last progress bar that works and not the first ??? if I understand it is that the progress bar is not a array
Each time you assign one to Actor valueBar, it replaced the last one assigned to it. So, yes ... because you only are referencing one with an Actor field, not multiple with an Actor{ } array.
ronald ronald

2021/2/25

#
I looked at the forum simply add to Actor valueBar and all valueBars ??? thank you for giving me explanations
danpost danpost

2021/2/25

#
ronald wrote...
I looked at the forum simply add to Actor valueBar and all valueBars ??? thank you for giving me explanations
I do not know why you would want a progress bar for each song. You would only have one song playing at any time.
ronald ronald

2021/2/25

#
watch this code compile 3 or 4 or 5 or 10 songs we wonder if the 2 or 3 or 4 or 9 progress bars are broken and why the last progress bar is working? if we look at the number of software downloads, all the progress bars work each time a file is downloaded
ronald ronald

2021/2/26

#
valueBar.setImage(img);
What does it look like in array?
danpost danpost

2021/2/26

#
ronald wrote...
valueBar.setImage(img);
What does it look like in array?
You would need an array of values, as well. And setValue and updateValueDisplay would need to know which value or bar they are to work on. I thought my code above (with just one bar) was sufficient. Placing the bar next to the current song makes identifying the song easy.
ronald ronald

2021/2/26

#
of course I understand you if i can still improve the code, why not give it a try and I just had a great idea, why install a percentage counter that indicates the length of the song that is playing if I don't tell nonsense will have to use for loop for setValue and updateValueDisplay or not like for buttons and songs?
danpost danpost

2021/2/26

#
ronald wrote...
if i can still improve the code, why not give it a try
I do not know how it improves anything to have a bar for each song. You only need one bar for the current song, as only one will be playing at any time.
and I just had a great idea, why install a percentage counter that indicates the length of the song that is playing
You could add text along side the bar. It is an idea.
will have to use for loop for setValue and updateValueDisplay or not like for buttons and songs?
Not like. Initializing an int array has all values preset to zero, so no need for a loop.
ronald ronald

2021/2/26

#
If I have 3 songs, I click on the first button and the third progress bar works, I tell myself that there is a problem but hey do as you want I will think about the percentage counter for one song and not 3 songs
There are more replies on the next page.
1
2
3
4