1
\$\begingroup\$

I use the following chunk of code to make bullets that fire towards the player's ship:

@Override
public void onTimePassed(final TimerHandler pTimerHandler)
{
    pTimerHandler.reset();
    if(spawnBullets){
        for(int i = 0; i < enemies.size(); i++){
            if(enemies.get(i).isShooter()){
                //shoot from this enemy to ship
                final Vector2 v = new Vector2(ship.getX() - enemies.get(i).getSprite().getX(),ship.getY() - enemies.get(i).getSprite().getY());
                v.nor();
                v.mul(BULLET_SPEED);
                createEnemyBullet(enemies.get(i).getSprite().getX(),enemies.get(i).getSprite().getY(), v);
            }
        }
    }
}

This works fine if the enemy is stationary, i.e. just a Sprite with no Body, but when the enemy is moving, my game completely freezes up. Sometimes it will happen immediately (on the first attempt to fire), or sometimes 1 or 2 bullets later.

It gets through this code (a log output after createEnemyBullet works fine), but locks up somewhere before anything else happens. Disabling this section of code makes the problem go away. What could be going wrong?

Edit

Here is createEnemyBullet():

private void createEnemyBullet(float x, float y, Vector2 v){
    //remove old bullet
    if(enemyBullets[enemyBulletCounter] != null){
        makeBulletExplosion(bullets[i].getX(),bullets[i].getY());
        mScene.detachChild(bullets[i]);
        mBulletPhysicsWorld.destroyBody(bulletsPhysical[i]);
        bullets[i] = null;
    }
    //create new bullet 
    enemyBullets[enemyBulletCounter] = new AnimatedSprite(x +8, y +8, this.mEnemyBulletTextureRegion); //Nasty magic number, oh well
    enemyBulletsBody[enemyBulletCounter] = PhysicsFactory.createCircleBody(this.mBulletPhysicsWorld, enemyBullets[enemyBulletCounter], BodyType.KinematicBody, BULLET_FIXTURE_DEF);

    enemyBullets[enemyBulletCounter].animate(55);
    enemyBulletsBody[enemyBulletCounter].setLinearVelocity(v);

    this.mBulletPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(enemyBullets[enemyBulletCounter], enemyBulletsBody[enemyBulletCounter], true, true));
    this.mScene.attachChild(enemyBullets[enemyBulletCounter]);
    //update counter
    enemyBulletCounter = enemyBulletCounter < ENEMY_BULLET_MAX -1 ? enemyBulletCounter + 1 : 0;
}
\$\endgroup\$
9
  • 2
    \$\begingroup\$ I guess there is something wrong with createEnemyBullet function, though there may be a problem with another section of your code that is showing itself whenever you call this function(meaning this function doesn't have any errors at all). \$\endgroup\$
    – Ali1S232
    Commented Aug 25, 2011 at 17:30
  • \$\begingroup\$ @Gajet Is Java Call-By-Name or Call-By-Value? I assume the arguments of createEnemyBullet() are being calculated once, then createEnemyBullet() is run, if this isn't that case that could potentially cause a problem. \$\endgroup\$
    – fredley
    Commented Aug 25, 2011 at 17:39
  • \$\begingroup\$ How about showing us what the createEnemyBullet() does? \$\endgroup\$
    – pek
    Commented Aug 25, 2011 at 18:19
  • 1
    \$\begingroup\$ @fredley: I'm not a Java developer so I don't have any idea about Java default function call system, but I guess that isn't the problem. as I mentioned earlier it's either some issue with tour createEnemyBullet function or it's some other bug that is showing itself whenever you call this function (don't know if it's possible in java but maybe some heap corruption somewhere) \$\endgroup\$
    – Ali1S232
    Commented Aug 25, 2011 at 18:34
  • 1
    \$\begingroup\$ createEnemyBullet() doesn't add to the "enemies" list, does it? \$\endgroup\$ Commented Aug 25, 2011 at 19:00

1 Answer 1

2
\$\begingroup\$

I'm going to write an answer instead of a comment.

Looking at your CreateBullet method: You are creating two new objects for each new bullet: AnimatedSprite and CircleBody You will have many short-lived instances of them. Better to recycle them. Have an object pool of both classes and initialize them as invisible/disabled. Each time a new bullet is requested just retrieve an unused object from each pool, initialize them and activate them. Bullets objects that are destroyed are returned to the pool for reuse.

A brute force "debugging" tip: To narrow the line causing the issue. Print to console between each line. This is not pretty, but will quickly show which line causes the hanging.

You also said the code works fine, if the enemy has no physical body. Could it be that you spawn new bullets inside the enemies body? That might trip the collision resolution of the physics engine.

\$\endgroup\$

You must log in to answer this question.

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