I am using andengine to make a game where a sprite (player) is going up across the stage, and I want the camera to stay centred on the sprite the entire time.

This is my world so far:

final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

/* Create two sprits and add it to the scene. */




The problem is that when the sprite reaches the top wall, it crashes. How can I fix this?

  • \$\begingroup\$ Seems like you stopped in the middle of your description of the problem... Care to fill us in a bit more on what the actual problem is? \$\endgroup\$ – MichaelHouse Apr 8 '12 at 15:37
  • \$\begingroup\$ thanks, I want the camera follow a sprite, which rises vertically from the stage. but when the sprite reaches the top crashes, do not know how to simulate the scroll? \$\endgroup\$ – tebitoq Apr 8 '12 at 17:00

you could make an int that will cause the map image to scroll for every pixel you move

ex: public int map_x = 0;

then when you draw the image

drawImage(map, map_x, 0, null);

now create a key listener.

      map_x=map_x+2;// this moves the map when you hit the right key, so when your man  
      moves, the map will scroll behind him, making the character seem to move throughout 
      the level.
   //other keylisteners here below.

so you could have your sprite move, but when your sprite moves on his x axis, the map will scroll through the opposite way in its x axis.

this is how I make my simple 2d Mario type games, very simple method.


You are asking two different things in the title and body of your question. The answer to the question in the title is: use camera.setChaseEntity


There is a body called roof. If the player has a body, that is why it crashes. You can just not attach the roof or change its position.

  • 2
    \$\begingroup\$ That's not an answer to his question. If you have tips and suggestions that aren't answers, please post them in the comments. \$\endgroup\$ – jcora Aug 15 '12 at 14:56
  • \$\begingroup\$ The question was "The problem is that when the sprite reaches the top wall, it crashes. How can I fix this?" From what I could understand from the question, this could well be that the roof (which is an object taken from an AndEngine example) prevented the object created by OP from ascending above the screen. This was by no means a tip or a suggestion. \$\endgroup\$ – JohnEye Aug 16 '12 at 7:38

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