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

2016/5/7

Adding Resume Play to a Paused Scenario

1
2
danpost danpost

2016/5/7

#
That was supposed to be:
isPaused = true;
I mistyped the name of the field.
danpost danpost

2016/5/7

#
Simvarghese wrote...
so this is what my full code looks like. is this correct?
It is "correct" if it is working properly or as you want it to. This is not a question you should ask. Test out the scenario and see if it is working properly. First, if it compiles -- great. Then, if all possible actions are being performed to your satisfaction without getting any run time error-- fantastic. Plug on...next step.
Serthem Serthem

2016/5/8

#
That is not working, it would be more complicated than that.
danpost danpost

2016/5/8

#
Serthem wrote...
That is not working, it would be more complicated than that.
Okay. Please update us on your current code.
Simvarghese Simvarghese

2016/5/8

#
public class Button extends Actor
{
    // Variables that are active for the entire Button class
    private static int songPlaying = 0; // The number (1, 2, ...) of the song playing, or 0 if no song is playing
    private static boolean isPaused= true;
    private static GreenfootSound gfs;         // The song to play 

    // Instance variables that are active for each instance of a Button
    private String image;               // This button's image
    private String song;                // This button's song name, or blank if the button is not a song
    private int buttonType;             // 1, 2, 3, ... for a song button, -1 for play/pause, -2 for stop
    
    private boolean isMouseClicked;     // True if the mouse was clicked on this instance
    private GreenfootImage image1;
    private GreenfootImage image2;
    
    private String sound;
    /**
     * Button constructor called when a new instance of a Button is created.
     */
    
    public Button(String imageName, String songName, int typeOfButton)
    {
        image = imageName;
        setImage(image);
        song = songName;
        
        buttonType = typeOfButton;
        image1 = new GreenfootImage("ButtonPlaySmall.png");
        image2 = new GreenfootImage("ButtonPauseSmall.png");
        
        sound=songName;
        gfs = new GreenfootSound(sound);
    }
    
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        isMouseClicked = Greenfoot.mouseClicked(this);
        checkSongPlaying();
    }   
    
    /**
     * checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
     */
    
    private void checkSongPlaying()
    {
      if (isMouseClicked)
      {
            getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
      }
         
      if (isMouseClicked && buttonType < 0)
      {
            if (songPlaying == 0) //no song is playing
            {
                getWorld().showText("Hey! There's nothing playing!", 250, 300);
            }
            else //song is playing
            {
                getWorld().showText("Pausing song " + songPlaying, 100, 380);
                gfs.pause();
                if (getImage() == image1) 
                    {
                     setImage(image2);
                    }
                else
                    {
                     setImage(image1);
                    }
                    songPlaying = 0;
                
               
            }
            if (isPaused)
                { 
                    gfs.play();
                }
      }
So I have added "ifPaused" code to get the song to start playing again when it is paused, but it still doesn't resume from where the song left off, because of that gfs.play command, so it starts the song from the beginning again. It also doesn't allow me to pause or stop the song again. So: i have found a way to get the song to start playing again after it has been paused, but thats about it. Have not been able to get it to stop playing after that, or resume playing I also figured out that the way the private static int songPlaying = 0; private static boolean isPaused= true; private static GreenfootSound gfs; are ordered in the Button extend Actor is sensitive to the way the scenario works. I know thats a lot, but I guess what I am wondring is how to just get the song to resume from where it left off. or what i should change in the code to get it to not just gfs.play();
danpost danpost

2016/5/8

#
Simvarghese wrote...
I also figured out that the way the private static int songPlaying = 0; private static boolean isPaused= true; private static GreenfootSound gfs; are ordered in the Button extend Actor is sensitive to the way the scenario works.
There is no chance that the order of these lines could make any difference. Having the 'isPaused' field does no good unless you change its value when appropriate; that is, setting it to 'false' when the play button is clicked and setting it back to 'true' when the 'pause' button is clicked.
You need to login to post a reply.
1
2