arraylist - Java 2d game finding correct enemy for fighting -
i making simple 2d game in java , trying fighting work. going pokemon-style fighting, doing when press spacebar, checks if colliding enemy, finds enemy in arraylist, , executes fight method using enemy. works of time, can't seem find enemy in arraylist. code checking is:
if (space) { (rectangle collideable : enemy.ens) { if (intersects(playerr, collideable)) { system.out.println("colliding enemy!"); x = locx; y = locy; playerr.setlocation(x, y); (int = 0; < game.enemies.size(); i++) { if (enemy.ens.get(i).equals(collideable)) { system.out.println("can't find enemy fight"); system.out.println(game.enemies.get(i).getname()); fightquestion(game.enemies.get(i), i); break; } } break; } } }
enemy.ens
created when render each enemy. game.enemies
created when read in of enemy stats, , add each enemy arraylist. every enemy try fight, getting point prints out colliding, not fight part. ideas why happening fantastic!
edit code when game.enemies filled:
public static void loadenemies() { im = getimagemanager(); scanner qwe; try { qwe = new scanner(new file("enemystats.txt")); while (qwe.hasnextline()) { string name = qwe.nextline(); string origin = qwe.nextline(); string weapon = qwe.nextline(); string gear = qwe.nextline(); string spec = qwe.nextline(); int hp = qwe.nextint(); int att = qwe.nextint(); int def = qwe.nextint(); int randx = (int) (math.random()*(18*scale*tilesize)); //give random x coordinate int randy = (int) (math.random()*(18*scale*tilesize)); //give random y coordinate if(qwe.hasnextline()){ qwe.nextline(); } enemies.add(new enemy(randx,randy,im,name,origin,weapon,gear,spec,hp,att,def)); //adds enemy arraylist int randi = (int) (math.random()*enemies.size()); for(int = 0; < 4;i++){ //adds enemy arraylist rendered enemiestorend.add(enemies.get(randi) ); } } } catch (filenotfoundexception e) { e.printstacktrace(); } }
why execute statement:
system.out.println("can't find enemy fight");
for each iteration in second loop, regardless of checking condition first? , why second loop check:
i < game.enemies.size();
instead of:
i < enemy.ens.size();
Comments
Post a Comment