ArrayIndexOutOfBound Exception While sortChildren() call

  ... questions about tools around the development with AndEngine.

ArrayIndexOutOfBound Exception While sortChildren() call

Postby andgamer » Tue Mar 13, 2012 10:21 am

Hello,
I developing a game using AndEngine. In the game I need to display a small sprite over another big size sprite. For that I set z-index for those sprite. Small sprite has the grater z-index than big sprite. After that I call sortChildren(). But some times the game getting ArrayIndexOutOfBound exception in this line. It is not happening all time.

Can anyone please help me out of this issue
andgamer
 
Posts: 6
Joined: Thu Oct 20, 2011 6:28 am

Re: ArrayIndexOutOfBound Exception While sortChildren() call

Postby imelenchon » Sat Mar 17, 2012 10:36 pm

Hello,

I have the same problem. I am looking a way to avoid it.
imelenchon
 
Posts: 4
Joined: Wed Mar 07, 2012 3:10 am

Re: ArrayIndexOutOfBound Exception While sortChildren() call

Postby OzLark » Sun Mar 18, 2012 2:49 am

I don't have experience doing what you're doing, but I'll make two points:

The ArrayOutOfBounds exception is typically caused when you modify object collections outside of the update thread if the work you are doing is on another thread then you must invoke your sort code on the update thread like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mEngine.runOnUpdateThread(new Runnable() {
  2.     @Override
  3.     public void run() {
  4.         // do sorting stuff here.
  5.     });
  6.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I'm not sure that the z order is the way to layer your sprites, I'm pretty sure I've read around here that you can attach the entity/child by calling this variant of mScene.attachChild():

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean attachChild(final IEntity pEntity, final int pIndex)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


According to the java doc for the underlying ArrayList this is going to, you can squeeze elements in (front, back, or anywhere) which will (I assume) influence the drawing order. The Java doc:

void java.util.ArrayList.add(int index, IEntity object)
Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.


You'll notice the bit I made bold suggests that no values are overwritten, but inserted before..

Again, I'm just guessing, so I could be wrong on all counts :)
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: ArrayIndexOutOfBound Exception While sortChildren() call

Postby imelenchon » Mon Mar 19, 2012 4:46 pm

Hello,

This issue is related to the same kind of problem when self detaching and sprite from an scene: the update thread (in this case the sortChildren) is looking for an entity that no longer exists in the scene.
Probably we are asking for a sorting before and entity is detached from the scene (an sprite for example). So the sorting starts with a set of entities and meanwhile the update thread removes one from the scene (via the pool to do that, wich is my case). So finally the sort ends trying to access an entity that is not there anymore.
I guess the sort should be also done in the update thread.

I put the sortChilden on a runnable and put it in the update thread. I did not get the exception until know.

The explanation about how to put a runnable into the update thread is in this post: post30577.html?hilit=updatethread#p30577

I hope this helps.
imelenchon
 
Posts: 4
Joined: Wed Mar 07, 2012 3:10 am


Return to Tools

Who is online

Users browsing this forum: RonScript, rule0date and 2 guests