I am using this method in Andengine to scroll through a list of items, by setting the camera’s offset.
@Override
public void onScroll(ScrollDetector pScollDetector, int pPointerID,
float pDistanceX, float pDistanceY) {
//Disable the menu arrows left and right (15px padding)
if(mCamera.getXMin()<=15)
menuleft.setVisible(false);
else
menuleft.setVisible(true);
if(mCamera.getXMin()>mMaxX-15)
menuright.setVisible(false);
else
menuright.setVisible(true);
//Return if ends are reached
if ( ((mCurrentX - pDistanceX) < mMinX) ){
return;
}else if((mCurrentX - pDistanceX) > mMaxX){
return;
}
//Center camera to the current point
this.mCamera.offsetCenter(-pDistanceX,0 );
mCurrentX -= pDistanceX;
//Set the scrollbar with the camera
float tempX =mCamera.getCenterX()-CAMERA_WIDTH/2;
// add the % part to the position
tempX+= (tempX/(mMaxX+CAMERA_WIDTH))*CAMERA_WIDTH;
//set the position
//scrollBar.setPosition(tempX, scrollBar.getY());
//set the arrows for left and right
menuright.setPosition(mCamera.getCenterX()+CAMERA_WIDTH/2-menuright.getWidth(),menuright.getY());
menuleft.setPosition(mCamera.getCenterX()-CAMERA_WIDTH/2,menuleft.getY());
//Because Camera can have negativ X values, so set to 0
if(this.mCamera.getXMin()<0){
this.mCamera.offsetCenter(0,0);
mCurrentX=0;
}
}
the problem is that i use this as my second scene in the Activity,
So when i navigate back to the first activity the first scene is out of position because of the camera being moved.
Is there anyway i can reset the camera when going back to the first scene?
I tried Camer.reset() to no avail.
Any suggestions?
Use
mCamera.setCenter(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2);. It will reset the camera position.By the way, I see you keep changing the menu arrows according to the camera position. You know, AndEngine has a class to handle this case 🙂 You have no reason to do it manually…
It’s called
HUD, which means Head-Up Display. You can extend the HUD class, a HUD is just a scene which is placed in a constant position on the screen. You can add as many entities to it as you want. Then, just callmCamera.setHUD(Your HUD object here);and you are done, no need to mess around with moving entities across the camera.Another issue I see is, you call
mCamera.offsetCenter(0, 0);.offsetCentersimply adds the parameters to the current center coordinates. Adding0has no influence at all, so this call is useless. What are you trying to achieve? Reset the camera back to (0, 0)?EDIT:
Here is a HUD example from my own game:
Then you create an instance of this class:
and set it as the camera’s HUD:
In any case you want to use more than 1 HUD, you set 1 as a HUD then make the next one be the first one’s child scene. For example, If I have another HUD called
DisplayStatsand I have created it this way:Then, just set it as the attack control’s child scene. Remember – a HUD is scene! All scene operations are legal here.
And now, the next HUD could be the child scene of
displayStats… And so on.