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

2020/4/12

Using an array to create a certain method

Mattzh Mattzh

2020/4/12

#
public class Ball extends Actor
{
    private int imageNumber;
    private int newColor;
    String [] image;

    public Ball(String [] ballColor)
    {
        image=ballColor;
        

    }

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        wallBounce();

    }   

    public void wallBounce()
    {
        move(5);
        if(isAtEdge())
        {
            move(-10);
            turn(60+Greenfoot.getRandomNumber(181)); // 60 to 240 inclusive
            changeColor();

        }

    }

    public void changeColor()
    {
        //generate a new integer from 0-4 that will become new color
        newColor = Greenfoot.getRandomNumber(5);        

        while(newColor==imageNumber)//if new color is equal to old color
        {
            newColor = Greenfoot.getRandomNumber(5);  //regenerate new color
        }

        imageNumber=newColor; //update current color of ball
        setImage("button-"+imageNumber+".png");

    }

}
The method I am referring to in this question is changeColor. The purpose of it is that when the Ball actor hits the wall, it changes to one of the 7 other colors, and the while prevents it from changing into the image that it previously was. Instead of using newColor and imageNumber, how would I accomplish the same method using arrays? Any help would be appreciated.
danpost danpost

2020/4/12

#
If not required to use an array, the following should work:
public void changeColor()
{
    imageNumber = (imageNumber+1+Greenfoot.getRandomNumber(4))%5;
    setImage("button-"+imageNumber+".png");
}
This at least eliminates the use of newColor. Your code, and the above of mine, only allow for 5 colors. If using 8 colors, change my 4 and 5 in line 3 to 7 and 8, respectively.
Mattzh Mattzh

2020/4/12

#
Thanks for responding; the code I sent earlier may have been a bit confusing. Regarding this project, I am required to used an array. I was able to simplify my code up to a small extent.
public class Ball extends Actor
{
    String [] image;
    private int lengthArray;
    
    public Ball(String [] ballColor, int arrayLength)
    {
        image=ballColor;
        lengthArray = arrayLength;
        

    }

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        wallBounce();

    }   

    public void wallBounce()
    {
        move(5);
        if(isAtEdge())
        {
            move(-10);
            turn(60+Greenfoot.getRandomNumber(181)); // 60 to 240 inclusive
            changeColor();

        }

    }

    public void changeColor()
    {
        setImage(image[Greenfoot.getRandomNumber(lengthArray)]);

    }

}
As of now, I got the changeColor method to change the color of the Ball actor when it hits the wall. My goal (and what I am not sure of) is to add something using arrays to regenerate a new color if it is equal to the old color.
danpost danpost

2020/4/12

#
Mattzh wrote...
My goal (and what I am not sure of) is to add something using arrays to regenerate a new color if it is equal to the old color.
It would be quite similar to the expression I used above on line 3. It would, however, then refer to an index pointer to the array. You do not need to pass the length of the array along with the array. You can simply acquire its length by:
int arrayLength = ballColor.length;
Just add an int index pointer field to hold the element number of the current color.
You need to login to post a reply.