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/26

#
ronald wrote...
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
In your code above (here), line 38 only ever references the last song assign it by line 57. So, line 110 only works on last bar created.
ronald ronald

2021/2/26

#
I tried to put this in an array but line 110 indicates that a method is needed
danpost danpost

2021/2/26

#
ronald wrote...
I tried to put this in an array but line 110 indicates that a method is needed
Did you try:
valueBar[songIndex]....
ronald ronald

2021/2/26

#
I just tried and here is the error
valueBar[songIndex].setImage(img);
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Background.updateValueDisplay(Background.java:108) at Background.<init>(Background.java:55) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at greenfoot.core.Simulation.newInstance(Simulation.java:580) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$0(WorldHandlerDelegateIDE.java:143) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:470) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:190) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/2/26

#
ronald wrote...
I just tried and here is the error << Error Trace Omitted >>
Show constructor codes (public Background()).
ronald ronald

2021/2/26

#
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[i];
            updateValueDisplay();           
            addObject(valueBar[i],750,i*50+100);
            }
    }
danpost danpost

2021/2/26

#
Line 36 should starrt:
valueBar[i] = ...
ronald ronald

2021/2/26

#
you mean line 16 of the constructor I just gave you
valueBar[i] = new SimpleActor();
always the same error line 16 is line 54 java.lang.NullPointerException at Background.<init>(Background.java:54) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at greenfoot.core.Simulation.newInstance(Simulation.java:580) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$0(WorldHandlerDelegateIDE.java:143) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:470) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:190) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/2/27

#
Around line 38, you should have:
private Actor[] valueBar = new Actor[songs.length];
ronald ronald

2021/2/27

#
everything works very well Thank you for the percentage, I'll see another time, as long as I understand your scenario Barriers And Bars Demos I tried a little, I have the impression that we must copy all the code Isn't there another way to do it? thanks again for the help, danpost
ronald ronald

2021/2/27

#
I put it in the Greenfoot scenario the progress bars stop before the end while on the IDE they go to the end
You need to login to post a reply.
1
2
3
4