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

2013/5/24

Hi im trying to make the the game without adding a counter yet but i dont understand why Greenfoot can't find the release method of my paddle, help!!!

landry landry

2013/5/24

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Paddle here. * * @author (your name) * @version (a version number or a date) */ public class Paddle extends Animals { private Ball myBall; public void addedToWorld(World world) { newBall(); } /** * 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() { if (Greenfoot.isKeyDown("left")) { moveSideways(-9); // Add your action code here. } if (Greenfoot.isKeyDown("right")) { moveSideways(9); } if (haveBall() && Greenfoot.isKeyDown("space")) { releaseBall(); } } public void moveSideways(int dist) { setLocation (getX() + dist, getY()); if (myBall != null) { myBall.move (dist); } } public void newBall() { myBall = new Ball(1); getWorld().addObject (myBall, getX(), getY()-20); } public boolean haveBall() { return myBall != null; } public void releaseBall() { myBall.release(); myBall = null; } }
landry landry

2013/5/24

#
I meant i'm trying to make the breakout game
danpost danpost

2013/5/24

#
Look in your Ball class and see if you have a 'public void release()' method
landry landry

2013/5/24

#
of course i forgot to create one , help i don't know how to make 1 .
danpost danpost

2013/5/24

#
It would help to see what you have in your Ball class so far.
landry landry

2013/5/24

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Animals { private int deltaX; private int deltaY; private int count = 2; private boolean stuck = true; public Ball(int direction) { setRotation(direction); } /** * 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() { if (!stuck) { move(); if (atWorldEdge()) { turn(155); } makeSmoke(); checkOut(); } } public void makeSmoke() { getWorld().addObject( new Smoke(), getX() , getY() ); } public boolean atWorldtEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getWidth() - 20) return true; else return false; } private void checkOut() { if(getY() == getWorld().getHeight()-1) { ((Nature) getWorld()).ballIsOut(); getWorld().removeObject(this); } } public void move() { setLocation (getX() + deltaX,getY() + deltaY); checkPaddle(); checkWalls(); } private void checkWalls() { if (getX() ==0 || getX() == getWorld().getWidth()-1) { deltaX = -deltaX; } if (getY() == 0) { deltaY = -deltaY; } } private void checkPaddle() { Actor paddle = getOneIntersectingObject(Paddle.class); if(paddle != null) { deltaY = -deltaY; int offset = getX() - paddle.getX(); deltaX = deltaX + (offset/10); if (deltaX >7) { deltaX = 7; } if (deltaX < -7) { deltaX = -7; } } } public void move(int dist) { setLocation (getX() + dist, getY()); } } I haven't added the bricks yet
danpost danpost

2013/5/24

#
public void release()
{
    stuck = false;
}
landry landry

2013/5/24

#
thanks but now i get this , I so confused java.lang.NullPointerException at Nature.ballisOut(Nature.java:40) at Nature.prepare(Nature.java:36) at Nature.<init>(Nature.java:22) and this is my World's code : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Nature here. * * @author (your name) * @version (a version number or a date) */ public class Nature extends World { private Paddle paddle; private Counter counter; /** * Constructor for objects of class Nature. * */ public Nature() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 600, 1); setPaintOrder (Ball.class,Smoke.class); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { addPaddle(); addBall(); Counter counter = new Counter(); addObject(counter, 59, 585); counter.setLocation(58, 578); ballisOut(); } public void ballisOut() { paddle.newBall(); } public void addPaddle() { Paddle paddle = new Paddle(); addObject(paddle, getWidth() / 2, getHeight() - 40); paddle.setLocation(344, 531); } public void addBall() { int direction = Greenfoot.getRandomNumber(360); Ball ball = new Ball(direction); addObject(ball, 341, 501); } }
danpost danpost

2013/5/24

#
In the 'addPaddle' method, change the first line from: Paddle paddle = new Paddle(); to paddle = new Paddle(); If you declare a field in a method with the same name as one that is declared in the class, then when that name is used later in that method, it refers to the one in the method and then is discarded at the end of the method. So, you never really set the field declared in the class to anything (it remained 'null' -- hence, the NullPointerException).
landry landry

2013/5/24

#
Okay tnsx bro !! but now when the game starts i somehow get 3 balls on the screen , none of them moves , only 1 tries to generate the smoke but can't. I think it has something to do with : public void newBall() { myBall = new Ball(1); getWorld().addObject (myBall, getX(), getY()-20); }
danpost danpost

2013/5/24

#
Yes, that is part of the problem. You are creating a new ball in your Nature class as well (that is the another part). You should always avoid doing the same thing in more than one place. It is not so bad when you are removing objects, but when adding object you end up getting more than you asked for. You said you had 3 balls on the screen -- I do not know if removing one of the two 'adds' we already know about will completely fix the problem or not; and which one to remove is at your discretion. If I were coding a breakout game, I would probably put something like this in the world act method:
if (getObjects(Ball.class).isEmpty)  paddle.setBall(new Ball());
Then in the paddle class, write the 'setBall' method to save the ball object in a field (if the field has an object, then it is being held; if not, then the ball is loose).
private Ball ball;

public void setBall(Ball newBall)
{
    ball = newBall;
}
To release the ball, I would just change the field to 'null'.
private void releaseBall()
{
    ball = null;
}
In the paddle act method I would use something like:
if (ball != null) ball.setLocation(getX(), getY()-20);
That would be all that was needed to maintain one ball object in the world and control the paddle releasing of the ball.
landry landry

2013/5/26

#
I still have a problem in my world with the addBall method in my world , could you please help ? it says can't find variable Empty . import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Nature here. * * @author (your name) * @version (a version number or a date) */ public class Nature extends World { private Paddle paddle; //private Counter counter; /** * Constructor for objects of class Nature. * */ public Nature() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 600, 1); setPaintOrder (Ball.class,Smoke.class); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { addPaddle(); addBall(); Counter counter = new Counter(); addObject(counter, 59, 585); counter.setLocation(58, 578); ballisOut(); } public void ballisOut() { paddle.newBall(); } public void addPaddle() { paddle = new Paddle(); addObject(paddle, getWidth() / 2, getHeight() - 40); paddle.setLocation(344, 531); } public void addBall() { if (getObjects(Ball.class).isEmpty) { paddle.setBall(new Ball()); //int direction = Greenfoot.getRandomNumber(360); //Ball ball = new Ball(direction); //addObject(ball, 341, 501); } } }
You need to login to post a reply.