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 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. */

this.mScene.setBackground(autoParallaxBackground);

this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);

this.mScene.registerUpdateHandler(this.mPhysicsWorld);

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

share|improve this question
    
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? –  Byte56 Apr 8 '12 at 15:37
    
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? –  Esteban Quintero Apr 8 '12 at 17:00
add comment

3 Answers

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.

share|improve this answer
1  
That's not an answer to his question. If you have tips and suggestions that aren't answers, please post them in the comments. –  akled Aug 15 '12 at 14:56
    
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. –  JohnEye Aug 16 '12 at 7:38
add comment

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.

if(key_IsDown)
{
   if(rightkey)
   {
      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.

share|improve this answer
add comment

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

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.