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

2021/4/26

I can't seem to fix the error I get for paddle 2, which is cannot find symbol. I understand how I got the error but do you guys know how to fix it?

Dannythechanp Dannythechanp

2021/4/26

#
 private void prepare()
    {
        for(int i = 0; i<numberBalls; i++)
        {
            int currentIndex = Greenfoot.getRandomNumber(arrayLength);
            Ball ball = new Ball(ballColor, arrayLength, currentIndex);

            addObject(ball, Greenfoot.getRandomNumber(801),Greenfoot.getRandomNumber(601));
            ball.setImage(ballColor[currentIndex]);
            
            ball.setRotation(Greenfoot.getRandomNumber(360));
        }
     
             int currentIndx = Greenfoot.getRandomNumber(arrayLengthx);
           
             Paddle paddle = new Paddle(paddleColor,arrayLengthx,paddle, paddle2);
             addObject(paddle,40,300);
            
             Paddle paddle2 = new Paddle(paddleColor,arrayLengthx,paddle, paddle2);
             addObject(paddle2,760,300);
             paddle.setImage(paddleColor[currentIndx]);
             
             paddle2.setImage(paddleColor[currentIndx]);
             
            
        }
Dannythechanp Dannythechanp

2021/4/26

#
This is a random color generated pong game, with the both the paddles and the ball having a randomly generated color at the start of the game. I used arrays for both the ball and the paddles.
danpost danpost

2021/4/26

#
Dannythechanp wrote...
This is a random color generated pong game, with the both the paddles and the ball having a randomly generated color at the start of the game. I used arrays for both the ball and the paddles.
Show entire class codes and indicate what cannot be found on what line.
Dannythechanp Dannythechanp

2021/4/26

#
MyWorld class line 111 says symbol paddle 2 cannot be found
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
//Below is the initialization of a GreenfootImage Array named "ballColor" with length of 4
GreenfootImage [] ballColor = new GreenfootImage [4];

//store the legnth of ballColor in arrayLength
private int arrayLength = ballColor.length;


private int numberBalls = 1;
//size of the balls
private int size = 20;

GreenfootImage [] paddleColor = new GreenfootImage [4];

private int arrayLengthx = paddleColor.length;


private int recwidth = 25;
private int reclength = 120;





    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        fillArray();
        prepare();
        
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);
        int b = Greenfoot.getRandomNumber(256);
        int a = Greenfoot.getRandomNumber(51);
 //creates a variable color named backColor
  Color backColor = new Color(r,g,b,a); 
  GreenfootImage background = getBackground();
  //sets the color "backColor" to be used for fill
  background.setColor(backColor);
  background.fill(); 
  
    }
    public void act()
    {
      addBall();  
    }
private void fillArray()
{
for(int j = 0; j<arrayLength; j++)
{
    GreenfootImage circlex = new GreenfootImage(size,size);
  int r = Greenfoot.getRandomNumber(256);
  int g = Greenfoot.getRandomNumber(256);
  int b = Greenfoot.getRandomNumber(256);
  int a = 100+Greenfoot.getRandomNumber(51);
 //creates a variable color named circleColor
  Color circleColor = new Color(r,g,b,a); 
  circlex.setColor(circleColor);
  circlex.fillOval(0,0,size,size);
 
  ballColor[j] =  circlex;
}
for(int p = 0; p<arrayLength; p++)
{
     GreenfootImage rectanglex = new GreenfootImage(recwidth,reclength);
  int r = Greenfoot.getRandomNumber(256);
  int g = Greenfoot.getRandomNumber(256);
  int b = Greenfoot.getRandomNumber(256);
  int a = 100+Greenfoot.getRandomNumber(51);
 //creates a variable color named rectangleColor
  Color rectangleColor = new Color(r,g,b,a); 
rectanglex.setColor(rectangleColor);
  rectanglex.fillRect(0,0,recwidth,reclength);
 
  paddleColor[p] =  rectanglex;
  
  

}
}

    private void prepare()
    {
        for(int i = 0; i<numberBalls; i++)
        {
            int currentIndex = Greenfoot.getRandomNumber(arrayLength);
            Ball ball = new Ball(ballColor, arrayLength, currentIndex);

            addObject(ball, Greenfoot.getRandomNumber(801),Greenfoot.getRandomNumber(601));
            ball.setImage(ballColor[currentIndex]);
            
            ball.setRotation(Greenfoot.getRandomNumber(360));
        }
     
             int currentIndx = Greenfoot.getRandomNumber(arrayLengthx);
           
             Paddle paddle = new Paddle(paddleColor,arrayLengthx,paddle,paddle2);
             addObject(paddle,40,300);
            
             Paddle paddle2 = new Paddle(paddleColor,arrayLengthx,paddle,paddle2);
             addObject(paddle2,760,300);
             paddle.setImage(paddleColor[currentIndx]);
             
             paddle2.setImage(paddleColor[currentIndx]);
             
            
        }
       
   
   
        
    

