title: Creating A Scrollable Level Selector(AndEngine)
body: <p>i am having troubles with my level selector. i am using the code i found on a tutorial on this forum but it isnt working correct.</p>
  
  <p>The fact is i have two scene's i am using when the user mashes play i set the engine to the second scene and then the user can scroll through the levels.</p>
  
  <p>Then when the user presses the back button i set the camera back to its initial position by saving the position before i leave and then setting it back using camer.setCenter(x,y);</p>
  
  <p>Once the user then goes back to the leve selector scene it doesnt worry correctly. When the user scrolls it scrolls off the screen and doesn't work as it shoud.</p>
  
  <p>Here is my code. If some one has a better way PLEASE feel free to share with me.</p>
  
  <pre><code>   @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 &gt; TURN_PAGE_DISTANCE) &amp;&amp; (page &gt; 0))
                   {
  
                           page--;
                           camera.offsetCenter(distanceX - CAMERA_WIDTH, 0);
                   }
                   else if ((distanceX &lt; -TURN_PAGE_DISTANCE) &amp;&amp; (page &lt; 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
                              &amp;&amp; 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&lt;LEVEL_PAGES; i++)
              {
                      int startX = i * CAMERA_WIDTH;
  
                      for (int j=0; j&lt;LEVEL_ROWS_PER_SCREEN; j++)
                      {
                              for (int k=0; k&lt;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 &lt;= maxLevelReached){
  
                                                                 Intent intent = new Intent(Menu.this, GameLevel.class);
  
                                                              intent.putExtra("level", levelClicked);
                                                              startActivity(intent);
                                                              woodClick.play();
                                                             }
                                                      return false;
                                              }
                                      };
  
  
                                      if (level &lt;= maxLevelReached)
                                      {
  
                                      }
                                      else
                                      {
                                        box.setColor(0.4117647f, 0.4117647f,0.4117647f);      
                                      }
  
                                      levelScene.attachChild(box);
                                      levelScene.registerTouchArea(box);
                                      int textOffX = 0;
                                      if (level &lt; 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 &gt; LEVELS)
                                      {
                                              break;
                                      }
                              }
                              if (level &gt; LEVELS)
                              {
                                      break;
                              }
  
  
                              boxY += spaceBetweenRaws + LEVEL_PADDING;
                              boxX = LEVEL_PADDING;
                      }
  
                      boxY = LEVEL_PADDING;
  
              }
      }
  </code></pre>