1
\$\begingroup\$

I got crash, making fire-balls as bullets.

I guess I should not use "private Array fireballs" in player class. it seems like when I add one more fire as index of 1 into the list, the first index does not look working on destroying in for loop in player class. it seems only destrying index of 0.

in playscreen class,

 public void handleInput(float dt){
       if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
            playerBlue.fire();
        }

in player class,

private Array<FireBall> fireballs;
public void update(float dt) {
  for (FireBall ball : fireballs) {
        ball.update(dt);
        if (ball.isDestroyed())
            fireballs.removeValue(ball, true);
    }   

 }
public void fire() {
    fireballs.add(new FireBall(screen, b2body.getPosition().x, b2body.getPosition().y, Blue.runRightSide ? true : false));

}

in fire class,

public class FireBall extends Weapon {
private TextureRegion fireball;
private float rotate;
public FireBall(PlayScreen screen, float x, float y, boolean rightside) {
    super(screen, x, y, rightside);
    fireball = new TextureRegion(screen.getAtlas().findRegion("fireball"),0,0,40,40);
    setRegion(fireball);
    setBounds(getX(),getY(),getWidth()*2,getHeight()*2);

}

@Override
protected void defineWeapon() {
    BodyDef bdef = new BodyDef();
    bdef.position.set(rightside ? getX() + 12 / AlienAdvantrue.PPM : getX() - 12 / AlienAdvantrue.PPM, getY());
    bdef.type = BodyDef.BodyType.DynamicBody;
    if (!world.isLocked())
        body = world.createBody(bdef);//?
    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(3 / AlienAdvantrue.PPM);
    fdef.filter.categoryBits = AlienAdvantrue.FIREBALL_BIT;
    fdef.filter.maskBits = AlienAdvantrue.GROUND_BIT
            |AlienAdvantrue.Enemy_Head_BIT
            |AlienAdvantrue.BRICK_BIT
            |AlienAdvantrue.COIN_BIT
            |AlienAdvantrue.Enemy_BIT;


    fdef.shape = shape;
    fdef.restitution = 1;
    fdef.friction = 0;
    body.setLinearVelocity(new Vector2(rightside ? 2 : -2, 2.5f));
    body.createFixture(fdef).setUserData(this); 

}

public void update(float dt){
    stateTime += dt;
    rotate +=100; // flip faste// ?
    rotate(rotate);
    setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);

    if((stateTime > 3 || setToDestroy) && !destroyed) {
        world.destroyBody(body);
        destroyed = true;
        stateTime=0;
    }
    if(body.getLinearVelocity().y > 2f)
        body.setLinearVelocity(body.getLinearVelocity().x, 2f);
    if((rightside && body.getLinearVelocity().x < 0) || (!rightside && body.getLinearVelocity().x > 0))
        setToDestroy();
}

}

\$\endgroup\$
2
\$\begingroup\$

The issue is probably this loop:

  for (FireBall ball : fireballs) {
        ball.update(dt);
        if (ball.isDestroyed())
            fireballs.removeValue(ball, true);
    }   

you're removing items while iterating on your loop. That plays funny with the internal iterator since the size of fireball changes: items are basically skipped, and other strange effects.

I'm not an expert, but what's recommended in this Stack Overflow answer is to handle the iterator manually:

public void update(float dt) {

for(Iterator<FireBall> i = fireballs.iterator(); i.hasNext();) {
       Fireball ball = i.next();           
        if (ball.isDestroyed())
          i.remove()
 }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.