private void addBall()
{
    if(Greenfoot.mouseClicked(null))
    {
       //collect mouse information and store in "mouse" variable
        MouseInfo mouse  = Greenfoot.getMouseInfo();
        
        int currentIndx = Greenfoot.getRandomNumber(arrayLength);
        Ball ballx = new Ball(ballColor,arrayLength, currentIndx); 
        addObject(ballx, mouse.getX(),mouse.getY());
        
        ballx.setImage(ballColor[currentIndx]);
        ballx.setRotation(Greenfoot.getRandomNumber(360));
    }
Dannythechanp Dannythechanp

2021/4/26

#
Paddle Class
/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Paddle extends Actor
{
    private int arrayLengthx;
    static int PLAYER1SPEED = 5;
    static int PLAYER2RIGHTSPEED = 5;           // The speed both players move their paddles with.
    static int PLAYER1_REGAIN_SPEED = 5;
    static int PLAYER2RIGHT_REGAIN_SPEED = 5;    // Should be equal to the speed of the players, regains the speed of the players.
    
    Paddle player1;
Paddle player2;
    
    public Paddle(GreenfootImage [] paddleColor, int arrayLengthx, Paddle paddle, Paddle paddle2 )//the Ball class constructor
    {
        paddle = player1;
      paddle2 = player2;
    }
    /**
     * Act - do whatever the Paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        player1Move();
    }    
    public void player1()
    {
    }
    public void player1Move()
    {
       if (Greenfoot.isKeyDown("w"))
        {   
            setLocation(getX(), getY() - PLAYER1SPEED);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + PLAYER1SPEED);
        }
    }
}
Dannythechanp Dannythechanp

2021/4/26

#
sorry line 109, my bad
danpost danpost

2021/4/26

#
Dannythechanp wrote...
sorry line 109, my bad
"paddle" is not defined until after the object is created. You cannot pass it into its own constructor.
Dannythechanp Dannythechanp

2021/4/26

#
I understand that but the issue is I have to somehow export paddle and paddle 2 to the paddle class, with the only way I see being to pass it through its own constructor. Since paddle2 isn't created until after paddle, when the newly defined paddle wants to export it, it cannot. I know that I could probably pass it through the ball constructor instead, after I already define both paddle and paddle2, and have all of the code in the ball class but I think it would get pretty messy. Do you have any suggestions?
danpost danpost

2021/4/27

#
I do not understand why a paddle would need to know anything but what keys to react to:
import greenfoot.*;

public class Paddle extends Actor
{
    int speed = 5;
    String upKey, downKey;
    
    public Paddle(String upKey, String downKey)
    {
        this.upKey = upKey;
        this.downKey = downKey;
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown(upKey))
        {
            setLocation(getX(), getY()-speed);
        }
        if (Greenfoot.isKeyDown(downKey))
        {
            setLocation(getX(), getY()+speed);
        }
    }
}
creating paddles with:
Paddle player1 = new Paddle("w", "s");
Paddle player2 = new Paddle("up", "down");
You need to login to post a reply.