0
\$\begingroup\$

I need to fix my destroyed fireball part. so, briefly, you can see there is condition, when fire ball takes 0.5 seconds, it will be destroyed, but the problem is that when I launch fireball more than 5 times, the state time is missing. meaning, statetime from first fireball takes only 0.2 or 0.3 seconds which means not to destroy firstfire ball, then, second fireball will start with 0 of statetime....

so I might need to put delay or change condition to destroy fireball, anyone give some good idea of delay...

public void update(float dt){
    if(destroyed)
      return;
    stateTime += dt;

    setRegion((TextureRegion) fireAnimation.getKeyFrame(stateTime, true));
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);

    if(b2body.getLinearVelocity().y > 2f)
        b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f);


if((stateTime > 0.5f &&  !destroyed)) {
        System.out.println("remove");
        world.destroyBody(b2body);
        destroyed = true;
        stateTime=0;
        }
    }

0.020851972 0.033725634 0.053520873 0.066143505 0.08290026 0.099830225 0.11613068 0.13314703 0.14978832 0.17205454 0.18305379 0.1999722 0.016918411 0.21638213 0.033328343 0.2332518 0.050198004 0.24974939 0.0666956 0.2671502 0.08409641 0.2834545 0.10040071 0.3003866 0.1173328 0.31650147 0.13344765 0.33359608 0.15054226 0.35006973 0.16701591 0.36673754 0.18368372 0.38316628 0.20011248 0.39938805 0.21633424 0.4166165 0.2335627 0.01722846 0.43257055 0.24951674 0.033182513 0.44934398 0.2662902 0.04995595 0.46642104 0.28336725 0.06703302 0.48323897 0.30018517 0.083850935 0.5002271 remove 0.017710429 0.032923356 0.04943849 0.07523671 0.08288573 0.100497365 0.11557686 0.13266633 0.14927213 0.1661589 0.1828977 0.19979131 0.21625298 0.23346049 0.017207505 0.24935424 0.03310126 0.26641506 0.05016208 0.28889254 0.07263956 0.2989407 0.08268771 0.31584114 0.09958816 0.33276853 0.116515554 0.3495424 0.13328943 0.3659887 0.1497357 0.3825479 0.16629489 0.39946073 0.18320774 0.41664386 0.20039086 0.01718313 0.4328036 0.21655062 0.033342883 0.44915196 0.23289897 0.049691234 0.465505 0.24925202 0.066044286 0.48231223 0.26605925 0.082851514 0.49968737 0.2834344 0.100226656 0.51679564 remove 0.01773908 0.03359734 0.050028652 0.06635733 0.08378251 0.1004918 0.117590256 0.13499662 0.15284775 0.16789775 0.18452707 0.016629318 0.20071205 0.0328143 0.21726353 0.04936579 0.23375686 0.06585911 0.25063464 0.082736894 0.26645997 0.098562226 0.28290412 0.11500637 0.299935 0.13203725 0.31656048 0.14866272 0.3334267 0.16552895 0.3501189 0.18222114 0.36671102 0.19881326 0.38633305 0.2184353 0.01962204 0.40130693 0.23340917 0.034595914 0.41854438 0.25064662 0.051833354 0.4356347 0.26773694 0.06892368 0.45077664 0.28287888 0.084065616 0.46706852 0.29917076 0.10035752 0.48343226 0.3155345 0.116721265 0.49974683 0.33184907 0.13303584 0.516265 remove 0.016947491 0.032934472 0.048946258 0.06602675 0.08432648 0.099015966 0.115975864 0.13195857 0.14873886 0.16540411 0.18193763 0.19992903 0.017991396 0.21548836 0.033550724 0.2321489 0.050211262 0.25003594 0.06809831 0.26674396 0.084806316 0.28344768 0.10151005 0.30490693 0.122969285 0.3170895 0.13515186 0.3336414 0.15170377 0.34876966 0.16683203 0.36539513 0.1834575 0.38354647 0.20160884 0.018151337 0.3993547 0.21741706 0.03395956 0.41623548 0.23429784 0.05084034 0.43323898 0.25130135 0.067843854 0.44962156 0.26768392 0.084226415 0.46707925 0.28514162 0.1016841 0.48308077 0.30114314 0.11768562 0.49984995 0.3179123 0.13445479 0.516058 remove 0.01654849 0.031967547 0.0488077 0.0658899 0.08240888 0.09975024 0.11665027 0.13329327 0.14952914 0.16542803 0.18207018 0.19933029 0.017260108 0.2156132 0.033543028 0.23173532 0.049665146 0.24841681 0.06634664 0.26591128 0.0838411 0.28397366 0.10190348 0.30036992 0.11829973 0.3168611 0.13479091 0.33443168 0.1523615 0.35014495 0.16807479 0.3657988 0.18372862 0.38298365 0.20091346 0.01718484 0.3988928 0.21682261 0.03309399 0.41890058 0.2368304 0.05310177 0.43216553 0.25009537 0.06636674 0.45275748 0.27068734 0.0869587 0.46601132 0.28394118 0.100212544 0.48274627 0.30067614 0.116947494 0.49909204 0.3170219 0.13329327 0.51553744 remove

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It looks like you are using 1 timer value for all your fireballs and it looks like you want each fireball to last 0.5 seconds before being destroyed. So, what you need to do is to have each fireball have its own timer.

I would have a class called FiireBall which contains all the fireball components as well as its own timer like this:

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.Body;


public class FireBall{
    public Body boxBody;
    public Animation<TextureRegion> animation;
    public float destroyTimer = 0.5f;
    public boolean toBeDestroyed = false; 

    public FireBall(Body bod, Animation<TextureRegion>  anim){
        animation = anim;
        boxBody = bod;
    }
    public void update(float delta){
        destroyTimer-= delta;

        if(destroyTimer <= 0){
            toBeDestroyed = true; // set destroy flag
        }

        // .. do your usual drawing
        // setRegion((TextureRegion) fireAnimation.getKeyFrame(stateTime, true));
        // setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);

        //limit speed as usual
        if(boxBody.getLinearVelocity().y > 2f)
            boxBody.setLinearVelocity(boxBody.getLinearVelocity().x, 2f);
    }
}

Now you have your fireball you just need to add one when needed with

FireBall newBall = new FireBall(box2dBody,animation);

Finally, you need an array to store all the FireBalls

Array<FireBall> fireballs = new Array<FireBall>();

Now you have an array for each of you fireballs you just loop through them each frame like this:

    for(FireBall fball:fireballs){
        if(fball.toBeDestroyed){
            System.out.println("remove");
            world.destroyBody(b2body);
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .