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

2013/9/2

Single Player Pong

1
2
RedCreeper RedCreeper

2013/9/4

#
I get this error after selecting single player.
java.lang.ClassCastException: java.util.ArrayList cannot be cast to greenfoot.Actor
	at Paddle.act(Paddle.java:65)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
The code that i have now is
if(Table.singlePlayer == true && returnPlayer() == 2) {
            if (!getWorld().getObjects(Ball.class).isEmpty())
            {
                // is ball in world  
                if (!getWorld().getObjects(Ball.class).isEmpty())  
                {  
                    // get reference to ball  
                    Actor ball = (Actor)getWorld().getObjects(Ball.class);  
                    // is ball on the half of the playfield this paddle is on  
                    if (Math.abs(ball.getX()-getX()) < 335)  
                    {  
                        // get vertical direction to move (dy = { -1, 0, 1 })  
                        int dy = (int)Math.signum((ball.getY()-getY())/3);  
                        // make move in direction determined (or remain in place)  
                        setLocation(getX(), getY()+5*dy);  
                    }  
                }  
            }
        }
danpost danpost

2013/9/4

#
In line 8, you declare the field 'ball' to hold an Actor object; yet, you are assigning a List object to it.
RedCreeper RedCreeper

2013/9/4

#
What would It be to fix it, nbtw that line is from the code u gave me earlier
danpost danpost

2013/9/4

#
Sorry, my bad! Did not finish it.
Actor ball = (Actor)getWorld().getObjects(Ball.class).get(0);
RedCreeper RedCreeper

2013/9/4

#
Thank you danpost. I have noticed that when the computer launches the ball the computer's paddle moves randomly what would the best way to fix this be
danpost danpost

2013/9/4

#
I believe, but not certain, that this code in the Paddle class (starting around line 59) is causing the movement you are talking about:
if(Table.singlePlayer == true && returnPlayer() == 2 && gotBall()) {
    int y = Greenfoot.getRandomNumber(500);
    if(y < 100) {
        y = y + Greenfoot.getRandomNumber(200);
    }
    else if(y > 400) {
        y = y - Greenfoot.getRandomNumber(200);
    }
    setLocation(getX(), y);
RedCreeper RedCreeper

2013/9/4

#
Also the ball can get stuck on the top and bottom of the paddle and then blounce in a random direction. Is there a way to stop the ball from getting stuck
RedCreeper RedCreeper

2013/9/4

#
What would make it so that the computer will choose a random location and then release the ball
danpost danpost

2013/9/4

#
RedCreeper wrote...
What would make it so that the computer will choose a random location and then release the ball
Something like this might work:
if (computer's paddle and has ball) // pseudo-code
{
  int y = Greenfoot.getRandomNumber(getWorld().getHeight()-200)+100;
  while (getY() != y)
  {
    setLocation(getX(), getY()+(int)Math.signum(y-getY()));
    Greenfoot.delay(2);
  }
  Greenfoot.delay(40);
  releaseBall();
}
danpost danpost

2013/9/4

#
You will probably need a double conditional to determine bouncing off a paddle. In the Ball class:
if (!getOneIntersectingObect(Paddle.class).isEmpty())
{
    if ((deltaX < 0 && getX() < 350) || (deltaX > 0 && getX() > 350) deltaX = -deltaX;
}
This says that if the ball is touching a paddle then if the ball is moving left and on the left side of the playfield, or if the ball is moving right and on the right side of the playfield, then bounce off the paddle. Once 'deltaX' is changed the condition cannot be true again until the ball makes it to the other side of the playfield.
RedCreeper RedCreeper

2013/9/4

#
Thank You danpost
You need to login to post a reply.
1
2