AndEngine 相机偏移和 2 个场景
我在 Andengine 中使用此方法通过设置相机的偏移量来滚动项目列表。
@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;
}
}
问题是我用它作为活动中的第二个场景, 因此,当我导航回到第一个活动时,由于相机被移动,第一个场景就位置不正确了。
无论如何,我可以在返回第一个场景时重置相机吗?
我尝试了 Camer.reset() 无济于事。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
mCamera.setCenter(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2);
。它将重置相机位置。顺便说一句,我看到您不断根据相机位置更改菜单箭头。你知道,AndEngine 有一个类来处理这种情况:) 你没有理由手动执行......
它称为
HUD
,意思是平视显示器。您可以扩展 HUD 类,HUD 只是一个场景,放置在屏幕上的固定位置。您可以根据需要向其中添加任意数量的实体。然后,只需调用 mCamera.setHUD(Your HUD object here); 即可完成,无需在相机上移动实体。我看到的另一个问题是,您调用
mCamera.offsetCenter(0, 0);
。offsetCenter
只是将参数添加到当前中心坐标。添加0
根本没有任何影响,所以这个调用是没有用的。你想达到什么目的?将相机重置回 (0, 0)?编辑:
这是我自己的游戏中的一个 HUD 示例:
然后您创建此类的一个实例:
并将其设置为相机的 HUD:
在任何情况下您想使用超过 1 个 HUD,您可以设置1 作为 HUD,然后使下一个成为第一个的子场景。例如,如果我有另一个名为
DisplayStats
的 HUD,并且我已通过以下方式创建了它:然后,只需将其设置为攻击控件的子场景即可。请记住 - HUD 就是场景!所有场景操作在这里都是合法的。
现在,下一个 HUD 可能是
displayStats
的子场景...等等。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);
.offsetCenter
simply adds the parameters to the current center coordinates. Adding0
has 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
DisplayStats
and 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.如果将来有人发现这个并且其他答案还不够,请尝试这个。
如果您使用的是
ZoomCamera
,这是必需的。我正在这样做,所以如果我“捏缩放”然后切换场景,一切都会变得一团糟,因为我的缩放系数正在调整。In case anyone finds this in the future and the other answers do not suffice, try this.
This is necessary if you are using a
ZoomCamera
. I was doing this and so if I "pinched to zoom" and then switched scenes, it was all messed up because my zoom factor was getting adjusted.