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

2018/5/1

Counter question

toba4366 toba4366

2018/5/1

#
I have a counter that I use for an array that goes up by one(until it hits seven then it restarts) and keeps track of it but the problem is that it goes in order I want it to be random and then once it has been used that it won't be reused until it restarts(I have it hardcoded so that when it hit the end) is there anybody who can help me?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class soundButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class soundButton extends Actor
{
    private String[] animalSounds =
    {"dog3.mp3", "lion.wav", "pig.wav", "platypus.wav", "cow.wav", "cat2.mp3", "Lion#2.mp3"};
    public static int count = 0; 
    private String sound;
    
    /**
     * Act - do whatever the soundButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)){
            play();
        }
    }   
    
    public soundButton(){
        GreenfootImage image=getImage();
        image.scale(image.getWidth()-1925, image.getHeight()-1925);
        setImage(image);
    }
    
    
    public void play(){
        if (count == 7){
            count = 0;
        }   
        Greenfoot.playSound(animalSounds[count]);
        count = count + 1;
   
    }
    
}
toba4366 toba4366

2018/5/1

#
I also need the numbers associated with the array sounds to stay the same because that is needed in the later code
public class platypus extends Actor
{
    /**
     * Act - do whatever the platypus wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 4)){
            Greenfoot.playSound("goodjob.wav");
        } 
        
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 1 )){
            Greenfoot.playSound("tryAgain.wav");
        }
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 2 )){
            Greenfoot.playSound("tryAgain.wav");
        }
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 3 )){
            Greenfoot.playSound("tryAgain.wav");
        }
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 5 )){
            Greenfoot.playSound("tryAgain.wav");
        }
        if (Greenfoot.mouseClicked(this) && (soundButton.count == 6 )){
            Greenfoot.playSound("tryAgain.wav");
        }
    } 
}
danpost danpost

2018/5/1

#
Upon restart, you can shuffle the array with:
java.util.Collections.shuffle(java.util.Arrays.asList(animalSounds));
You need to login to post a reply.