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

2013/12/7

How to reflect an object of another object?

UltimateCoder UltimateCoder

2013/12/7

#
I have this game where there is a ball, and when it hits the paddle, the ball should reflect of the paddle. Does anyone know the code for this? Here is my current code for the ball: { public void act() { move(3); atWorldEdge(); moveAround(); } public boolean atWorldEdge() { if (getX() <= 5 || getX() >= getWorld() . getWidth() -5) return true; if (getY() <= 5 || getY() >= getWorld() . getHeight() -5) return true; else return false; } private void moveAround() { move(5); if (atWorldEdge()) { turn(Greenfoot.getRandomNumber (30) - 30); } } }
Yogibaer Yogibaer

2013/12/7

#
The general code how to reflect the ball your code shows already. Instead of 'worldEdge' now you have to check if the ball is near or hits the paddle. The method to do this is
getOneObjectAtOffset(int dx,int dy,Class.class)
The class is your paddle-class. To be able to hit the paddle it`s necessary one or two paddles are in the world. I hope this hints lead you to the solution. kind regards Yogibaer
You need to login to post a reply.