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

2012/4/26

Reflection

thefutureisnow thefutureisnow

2012/4/26

#
Hello world! I was wondering if there is an easier way to make things rebound off objects besides stating them by tens. You see, I'm creating a Pong game, and when I programmed the ball to be able to reflect, it ended up with 357 lines of code (including empty lines which I use to organize). If anyone knows a better way of doing this, please comment. Thanks!
danpost danpost

2012/4/26

#
I know you were asked to post your code, but I do not think they meant 357 lines of it! You could, however, give a sample of what you have, like a couple sets of your 'tens' and any previous and/or following code that may be relevant.
thefutureisnow thefutureisnow

2012/4/26

#
No, this is a different project, sorry for getting you confused. No the code I have works fine, but I was just wondering if there is an easier way of making reflections. But here's a sample of my code within my act method: if(getY() >= getWorld().getHeight() -5) { if(getRotation() <= 10 && getRotation() >=0) { setRotation(350); } if(getRotation() <= 20 && getRotation() >= 11) { setRotation(340); } if(getRotation() <= 30 && getRotation() >= 21) { setRotation(330); } if(getRotation() <= 40 && getRotation() >= 31) { setRotation(320); } if(getRotation() <= 50 && getRotation() >= 41) { setRotation(310); } if(getRotation() <= 60 && getRotation() >= 51) { setRotation(300); } if(getRotation() <= 70 && getRotation() >= 61) { setRotation(290); } if(getRotation() <= 80 && getRotation() >= 71) { setRotation(280); } } That's a sample.
danpost danpost

2012/4/26

#
Why not just say 'setRotation(360 - getRotation());'
thefutureisnow thefutureisnow

2012/4/26

#
Wow, never thought of that. Thanks!
danpost danpost

2012/4/26

#
Any time a pattern, like in the above code you provided, occurs, you should consider what options you may have to condense the code. Ask yourself if there might be a way to code it without any 'if's or 'switch'es. Almost always, you will find another way.
You need to login to post a reply.