Java射击游戏移除子弹对象
我有一个射击游戏,当按下空格键时,会创建一个子弹对象。当子弹物体在屏幕外时,我想删除这个物体,这样我可以射击10次以上 实例化:Java射击游戏移除子弹对象,java,arrays,object,Java,Arrays,Object,我有一个射击游戏,当按下空格键时,会创建一个子弹对象。当子弹物体在屏幕外时,我想删除这个物体,这样我可以射击10次以上 实例化: Shot[] shot = new Shot[10]; 空格条形码: if (key == KeyEvent.VK_SPACE) { if (shots < maxShots) { if (canShoot) { shot[shots] = new Shot(player.x, player.y, player.
Shot[] shot = new Shot[10];
空格条形码:
if (key == KeyEvent.VK_SPACE) {
if (shots < maxShots) {
if (canShoot) {
shot[shots] = new Shot(player.x, player.y, player.width, player.height, shotDir);
shots++;
canShoot = false;
}
}
}
请改用,以便在超出范围时将其从列表中删除
ArrayList<Shot> shot = new ArrayList<Shot>();
ArrayList shot=new ArrayList();
迭代时不应删除,因此可能必须存储对象(或对象本身)的索引,并在找到要删除的项目符号后删除它们
另外,如果您只想要10个,您可以将容量设置为10,只需监视大小以确保它不会超过
class Weapon
{
private ArrayList<Shot> magazine;
private int maxShots;
private double rotationAngle = 90;
private double rotationSpeed = 1.5;
public Weapon(int maxShots)
{
this.maxShots = maxShots;
magazine = new ArrayList<Shot>(maxShots);
}
public void rotate(int direction)
{
// no need to create hashamp etc to hold "left" or "right"
// when passing keyPressed, KeyeEvent.getKeyCode is enough.
// i see alot of people having separate if statements,
// why, i dont know when you can only go in one
// direction at a time!
// also of note, most people wrongly assume that
// either left or right will be passed, hence, if/else
// statements. use "if/else if" to guarantee only
// left or right
if (direction == KeyEvent.VK_LEFT)
{
angle-=speed;
}
else if (direction == KeyEvent.VK_RIGHT)
{
angle+=speed;
}
}
public void draw(Graphics2D g2, Dimension boundary)
{
// do NOT check if shot is out of bounds here,
// the only thing that should be done in draw,
// is draw!
}
public Iterator<Shot> getMagazine()
{
// by returning the iterator, you guarantee
// concurrency i.e preventing concurrent modifications
// to data, for example, drawing shots, moving shots
// and removing them
return magazine.iterator();
}
public void shoot()
{
double radian = Math.toRadians(-angle); // invert angle
double x = [center x] + ([length of weapon] * Math.cos(radian))-[your shot diameter];
double y = [center y] + ([length of weapon] * Math.sin(radian))-[your shot diameter];
if (magazine.size() < capacity)
{
magazine.add(new Shot(x,y,radian));
}
}
}
class Shot extends Ellipse2D.Double
{
Point2D.Double point;
Point2D.Double velocity;
double shotSpeed = 10;
public Shot(Point2D.Double origin, double radian)
{
velocity = new Point2D.Double(Math.cos(radian)*shotSpeed,Math.sin(radian)*shotSpeed);
point = new Point2D.Double(origin.x+velocity.x,origin.y+velocity.y);
setFrame(point.x,point.y,[shot diameter],[shot diameter]);
}
public void draw(Graphics2D g2)
{
g2.setColor(Color.pink);
g2.fill(this);
}
public void move()
{
point.x+=velocity.x;
point.y+=velocity.y;
setFrame(point.x,point.y,[shot diameter],[shot diameter]);
}
}
// use javax.swing.Timer to update/paint instead is
// while(true) with a Thread.sleep. it allows for stopping and
// restarting without creating additional objects
public void actionPerformed(ActionEvent event)
{
// check for rotation......
// check for shots fired....
// iterator is faster than conditional loops
Iterator<Shot> iterator = weapon.getMagazine();
while(iterator.hasNext())
{
Shot shot = iterator.next();
// getBounds() is the bounds of your panel
// shot.getBounds() is an inherited method of
// Ellipse2D.Double
// use java.awt.geom.RectangleShape.contains(Rectangle) method
if (getBounds().contains(shot.getBounds()))
{
// if the shot is within the rectangle, move it
shot.move();
}
else
{
iterator.remove();
}
}
// collision detection etc...
repaint();
}
{
私人ArrayList杂志;
私有int-maxShots;
专用双旋转角=90;
私人双旋转速度=1.5;
公共武器(int-maxShots)
{
this.maxShots=maxShots;
杂志=新阵列列表(maxShots);
}
公共空心旋转(int方向)
{
//无需创建hashamp等来保持“左”或“右”
//按下传递键时,KeyeEvent.getKeyCode就足够了。
//我看到很多人都有各自的if语句,
//为什么,我不知道你什么时候只能坐一次车
//一次一个方向!
//同样值得注意的是,大多数人错误地认为
//左或右将被传递,因此,if/else
//声明。使用“如果/否则如果”仅保证
//左还是右
if(direction==KeyEvent.VK_LEFT)
{
角度-=速度;
}
else if(direction==KeyEvent.VK_RIGHT)
{
角度+=速度;
}
}
公共空心图(图形2D g2,尺寸边界)
{
//不要在这里检查射门是否超出范围,
//在抽签时唯一应该做的事,
//是抽签!
}
公共迭代器getMagazine()
{
//通过返回迭代器,您可以保证
//并发性,即防止并发修改
//到数据,例如,绘图快照、移动快照
//并将其移除
return.iterator();
}
公开射击()
{
双弧度=数学上的toradian(-angle);//反转角度
双x=[中心x]+([武器长度]*数学cos(弧度))-[你的射击直径];
双y=[中心y]+([武器长度]*数学sin(弧度))-[你的射击直径];
if(刀库尺寸()<容量)
{
添加(新快照(x,y,弧度));
}
}
}
类快照扩展了Ellipse2D。双精度
{
点2D。双点;
点2D。双速度;
双拍速度=10;
公共快照(点2D.双原点,双弧度)
{
速度=新点2d.Double(数学cos(弧度)*拍摄速度,数学sin(弧度)*拍摄速度);
点=新点2d.Double(origin.x+velocity.x,origin.y+velocity.y);
设定帧(点x,点y,[喷丸直径],[喷丸直径]);
}
公共空白绘图(图形2D g2)
{
g2.setColor(颜色为粉红色);
g2.填充(此);
}
公开作废动议()
{
点x+=速度x;
点y+=速度y;
设定帧(点x,点y,[喷丸直径],[喷丸直径]);
}
}
//改为使用javax.swing.Timer更新/绘制
//虽然(真的)有一个线程。睡眠。它允许停车和停车
//在不创建其他对象的情况下重新启动
已执行的公共无效操作(操作事件)
{
//检查旋转。。。。。。
//检查是否有枪声。。。。
//迭代器比条件循环快
迭代器迭代器=武器.getMagazine();
while(iterator.hasNext())
{
Shot Shot=iterator.next();
//getBounds()是面板的边界
//getBounds()是一个继承的
//省略号2d,双人
//使用java.awt.geom.RectangleShape.contains(Rectangle)方法
if(getBounds().contains(shot.getBounds()))
{
//如果快照位于矩形内,请移动它
shot.move();
}
其他的
{
iterator.remove();
}
}
//碰撞检测等。。。
重新油漆();
}
shot[i]背后的代码是什么?边界外(w,h)?谢谢,它花了一点时间才开始工作,但我成功了。
public boolean outOfBounds(int screenx, int screeny) {
if (x < 0 || x > screenx || y < 0 || y > screeny) {
return true;
} else {
return false;
}
}
ArrayList<Shot> shot = new ArrayList<Shot>();
class Weapon
{
private ArrayList<Shot> magazine;
private int maxShots;
private double rotationAngle = 90;
private double rotationSpeed = 1.5;
public Weapon(int maxShots)
{
this.maxShots = maxShots;
magazine = new ArrayList<Shot>(maxShots);
}
public void rotate(int direction)
{
// no need to create hashamp etc to hold "left" or "right"
// when passing keyPressed, KeyeEvent.getKeyCode is enough.
// i see alot of people having separate if statements,
// why, i dont know when you can only go in one
// direction at a time!
// also of note, most people wrongly assume that
// either left or right will be passed, hence, if/else
// statements. use "if/else if" to guarantee only
// left or right
if (direction == KeyEvent.VK_LEFT)
{
angle-=speed;
}
else if (direction == KeyEvent.VK_RIGHT)
{
angle+=speed;
}
}
public void draw(Graphics2D g2, Dimension boundary)
{
// do NOT check if shot is out of bounds here,
// the only thing that should be done in draw,
// is draw!
}
public Iterator<Shot> getMagazine()
{
// by returning the iterator, you guarantee
// concurrency i.e preventing concurrent modifications
// to data, for example, drawing shots, moving shots
// and removing them
return magazine.iterator();
}
public void shoot()
{
double radian = Math.toRadians(-angle); // invert angle
double x = [center x] + ([length of weapon] * Math.cos(radian))-[your shot diameter];
double y = [center y] + ([length of weapon] * Math.sin(radian))-[your shot diameter];
if (magazine.size() < capacity)
{
magazine.add(new Shot(x,y,radian));
}
}
}
class Shot extends Ellipse2D.Double
{
Point2D.Double point;
Point2D.Double velocity;
double shotSpeed = 10;
public Shot(Point2D.Double origin, double radian)
{
velocity = new Point2D.Double(Math.cos(radian)*shotSpeed,Math.sin(radian)*shotSpeed);
point = new Point2D.Double(origin.x+velocity.x,origin.y+velocity.y);
setFrame(point.x,point.y,[shot diameter],[shot diameter]);
}
public void draw(Graphics2D g2)
{
g2.setColor(Color.pink);
g2.fill(this);
}
public void move()
{
point.x+=velocity.x;
point.y+=velocity.y;
setFrame(point.x,point.y,[shot diameter],[shot diameter]);
}
}
// use javax.swing.Timer to update/paint instead is
// while(true) with a Thread.sleep. it allows for stopping and
// restarting without creating additional objects
public void actionPerformed(ActionEvent event)
{
// check for rotation......
// check for shots fired....
// iterator is faster than conditional loops
Iterator<Shot> iterator = weapon.getMagazine();
while(iterator.hasNext())
{
Shot shot = iterator.next();
// getBounds() is the bounds of your panel
// shot.getBounds() is an inherited method of
// Ellipse2D.Double
// use java.awt.geom.RectangleShape.contains(Rectangle) method
if (getBounds().contains(shot.getBounds()))
{
// if the shot is within the rectangle, move it
shot.move();
}
else
{
iterator.remove();
}
}
// collision detection etc...
repaint();
}