arrays - Collision detection and reaction java -
i writing small game 20 balloons created on screen , mouse released on them expands them. supposed 'pop' when 1 balloon touches another, @ present when click balloon pops random 1 , throws 'array index out of bounds' exception. i've racked brain figure out why code isn't working can't it. here's of code causing problem:
import comp102.*; import java.util.*; import java.awt.color; public class balloongame implements uibuttonlistener, uimouselistener{ // fields private final int numballoons = 20; private int currentscore; // score current game private int highscore = 0; // highest score in games far. private int totalpopped = 0; balloon balloons[] = new balloon[numballoons]; // constructor /** set gui, start new game. */ public balloongame(){ ui.setmouselistener(this); ui.addbutton("new game", this); ui.addbutton("lock score", this); this.newgame(); } // gui methods respond buttons , mouse /** respond button presses, start new game , end current game */ public void buttonperformed(string cmd){ if (cmd.equals("new game")) { this.newgame(); } else if (cmd.equals("lock score")) { this.endgame(); } } /** respond mouse released main action of game*/ public void mouseperformed(string action, double x, double y) { if (action.equals("released")) { this.doaction(x, y); } } /** start game: clear graphics pane initialise score information make new set of balloons @ random positions */ public void newgame(){ ui.cleargraphics(); this.currentscore = 0; this.totalpopped = 0; (int = 0; < this.balloons.length; i++) { this.balloons[i] = new balloon(50 + math.random()*400, 50 + math.random()*400); this.balloons[i].draw(); } ui.printmessage("new game: click on balloon. high score = "+this.highscore); } /** main game action. find balloon @ (x,y) if any, expand check whether touching balloon, if so, update totalpopped, pop both balloons, , remove them list recalculate score. if there no balloons left, end game. */ public void doaction(double x, double y) { (int = 0; < this.balloons.length; i++) { if (this.balloons[i].on(x, y) && !this.balloons[i].ispopped()) { this.balloons[i].expand(); } (int j = 1; j <this.balloons.length; j++) { if (this.balloons[i].istouching(this.balloons[j]) && this.balloons[j] != null) { this.totalpopped +=2; this.balloons[i].pop(); this.balloons[j].pop(); this.balloons[i] = null; this.balloons[j] = null; } } } this.calculatescore(); if (totalpopped == numballoons) { this.endgame(); } } /** find balloon point (x, y) on. * returns null if point not on balloon*/ public balloon findballoon(double x, double y){ return null; } /** find , return balloon touching balloon * returns null if no such balloon. */ public balloon findtouching(balloon balloon){ return null; } /** calculate score: sum of sizes of current ballons, minus total of popped balloons (totalpopped). report score message */ public void calculatescore(){ (balloon b: balloons) { this.currentscore += b.size(); } if (currentscore >= highscore) { this.highscore = this.currentscore; } ui.printmessage("score = "+this.currentscore+" high score = "+this.highscore); } /** returns true if balloons have been popped, * returns false if of balloons not popped */ public boolean allpopped(){ (balloon b : this.balloons){ if (!b.ispopped()){ return false; } } return true; } /** end current game. record the score new high score if better print message clear list of balloons (so player can't keep playing) */ public void endgame(){ this.highscore = this.currentscore; ui.println("high score = " + this.highscore); arrays.fill(balloons, null); } // main public static void main(string[] arguments){ balloongame ob = new balloongame(); } } uses balloon class also:
import comp102.*; import java.util.*; import java.awt.color; import java.io.*; /** represents balloon can grow until pops. balloon can whether particular point on it, , whether touching balloon. can return size. once has popped, no point on it, , can't touch balloon. also, size reported negative value. */ public class balloon{ // fields private double radius = 10; private double centerx, centery; private color color; private boolean popped = false; // constructors /** construct new balloon object. parameters coordinates of center of balloon not draw balloon yet. */ public balloon(double x, double y){ this.centerx = x; this.centery = y; this.color = color.gethsbcolor((float)math.random(), 1.0f, 1.0f); } public void draw(){ ui.setcolor(color); ui.filloval(centerx-radius, centery-radius, radius*2, radius*2); if (!this.popped){ ui.setcolor(color.black); ui.drawoval(centerx-radius, centery-radius, radius*2, radius*2); } } /** make balloon larger random amount between 4 , 10*/ public void expand(){ if (! this.popped){ this.radius = this.radius + (math.random()*6 + 4); this.draw(); } } /** pop balloon (changes colour gray, draws, , pauses briefly)*/ public void pop(){ this.color = color.lightgray; this.popped = true; this.draw(); ui.sleep(20); } /** returns true if balloon has been popped */ public boolean ispopped(){ return this.popped; } /** returns true if point (x,y) on balloon, , false otherwise */ public boolean on(double x, double y){ if (popped) return false; double dx = this.centerx - x; double dy = this.centery - y; return ((dx*dx + dy*dy) < (this.radius * this.radius)); } /** returns true if balloon touching other balloon, , false otherwise * returns false if either balloon popped. */ public boolean istouching(balloon other){ if (this.popped || other.popped) return false; double dx = other.centerx - this.centerx; double dy = other.centery - this.centery; double dist = other.radius + this.radius; return (math.hypot(dx,dy) < dist); } /** calculates , returns area of balloon * returns in "centi-pixels" (ie, number of pixels/100) * keep them in reasonable range. * returns negative size if popped.*/ public int size(){ int s = (int) ((this.radius * this.radius * math.pi)/100); if (popped) { s = 0 - s; } return s; } }
is right?
(int j = 1; j <this.balloons.length; j++) { doesn't allow , j equal, end asking whether balloon touching itself? mean j = + 1 ?
if getting null pointer exceptions, debugger , step through until see where. guess visiting array items have been popped, , hence null.
if (this.balloons[i].istouching(this.balloons[j]) && this.balloons[j] != null) you testing this.balloons[j] null after using it. i'd put null checks before trying work each item.
Comments
Post a Comment