求大牛解答 真心感谢 两个类型是一样的啊 添加不进去急死了
this.little1.remove(j);
j--;//避免下标越界
你这个代码贴的 我都看不出来 既然报错了,
1.看看什么错误,贴出来错误
2.你的l[i].shot()类型和你enemyBullets的类型是不是一致?
看看shot方法,的debug追踪下
我感觉问题出在remove上,一定是越界了。
shot方法返回的是一个集合,集合里面的元素是Bullet1类型,而你的enemyBullets集合里的泛型不知道是什么类型(在你的代码里没看到),如果不是Bullet1类型的话就会报错(因为当你调用addAll方法是,是把shot方法返回的集合里的元素加到enemyBullets集合里去,如果这两个集合里的元素的泛型类型不一致的话就会报错),希望对你有帮助
package mygame;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
public class aw extends Frame {
public int esize=9;
public int num=0;
public little[] l;
private static final long serialVersionUID = 1L;
private int GAME_WIDTH = 900;
private int GAME_HEIGHT = 672;
private BufferedImage offScreenImage;
private hero hero1=new hero(new Floatpoint(50.0F, 110.0F));
private float mapposition=0.0f;
public int speed=2;
public boolean up=false;
public boolean down=false;
public boolean right=false;
public boolean left=false;
public boolean rightup=false;
public boolean leftdown=false;
public boolean rightdown=false;
public boolean leftup=false;
public static boolean right1=true;
public static boolean left1=false;
public boolean jump=false;
public static boolean jumpfinshed=true;
public boolean jumpcontrol;
public int con=900;
public int deadline;
List<bullet1> heroBullets;
List<bullet1> enemyBullets;
public grass grass1=new grass(new Floatpoint(738.0F,110.0F));
public grass grass2=new grass(new Floatpoint(1026.0F,110.0F));
int finishline;
float firstline;
public tank t=new tank(new Floatpoint(600,110));
int temp=0;
int shottemp=0;
@Override
public void update(Graphics g) {
int i;
//1.寰楀埌缂撳啿鍥惧儚
if(this.offScreenImage == null) {
this.offScreenImage = (BufferedImage) this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
//2.寰楀埌缂撳啿鍥惧儚鐨勭敾绗�
Graphics2D gOffScreen = (Graphics2D) this.offScreenImage.getGraphics();
//3.缁樺埗缂撳啿鍥惧儚
Color c = gOffScreen.getColor();
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
g.drawImage(offScreenImage, 0, 0, null);
gOffScreen.drawImage(imgs.BACKGROUND01, 0, 0, (int) (GAME_WIDTH*3), GAME_HEIGHT*3,(int)(mapposition),0,con, GAME_HEIGHT,null);
for(i=0;i<esize;i++)
{
this.l[i].drawit(gOffScreen, mapposition,temp);
}
this.grass1.drawgrass(gOffScreen,hero1.position,mapposition);
this.grass2.drawgrass(gOffScreen,hero1.position,mapposition);
this.hero1.drawPlayer( gOffScreen, mapposition);
this.t.drawgrass(gOffScreen, mapposition);
for(int j=0;j<this.heroBullets.size();j++)
{ this.heroBullets.get(j).drawit1(gOffScreen, mapposition);}
for(int k=0;k<this.enemyBullets.size();k++)
{ this.enemyBullets.get(k).drawit1(gOffScreen, mapposition);}
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
}
public void movecontrol()
{ check1();
if(jumpfinshed==false)
{ if(jump)
{if(right1)
{hero1.direction=51;
if(right)
{hero1.position.x+=speed/2;}
if(left)
{hero1.position.x-=speed/2;}}
else if(left1)
{hero1.direction=61;
if(right)
{hero1.position.x+=speed/2;}
if(left)
{hero1.position.x-=speed/2;}}
}}
else if(rightup)
{hero1.direction=5;hero1.position.x+=speed;}
else if(rightdown)
{hero1.direction=8;hero1.position.x+=speed;}
else if(leftup)
{hero1.direction=6;hero1.position.x-=speed;}
else if(leftdown)
{hero1.direction=7;hero1.position.x-=speed;}
else if(up)
{if(right1)
{hero1.direction=1;}
if(left1)
{hero1.direction=11;}
}
else if(down)
{if(right1)
{hero1.direction=14;System.out.println(hero1.position.x);System.out.println(mapposition);}
if(left1)
{hero1.direction=12;}}
else if(right)
{ if(jumpfinshed)
{hero1.direction=4;}
hero1.position.x+=speed;}
else if(left)
{ if(jumpfinshed)
{hero1.direction=2;}
hero1.position.x-=speed;}
else if(right1)
{hero1.direction=22;}
else if(left1)
{hero1.direction=21;}
}
public void refresh()
{ int i=0,j;
little[] a={new little(new Floatpoint(300,204)),new little(new Floatpoint(590,204)),new little(new Floatpoint(650,110)),new little(new Floatpoint(931,110)),
new little(new Floatpoint(1286,110)),new little(new Floatpoint(2018,110)),new little(new Floatpoint(1476,80)),new little(new Floatpoint(1436,204)),new little(new Floatpoint(100,110))};
this.l=a;
}
public void launchFrame() {
refresh();
addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=true;left1=true;right1=false; break;
case KeyEvent.VK_UP : up= true;break;
case KeyEvent.VK_DOWN :down = true; break;
case KeyEvent.VK_RIGHT : right = true;right1=true;left1=false;break;
case KeyEvent.VK_Q: jumpcontrol=true;jumpfinshed=false;break;
case KeyEvent.VK_W : hero1.shot=true; break;
}
if(up==false&&down==true&left==true&&right==false) {
leftdown=true;
}
else if(up==true&&down==false&left==false&&right==true) {
rightup=true;
}
else if(up==true&&down==false&left==true&&right==false) {
leftup=true;
}
else if(up==false&&down==true&left==false&&right==true) {
rightdown=true;
}
}
public void keyReleased(KeyEvent e) {
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=false; break;
case KeyEvent.VK_UP : up= false;break;
case KeyEvent.VK_DOWN :down = false; break;
case KeyEvent.VK_RIGHT : right = false;break;
case KeyEvent.VK_Q:jumpcontrol=false ;break;
case KeyEvent.VK_W : hero1.shot=false; break;
}
if(leftup=true)
{leftdown=false;leftup=false;}
else if(up=false)
{leftup=false;rightup=false;}
if(rightup=true)
{rightdown=false;rightup=false;}
else if(up=false)
{leftup=false;rightup=false;}
if(up==false&&down==false&left==false&&right==false) {
leftdown=false;leftup=false;rightup=false;rightdown=false;
}
}
public void keyTyped(KeyEvent e)
{
}});
this.setBounds(100, 100, GAME_WIDTH, GAME_HEIGHT);
this.setBackground(Color.GREEN);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
this.setTitle("TankWar");
this.setResizable(false);
this.setVisible(true);
new Thread(new PaintThread()).start();
new Thread(new movepaint()).start();
}
class movepaint implements Runnable {
@Override
public void run() {
// TODO 自动生成的方法存根
while(true)
{
temp+=2;
while(temp==12)
{temp=0;}
heroBullets=hero1.shotcontrol();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();
enemyShotControl();
controllittle();
t.move(mapposition);
movecontrol();
map();
if(jump=true)
{jumpf();}
moveforward();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void enemyshot(Graphics2D g, float s)
{ int i=120,j;
if(++i>=120)
{ for(j=0;;j++)
{}
}
else
{i=0;}
}
public void jumpf()
{ if(jumpfinshed==false)
{if(jumpcontrol==false&&jumpfinshed==true)
{ firstline=hero1.position.y;}}
if(hero.up==false)
{if(hero1.position.y>=finishline&&hero.down)
{jumpfinshed=true;jump=false;System.out.println(finishline);}}}
public void moveforward()
{ if(hero1.position.x>=mapposition+GAME_WIDTH/5&&(hero1.direction==4||hero1.direction==5||hero1.direction==8))
{mapposition+=speed; con=(int)(GAME_WIDTH+mapposition);}
if(hero1.position.x>=mapposition+GAME_WIDTH/5&&hero1.direction==51)
{mapposition+=5*speed; con=(int)(GAME_WIDTH+mapposition);}
}
public void check()
{ int x;
x=hero1.direction;
if(hero1.position.y<finishline&&jumpfinshed)
{hero1.direction=99;}
else if(hero1.position.y==finishline&&jumpfinshed)
{hero1.direction=x;}}
public void map()
{ if(grass1.ongrass(hero1)==false)
{
if(hero1.position.x<868&&hero1.position.x>740&&jumpfinshed==false)
{ finishline=500;}
else if(hero1.position.x<868&&hero1.position.x>740&&jumpfinshed==true)
{ hero1.direction=99;}}
else {hero1.position.x++;}
if(grass2.ongrass(hero1)==false)
{
if(hero1.position.x<1160&&hero1.position.x>1022&&jumpfinshed==false)
{ finishline=500;}
else if(hero1.position.x<1160&&hero1.position.x>1022&&jumpfinshed==true)
{ hero1.direction=99;}}
else {hero1.position.x++;}
if(hero1.position.x<=1345)
{finishline=110;check();}
else if(hero1.position.x<=1381)
{ if(hero1.position.y<82)
{finishline=80;check();}
else {finishline=110;}}
else if(hero1.position.x<=1411)
{ if(hero1.position.y<82)
{finishline=80;check();}
else if(hero1.position.y<112)
{finishline=110;check();}
else{finishline=204;check();}}
else if(hero1.position.x<=1475)
{if(hero1.position.y<82)
{finishline=80;check();}
else {finishline=204;check();}}
else if(hero1.position.x<=1541)
{if(hero1.position.y<82)
{finishline=80;check();}
else {finishline=155;check();}}
else if(hero1.position.x<=1573)
{finishline=80;
check(); }
else if(hero1.position.x<=1701)
{if(hero1.position.y {finishline=80;check();}
else {finishline=140;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else if(hero1.position.y {finishline=140;check();}
else{finishline=204;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else {finishline=204;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else if(hero1.position.y {finishline=110;check();}
else{finishline=204;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=110;check();}
else{finishline=204;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=110;check();}
else{finishline=170;check();}}
else if(hero1.position.x {finishline=110;
check();}
else if(hero1.position.x {if(hero1.position.y {finishline=110;check();}
else{finishline=170;check();}
}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else if(hero1.position.y {finishline=110;check();}
else{finishline=170;check();}}
else if(hero1.position.x {finishline=80;check();}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else{finishline=155;check();}}
else if(hero1.position.x {finishline=80;check();}
else if(hero1.position.x {if(hero1.position.y {finishline=80;check();}
else{finishline=140;check();}}
else if(hero1.position.x {finishline=140;check();}
else if(hero1.position.x {if(hero1.position.y {finishline=110;check();}
else{finishline=140;check();}}
else if(hero1.position.x {finishline=110;check();}
else if(hero1.position.x { if(jumpfinshed)
{hero1.direction=99;}}
else if(hero1.position.x {if(hero1.position.y {finishline=140;check();}
else{finishline=204;check();}}
else if(hero1.position.x {if(hero1.position.y {finishline=140;check();}
else{finishline=170;check();}}
else if(hero1.position.x {finishline=170;check();}
else if(hero1.position.x {if(hero1.position.x110)
{
{hero1.direction=99;}}
else if(hero1.position.x<=2464&&hero1.position.y<110)
{finishline=110;check();
}}
else if(hero1.position.x<=2498)
{ if(hero1.position.x<=2498&&jumpfinshed&&hero1.position.y>110)
{
finishline=204;check();}
else if(hero1.position.x<=2498&&hero1.position.y<110&&hero1.position.y>80)
{finishline=110;check();}
else if(hero1.position.x<=2498&&hero1.position.y<80)
{finishline=80;check();}}
else if(hero1.position.x<=2531)
{ if(hero1.position.x<=2531&&hero1.position.y<80)
{finishline=80;check();}
if(hero1.position.x<=2531&&hero1.position.y>80)
{finishline=155;check();}}
else if(hero1.position.x<=2558)
{ if(jumpfinshed)
{hero1.direction=99;}}
else if(hero1.position.x<=2595)
{ finishline=110;check();}
else if(hero1.position.x<=2627)
{if(hero1.position.y<112)
{finishline=110;check();}
else{finishline=140;check();}}
else if(hero1.position.x<=2690)
{ finishline=140;check();}
else if(hero1.position.x<=2754)
{if(hero1.position.y {finishline=140;check();}
else{finishline=204;check();}}
else if(hero1.position.x { finishline=204;check();}
else if(hero1.position.x { if(jumpfinshed)
{hero1.direction=99;}}
else if(hero1.position.x { finishline=170;}
else if(hero1.position.x { if(jumpfinshed)
{hero1.direction=99;}}
else if(hero1.position.x { finishline=140;check();}
else if(hero1.position.x { if(hero1.position.y {finishline=110;check();}
else {finishline=204;check();}
}
else if(hero1.position.x { if(hero1.position.y {finishline=110;check();}
else if(hero1.position.y112)
{finishline=155;check();System.out.println(finishline);}
else if(hero1.position.y157)
{finishline=204;check();}
}
else if(hero1.position.x<=3166)
{ if(hero1.position.y<142)
{finishline=140;check();}
else {finishline=204;check();}}
else if(hero1.position.x<=3204)
{if(hero1.position.y<172)
{finishline=170;check();}
else {finishline=204;check();}
}
check1();
}
public void check1()
{ if((hero1.position.y>finishline+3&&jumpfinshed))
{hero1.direction=99;}}
public void controllittle()
{ int i;
for(i=0;i<esize;i++)
{
if(hero1.position.x<l[i].location.x)
{ if (hero1.position.y==l[i].location.y)
{l[i].direction=2;}
else if(hero1.position.y<l[i].location.y)
{l[i].direction=6;}
else {l[i].direction=7;}}
else if(hero1.position.x>l[i].location.y)
{ if (hero1.position.y==l[i].location.y)
{l[i].direction=4;}
else if(hero1.position.y<l[i].location.y)
{l[i].direction=5;}
else {l[i].direction=8;}
}}
}
private void enemyShotControl()
{
for (int i = 0; i < this.esize; i++)
if ((this.hero1.position.x > (this.l[i].location.x -100)) &&
(this.hero1.position.x < (this.l[i].location.x +100)))
{
this.enemyBullets.addAll(l[i].shot());
l[i].shot = true;
}
else {
l[i].shot = false;
}
}
public static void main(String[] args) {
aw tc = new aw();
tc.launchFrame();
}
}
package mygame;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import mygame.aw.movepaint;
public class little extends Floatpoint {
public Floatpoint location;
private int t=1;
public boolean alive;
public float size = 3.0F;
public int direction;
int temp=0;
public little(Floatpoint point)
{this.location=point; Thread ta=new Thread();
ta.start();}
boolean we=true;
public List<bullet1>little1=new ArrayList<bullet1>();
public boolean shot=true;
public boolean first=true;
public void drawit(Graphics2D g,float mapposition,int x)
{ if(direction==6)
{t=1;g.drawImage(imgs.ENEMYSHOT3,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
else if(direction==2)
{t=2;g.drawImage(imgs.ENEMYSHOT3,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
else if(direction==7)
{t=2;g.drawImage(imgs.ENEMYSHOT3,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
else if(direction==4)
{t=2;g.drawImage(imgs.ENEMYSHOT1,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
else if(direction==8)
{t=1;g.drawImage(imgs.ENEMYSHOT1,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
else if(direction==5)
{t=3;g.drawImage(imgs.ENEMYSHOT1,(int) ((this.location.x-mapposition)*3),(int)(this.location.y)*3-100,(int) (this.location.x-mapposition)*3+86,(int)this.location.y*3,86*(t-1),0,86*t, 100, null);}
}
public List<bullet1> shot()
{
if(this.shot&&++temp%1200==0)
{this.little1.add(new bullet1(new Floatpoint(this.location.x,this.location.y-22)));}
{for(int j=0;j<this.little1.size();j++)
{ this.little1.get(j).move(this.direction);
}}
for(int j=0;j<this.little1.size();j++)
{
if(this.little1.get(j).position.y>=204||this.little1.get(j).position.y<0)
{this.little1.remove(j);j--;}
}
return little1;}
}
package mygame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
public class bullet1 {
public static Floatpoint position;
public static boolean isAlive = true;
private int direction ;
public int type;
public float size = 3.0F;
public float v=(float) 8.5;
private float speedx = 2F;
private float speedy = 2F;
public static int energy = 50;
public boolean first=true;
public bullet1(Floatpoint p, int direction)
{
this.position=p;
this.direction=direction;
}
public bullet1(Floatpoint p)
{
this.position=p;
}
public void drawit1(Graphics2D g,Float mapposition)
{
if (isAlive) {
g.setColor(Color.WHITE);
g.fillRoundRect((int)(this.position.x- mapposition) * 3 - (int)this.size, (int)this.position.y* 3 - (int)this.size, (int)this.size * 2, (int)this.size * 2, (int)this.size * 2, (int)this.size * 2);
}
}
public void move(int direction) {
if(first)
{type=direction;
first=false;
}
if(type==1)
{position.y+=speedy;}
else if(type==2)
{position.x-=speedx;}
else if(type==4)
{position.x+=speedx;}
else if(type==5)
{position.x+=speedx;
position.y-=speedy;}
else if(type==6)
{position.x-=speedx;
position.y-=speedy;}
else if(type==7)
{position.x-=speedx;
position.y+=speedy;}
else if(type==7)
{position.x+=speedx;
position.y+=speedy;}
}}
下面发了部分源码 太大了 求各位大能帮帮忙啊
直接一个工程地址还好点