Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok with the help of Mr JohnEye, I've been able to fix the problem in my code. Consider the following snippet:</p> <pre><code> protected void checkSoldierCollision() { // TODO Auto-generated method stub int numBombs = bombGroup.getChildCount(); int numTroops = troopsGroup.getChildCount(); final ArrayList&lt;Sprite&gt; toBeDetached = new ArrayList&lt;Sprite&gt;(); for (int i = 0; i &lt; numBombs; i++) { Sprite s = (Sprite) bombGroup.getChildByIndex(i); for (int j = 0; j &lt; numTroops; j++) { Sprite s2 = (Sprite) troopsGroup.getChildByIndex(j); if (s.collidesWith(s2)) { Sprite splat = createSplat(); splat.setPosition(s.getX(), s.getY()); getEngine().getScene().attachChild(splat); Sprite splode = createExplosion(); splode.setPosition(s.getX(), s.getY()); explodeGroup.attachChild(splode); // getEngine().getScene().attachChild(splode); // WARNING: cannot detach from the list // while looping through the list toBeDetached.add(s); toBeDetached.add(s2); } } } runOnUpdateThread(new Runnable() { @Override public void run() { for (Sprite s : toBeDetached) { s.detachSelf(); Sprite splat = createSplat(); splat.setPosition(s.getX(), s.getY()); getEngine().getScene().attachChild(splat); Sprite splode = createExplosion(); splode.setPosition(s.getX(), s.getY()); explodeGroup.attachChild(splode); // getEngine().getScene().attachChild(splode); } toBeDetached.clear(); } }); } </code></pre> <p>As I've mentioned, the game crashes because of a discrepancy in count between the 2 colliding objects. This situation happens when, as Mr. JohnEye points out, a sprite is spawned in the UI thread, but not in the Update thread as well. Furthermore, consider my createExplosion, createBomb and createSplat functions:</p> <p>CREATE BOMB</p> <pre><code> public Sprite createBomb(float x, float y) { Sprite bombSprite = new Sprite(x, y, this.mBombTextureRegion, getVertexBufferObjectManager()); MoveYModifier downModBomb = new MoveYModifier(1, 60, FLOOR); downModBomb.addModifierListener(new IModifierListener&lt;IEntity&gt;() { @Override public void onModifierStarted(IModifier&lt;IEntity&gt; pModifier, IEntity pItem) { } @Override public void onModifierFinished(IModifier&lt;IEntity&gt; pModifier, final IEntity pItem) { runOnUpdateThread(new Runnable() { @Override public void run() { pItem.detachSelf(); } }); AnimatedSprite explodeSprite = createExplosion(); explodeSprite.setPosition(pItem.getX(), FLOOR); explodeGroup.attachChild(explodeSprite); // getEngine().getScene().attachChild(explodeGroup); } }); bombSprite.registerEntityModifier(downModBomb); return bombSprite; } </code></pre> <p>CREATE EXPLOSION</p> <pre><code>public AnimatedSprite createExplosion() { AnimatedSprite boomSprite = new AnimatedSprite(0, 0, this.mExplodeTextureRegion, getVertexBufferObjectManager()); DelayModifier delay = new DelayModifier(.3f); // delay in seconds, can // take float numbers .5 // seconds delay.addModifierListener(new IModifierListener&lt;IEntity&gt;() { @Override public void onModifierStarted(IModifier&lt;IEntity&gt; pModifier, IEntity pItem) { ((AnimatedSprite) pItem).animate(new long[] { 100, 100, 100 }, // durations/frame new int[] { 1, 2, 3 }, // which frames true); // loop } @Override public void onModifierFinished(IModifier&lt;IEntity&gt; pModifier, final IEntity pItem) { runOnUpdateThread(new Runnable() { @Override public void run() { ((AnimatedSprite) pItem).detachSelf(); } }); } }); boomSprite.registerEntityModifier(delay); // register action return boomSprite; } </code></pre> <p>CREATE SPLAT</p> <pre><code>public Sprite createSplat() { Sprite splatSprite = new Sprite(0, 0, this.mSplatTextureRegion, getVertexBufferObjectManager()); DelayModifier delay = new DelayModifier(.3f); // delay in seconds, can // take float numbers .5 // seconds delay.addModifierListener(new IModifierListener&lt;IEntity&gt;() { @Override public void onModifierStarted(IModifier&lt;IEntity&gt; pModifier, IEntity pItem) { } @Override public void onModifierFinished(IModifier&lt;IEntity&gt; pModifier, final IEntity pItem) { runOnUpdateThread(new Runnable() { @Override public void run() { pItem.detachSelf(); } }); } }); splatSprite.registerEntityModifier(delay); // register action return splatSprite; } </code></pre> <p>Notice that all of them detach/generate other sprites in the runOnUpdate thread. The game crashes in my earlier run-throughs because I did not detach/generate all these sprites on the runOnUpdate class. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload