Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ive just started with andEngine the past week and i only started learning java/android 3 weeks.

I can use a for loop to add multiple sprites to the screen but when i try to check collisions on them it only does it to one and not the rest.

I want to be able to add a specific number for sprites made from the same texture to the scene, add collision detection to them and also make them slide across the screen (im making a game where you avoid the obstacles).

My simple code:

private void createobstacle(float pX, float pY) {

    obstacle = new AnimatedSprite(pX, pY, this.mObjTextureRegion.deepCopy(),     getVertexBufferObjectManager());
    obstacle.setScale(MathUtils.random(0.5f, 3f));

private void createobstacle(int num) {
    for(int i=0; i<=num; i++ ) {
        final float xPos = MathUtils.random(30.0f, (CAMERA_WIDTH - 30.0f));
        final float yPos = MathUtils.random(30.0f, (CAMERA_HEIGHT - 30.0f));
        createobstacle(xPos, yPos);

Ive read about arrays but i cannot find any tutorials about anything im stuck with.

Any help would be great!

share|improve this question
up vote 0 down vote accepted

The sample code you provided is not going to allow you to do collision detection or the like, because you're not holding references to multiple sprites--instead, you're replacing the same field. Let me give you an example that resembles your code, but simplifies the problem:

private int x;

private void setXRepeatedly(int count) {
    for (int i = 0; i < count; i++) {

private void setX(int amount) {
    x = amount;

If I ran this I would have no way of comparing one of the values of x with another value of x, because the older x value is constantly getting rewritten with no reference.

This is somewhat analogous to your code. Your createObstacle(int) method repeatedly calls your createObstacle(float, float) method, and the latter repeatedly sets the same obstacle field. So you're not holding a reference to multiple obstacles, you're only holding one.


One solution would simply be to use multiple fields. In my example above, instead of having just an x variable, I could have a y, a z, and so forth. A better approach would be to use an array, and an even better approach would be to use a java collection. First Arrays:

In my example code above, instead of using int x, I could declare an array of ints, like this:

int[] myArray = new int[10];

This declares an array of ints and allocates memory for 10 of them. Now I could set individual ints in the array by putting the reference number in brackets. Like this:

myArray[8] = 5; myArray[7] = 2; etc.

enter image description here

So, in your example, we could have a declaration something like:

AnimatedSprite[] obstacles = new AnimatedSprite[10];
obstacles[0] = new AnimatedSprite(etc, etc.);
obstacles[1] = new AnimatedSprite(etc, etc.);

Note, arrays start counting at zero, not at one, and if you attempt to access or place data outside the bounds of what was allocated, you'll get an error. These and other reasons are why it often makes sense to use one of the java collections instead of an array.

ArrayList Example

There are a number of different java collections with different feaures and benefits for different situations. An ArrayList is a handy simple one that avoids the problem described above of having to know the total size in advance. You create an ArrayList like this:

ArrayList<AnimatedSprite> obstacles = new ArrayList<AnimatedSprite>();

Now I can add objects to the ArrayList using the add() method, like this:

obstacles.add(new AnimatedSprite(etc.,etc.));

You can also call methods like these:

  • get( int index ) - retrieves object reference from ArrayList index position
  • size() - returns ArrayList size
  • remove( int index ) - removes the element at the specified position in this list. Shifts any subsequent elements to the left and returns the element that was removed from the list.
  • indexOf( Object o) - finds the index in this list of the first occurrence of the specified element
  • clear() - removes all of the elements
share|improve this answer
Sidenote, I'm not a huge fan of the names of your methods. Having two methods called createObstacle() when only one of them creates an obstacle is going to lead to confusion. – Cameron Fredman Feb 18 '13 at 16:42

You have add your all the required obstacles in to the list and in onManangeUpdate method of the sprite you can loop through. Check for collision and perform whatever thing you want.

While creation time:


While in onManageUpdate of the sprite:

for(int i=0;i<obstacleList.size();i++){
        // do things you want
share|improve this answer

Your Answer


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.