Java LibGDX中的多敌人阵列

Java LibGDX中的多敌人阵列,java,android,arrays,libgdx,Java,Android,Arrays,Libgdx,我试图制造一个多敌人阵列,每30秒一颗新子弹从一个随机点射出。如果子弹被点击,它就会消失,并且会出现爆炸般的爆裂声。如果子弹击中了球,球就会砰的一声弹起。 所以子弹应该换成不同的精灵或纹理。流行音乐也一样 但所有发生的只是子弹,如果被碰了,砰的一声,其他什么都不会发生。如果修改了,那么子弹会一直闪烁,因为更新太多了 我在代码中添加了注释,以解释有关问题的更多信息 下面是代码。 如果需要更多的代码,我将提供 多谢各位 public class GameRenderer { private Gam

我试图制造一个多敌人阵列,每30秒一颗新子弹从一个随机点射出。如果子弹被点击,它就会消失,并且会出现爆炸般的爆裂声。如果子弹击中了球,球就会砰的一声弹起。 所以子弹应该换成不同的精灵或纹理。流行音乐也一样

但所有发生的只是子弹,如果被碰了,砰的一声,其他什么都不会发生。如果修改了,那么子弹会一直闪烁,因为更新太多了

我在代码中添加了注释,以解释有关问题的更多信息

下面是代码。 如果需要更多的代码,我将提供

多谢各位

public class GameRenderer {

private GameWorld myWorld;
private OrthographicCamera cam;
private ShapeRenderer shapeRenderer;
private SpriteBatch batcher;

// Game Objects
private Ball ball;
private ScrollHandler scroller;
private Background background;
private Bullet bullet1;
private BulletPop bPop;

private Array<Bullet> bullets;

    // This is for the delay of the bullet coming one by one every 30 seconds.
/** The time of the last shot fired, we set it to the current time in nano when the object is first created */
double lastShot = TimeUtils.nanoTime();
  /** Convert 30 seconds into nano seconds, so 30,000 milli = 30 seconds */
 double shotFreq = TimeUtils.millisToNanos(30000);

// Game Assets
private TextureRegion bg, bPop;
private Animation bulletAnimation, ballAnimation;
private Animation ballPopAnimation;

public GameRenderer(GameWorld world) {
    myWorld = world;
    cam = new OrthographicCamera();
    cam.setToOrtho(true, 480, 320);

    batcher = new SpriteBatch();
    // Attach batcher to camera
    batcher.setProjectionMatrix(cam.combined);

    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(cam.combined);

            // This is suppose to produce 10 bullets at random places on the background.
    bullets = new Array<Bullet>();
    Bullet bullet = null;
    float bulletX = 00.0f;
    float bulletY = 00.0f;
    for (int i = 0; i < 10; i++) {
        bulletX = MathUtils.random(-10, 10);
        bulletY = MathUtils.random(-10, 10);
        bullet = new Bullet(bulletX, bulletY);

        AssetLoader.bullet1.flip(true, false);
        AssetLoader.bullet2.flip(true, false);

        bullets.add(bullet);
    }

    // Call helper methods to initialize instance variables
    initGameObjects();
    initAssets();
}

private void initGameObjects() {
    ball = GameWorld.getBall();
    bullet1 = myWorld.getBullet1();
    bPop = myWorld.getBulletPop();
    scroller = myWorld.getScroller();
}

private void initAssets() {
    bg = AssetLoader.bg;
    ballAnimation = AssetLoader.ballAnimation;
    bullet1Animation = AssetLoader.bullet1Animation;
    ballPopAnimation = AssetLoader.ballPopAnimation;
}

    // This is to take the bullet away when clicked or touched.
public void onClick() {
    for (int i = 0; i < bullets.size; i++) {
        if (bullets.get(i).getBounds().contains(0, 0))
            bullets.removeIndex(i);
    }
}

private void drawBackground() {
    batcher.draw(bg1, background.getX(), background.getY(), background.getWidth(), backgroundMove.getHeight());
}

public void render(float runTime) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    batcher.begin();
    // Disable transparency 
    // This is good for performance when drawing images that do not require
    // transparency.
    batcher.disableBlending();

    drawBackground();

    batcher.enableBlending();

            // when the bullet hits the ball, it should be disposed or taken away and a ball pop sprite/texture should be put in its place
    if (bullet1.collides(ball)) {
                    // draws the bPop texture but the bullet does not go just keeps going around, and the bPop texture goes.
        batcher.draw(AssetLoader.bPop, 195, 273);
    }

    batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

           // this is where i am trying to make the bullets come one by one, and if removed via the onClick() then bPop animation 
           // should play but does not???
    if(TimeUtils.nanoTime() - lastShot > shotFreq){
         // Create your stuff
        for (int i = 0; i < bullets.size; i++) {
            bullets.get(i);
            batcher.draw(AssetLoader.bullet1Animation.getKeyFrame(runTime), bullet1.getX(), bullet1.getY(), bullet1.getOriginX(), bullet1.getOriginY(), bullet1.getWidth(), bullet1.getHeight(), 1.0f, 1.0f, bullet1.getRotation());
            if (bullets.removeValue(bullet1, false)) {
                batcher.draw(AssetLoader.ballPopAnimation.getKeyFrame(runTime), bPop1.getX(), bPop1.getY(), bPop1.getWidth(), bPop1.getHeight());
            }
        }
         /* Very important to set the last shot to now, or it will mess up and go full auto */
         lastShot = TimeUtils.nanoTime();
      }

    // End SpriteBatch
    batcher.end();
}
}
公共类游戏渲染器{
私人游戏世界myWorld;
私人正交摄像机;
私人shaperender shaperender;
专用SpriteBatch配料机;
//游戏物品
私人舞会;
私人卷轴;
私人背景;
私人子弹通讯1;
私人通讯;
专用阵列子弹;
//这是因为子弹每30秒一颗接一颗的延迟。
/**最后一次发射的时间,我们将其设置为第一次创建对象时的当前时间(以纳米为单位)*/
double lastShot=TimeUtils.nanoTime();
/**将30秒转换为纳秒,因此30000毫秒=30秒*/
双倍喷射频率=TimeUtils.millisToNanos(30000);
//游戏资产
私人纹理区域背景,bPop;
私人动画、动画、民谣;
私人动画;
公共游戏渲染器(游戏世界){
我的世界=世界;
cam=新的正交摄影机();
凸轮设置为(真,480320);
批处理程序=新的SpriteBatch();
//将批处理程序连接到相机
batcher.setProjectionMatrix(cam.combined);
ShaperEnder=新的ShaperEnder();
ShaperEnder.setProjectionMatrix(cam.combined);
//假设在背景上的任意位置产生10个子弹。
项目符号=新数组();
Bullet=null;
浮球X=00.0f;
浮动弹头=00.0f;
对于(int i=0;i<10;i++){
bulletX=数学随机(-10,10);
bulletY=MathUtils.random(-10,10);
子弹=新子弹(子弹X,子弹Y);
AssetLoader.bullet1.flip(真、假);
AssetLoader.bullet2.flip(真、假);
项目符号。添加(项目符号);
}
//调用helper方法来初始化实例变量
initGameObjects();
初始资产();
}
私有void initGameObjects(){
ball=GameWorld.getBall();
bullet1=myWorld.getBullet1();
bPop=myWorld.getBulletPop();
scroller=myWorld.getScroller();
}
私人资产(){
bg=AssetLoader.bg;
ballAnimation=资产装载机。ballAnimation;
Bullet1动画=AssetLoader.Bullet1动画;
ballPopAnimation=AssetLoader.ballPopAnimation;
}
//这是在单击或触摸时将子弹带走。
公共void onClick(){
对于(int i=0;ishotFreq){
//创造你的东西
对于(int i=0;i

谢谢你

嗯……为什么你要在添加新项目符号的
中绘制图形?这样的话,你画的所有画面每30秒只会淹没一帧。如果
中有
,您应该始终只添加/删除对象并在外部绘制它们。如果
,则该
内无图纸

除了米朗的回答

子弹。得到(我);行不行。。您需要将返回的项目符号存储到一个变量中,您似乎为该变量创建了bullet1变量

另外,在循环数组时,不应该向数组中添加元素或从数组中删除元素。考虑使用第二个数组来添加/移除元素,并使用它来改变主数组或使用迭代器。 [编辑] 在这种特殊情况下,您也可以执行类似的操作,尽管每次单击仅适用于一个项目符号

int index = -1;
for (int i = 0; i < bullets.size; i++) {
    if (bullets.get(i).getBounds().contains(0, 0)) {
        index = i;
        break;
    }
}
if(index > -1) bullets.removeIndex(index);
int索引=-1;
对于(int i=0;i