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

2016/12/19

Brick breaker help

gowerc gowerc

2016/12/19

#
Just need someone to help me figure out whats wrong with this code the Ball keeps getting stuck in the top right corner int xBall, yBall; int xMoveVal = 5; int yMoveVal = 5; /** * 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() { xBall = getX(); yBall = getY(); setLocation(xBall + xMoveVal, yBall - yMoveVal); if (yBall < 10) { yMoveVal*=-1; } if (xBall > getWorld().getWidth()) { xMoveVal*=-1; } if (yBall > getWorld().getHeight()) { yMoveVal*=-1; } if (xBall < 0) { xMoveVal*=-1; } if (isTouching(Paddle.class)) { xMoveVal*=-1; } } }
danpost danpost

2016/12/19

#
The x and y values you are checking are those before you move the actor with 'setLocation'. Try re-assigning them after setting the location of the actor. Also, unless you are using an unbounded world (using a world constructor with a 'false' parameter after the three int values), the location of the actor will never have coordinate values of those you are checking for (less than zero or greater than a dimension of the world). Adjust the coordinate values you are checking for to be something within world bounds (zero to one less than a dimension of the world). You may want to be consistent and use ten and ten less than any dimension of the world (since you are already checking for '10' along the lower end of one dimension).
gowerc gowerc

2016/12/19

#
Thanks it works now
You need to login to post a reply.