Assigning objects to variables in java -
hi im making game of breakout , im having problem getting ball bounce off paddle or bricks.
heres code create ball bounce off bricks , paddle.
private void checkforhit(){ gobject collider = getelementat( ballx, ballspeed); if(collider == paddle){ ballspeed = -ballspeed; } }
i don't know why not working, small.
update
when set set if(collider.equals(paddle))
i java.lang.nullpointerexception error when run game.
it's because trying utilize == signs on objects. normal syntax when comparing objects codechimp explained:
<objectname>.equals( <otherobject> )
you have create sure objects using (if made them yourself) has equal method utilize , compares right fields values etc. other objects. it's of import part of oop larn how objects work together.
you might wanna take @ keyword: instanceof while @ it.
also if looking compare 2 different field values, both fx integer values in each of 2 classes have. create getter method field values , simple compare specific values.
if ( collider.getspecificvalue() == paddle.getspecificvalue() ) { }
java object breakout
No comments:
Post a Comment