gpt4 book ai didi

java - 创建可滚动级别选择器(AndEngine)

转载 作者:行者123 更新时间:2023-12-01 15:11:22 25 4
gpt4 key购买 nike

我的级别选择器出现问题。我正在使用我在该论坛的教程中找到的代码,但它无法正常工作。

事实是,当用户混合播放时,我正在使用两个场景,我将引擎设置为第二个场景,然后用户可以滚动级别。

然后,当用户按下后退按钮时,我通过在离开之前保存位置然后使用camer.setCenter(x,y)将其设置回初始位置,将相机设置回其初始位置;

一旦用户返回到级别选择器场景,它就不会正确担心。当用户滚动时,它会滚出屏幕并且无法正常工作。

这是我的代码。如果有人有更好的方法,请随时与我分享。

   @Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

// TODO Auto-generated method stub
clickDetector.onTouchEvent(pSceneTouchEvent);
surfaceScrollDetector.onTouchEvent(pSceneTouchEvent);

return true;
}









@Override
public void onScrollStarted(ScrollDetector pScollDetector,
int pPointerID, float pDistanceX, float pDistanceY) {
// TODO Auto-generated method stub
distanceX = 0;

}




@Override
public void onScroll(ScrollDetector pScollDetector, int pPointerID,
float pDistanceX, float pDistanceY) {
// TODO Auto-generated method stub

camera.offsetCenter(-pDistanceX, 0);

distanceX += pDistanceX;
}




@Override
public void onScrollFinished(ScrollDetector pScollDetector,
int pPointerID, float pDistanceX, float pDistanceY) {
if ((distanceX > TURN_PAGE_DISTANCE) && (page > 0))
{

page--;
camera.offsetCenter(distanceX - CAMERA_WIDTH, 0);
}
else if ((distanceX < -TURN_PAGE_DISTANCE) && (page < LEVEL_PAGES - 1))
{

page++;
camera.offsetCenter(distanceX + CAMERA_WIDTH, 0);
}
else
{

camera.offsetCenter(distanceX, 0);
}

}




@Override
public void onClick(ClickDetector pClickDetector, int pPointerID,
float pSceneX, float pSceneY) {
//loadLeve(levelClicked);
}

private void createHUD()
{
// 不用考虑层
hud = new HUD();
Rectangle hudBox = new Rectangle(20, 700, 80, 80, getVertexBufferObjectManager())
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX,
float pTouchAreaLocalY)
{
// TODO Auto-generated method stub
if (pSceneTouchEvent.isActionUp())
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
Toast.makeText(Menu.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
}

return true;
}
};
hudBox.setColor(Color.RED);
hud.attachChild(hudBox);
hud.registerTouchArea(hudBox);
camera.setHUD(hud);
}

@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
if (pKeyCode == KeyEvent.KEYCODE_BACK
&& pEvent.getAction() == KeyEvent.ACTION_DOWN
) {


if(level_scene){
level_scene = false;
camera.setCenter(camera_x, camera_y);
mEngine.setScene(MainMenuScene);
}else{
finish();
}
return true;

}
return super.onKeyDown(pKeyCode, pEvent);




}

private void createLevelBoxes()
{


// 计算行间距
int spaceBetweenRaws = (CAMERA_HEIGHT / LEVEL_ROWS_PER_SCREEN) - LEVEL_PADDING;
// 计算列间距
int spaceBetweenColumns = (CAMERA_WIDTH / LEVEL_COLUMNS_PER_SCREEN) - LEVEL_PADDING;

int level = 0;

int boxX = LEVEL_PADDING;
int boxY = LEVEL_PADDING;

for (int i=0; i<LEVEL_PAGES; i++)
{
int startX = i * CAMERA_WIDTH;

for (int j=0; j<LEVEL_ROWS_PER_SCREEN; j++)
{
for (int k=0; k<LEVEL_COLUMNS_PER_SCREEN; k++)
{
final int levelToLoad = level;
box = new Sprite(startX + boxX, boxY, 90,70,this.levelIcon,this.getVertexBufferObjectManager())
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX,
float pTouchAreaLocalY)
{

// TODO Auto-generated method stub

levelClicked = levelToLoad;
if(levelClicked <= maxLevelReached){

Intent intent = new Intent(Menu.this, GameLevel.class);

intent.putExtra("level", levelClicked);
startActivity(intent);
woodClick.play();
}
return false;
}
};


if (level <= maxLevelReached)
{

}
else
{
box.setColor(0.4117647f, 0.4117647f,0.4117647f);
}

levelScene.attachChild(box);
levelScene.registerTouchArea(box);
int textOffX = 0;
if (level < 10)
{
textOffX = 28;
}
else
{
textOffX = 20;
}
box.attachChild(new Text(textOffX, 20, font, String.valueOf(level + 1),this.getVertexBufferObjectManager()));


level++;
boxX += spaceBetweenColumns + LEVEL_PADDING;
if (level > LEVELS)
{
break;
}
}
if (level > LEVELS)
{
break;
}


boxY += spaceBetweenRaws + LEVEL_PADDING;
boxX = LEVEL_PADDING;
}

boxY = LEVEL_PADDING;

}
}

最佳答案

我认为当用户按下后退键时,您必须再次将相机的 View 范围调整到当前场景。

关于java - 创建可滚动级别选择器(AndEngine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com