Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question
2  
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). –  Ali.S Aug 25 '11 at 17:30
    
@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. –  fredley Aug 25 '11 at 17:39
    
How about showing us what the createEnemyBullet() does? –  pek Aug 25 '11 at 18:19
1  
@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) –  Ali.S Aug 25 '11 at 18:34
1  
createEnemyBullet() doesn't add to the "enemies" list, does it? –  John McDonald Aug 25 '11 at 19:00
show 4 more comments

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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