2012-02-13 5 views
1

카메라를 차체를 따라 움직이는 데 어려움을 겪고 있습니다. Racer Game 예제 프로젝트를 꾸미고 있습니다. 타일 ​​맵은 1024 x 786이며 카메라는 차체를 추적하도록 설정됩니다. 여기에 코드입니다 :AndEngine Chase 카메라가 본체를 따라 가지 않음

@Override 
    public Scene onCreateScene() { 
     this.mEngine.registerUpdateHandler(new FPSLogger()); 

     this.mScene = new Scene(); 
     //this.mScene.setBackground(new Background(0, 0, 0)); 

     /** Tiled Map Test **/ 
     try { 
      final TMXLoader tmxLoader = new TMXLoader(this.getAssets(), this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, 
        this.getVertexBufferObjectManager(), new ITMXTilePropertiesListener() { 
       @Override 
       public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, 
         final TMXProperties<TMXTileProperty> pTMXTileProperties) { 
        /* We are going to count the tiles that have the property "box=true" or "boxBool=true" set. */ 
        if(pTMXTileProperties.containsTMXProperty("box", "true")) { 
         SpeedsterGameActivity.this.numBoxes++; 
        } 
       } 
      }); 
      // Load the TMX file into an Object 
      this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/level3.tmx"); 

      this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(SpeedsterGameActivity.this, "Box count in this TMXTiledMap: " + SpeedsterGameActivity.this.numBoxes, Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } catch (final TMXLoadException e) { 
      Debug.e(e); 
     } 

     // Get the first TMX Layer and add it to the scene 
     final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0); 
     this.mScene.attachChild(tmxLayer); 

     /* Make the camera not exceed the bounds of the TMXEntity. */ 
     this.mBoundChaseCamera.setBounds(0, 0, tmxLayer.getHeight(), tmxLayer.getWidth()); 
     this.mBoundChaseCamera.setBoundsEnabled(true); 

     /* Debugging stuff */ 
     Debug.i("Game Info", "Height & Width: " + tmxLayer.getHeight() + " x " + tmxLayer.getWidth()); 

     int[] maxTextureSize = new int[1]; 
     GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0); 
     Debug.i("Game Info", "Max texture size = " + maxTextureSize[0]); 
     /**********/ 

     /* Calculate the coordinates for the face, so its centered on the camera. */ 
     final float centerX = (CAMERA_WIDTH - this.mVehiclesTextureRegion.getWidth())/2; 
     final float centerY = (CAMERA_HEIGHT - this.mVehiclesTextureRegion.getHeight())/2; 

     /* Create the sprite and add it to the scene. */ 
     final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mVehiclesTextureRegion, this.getVertexBufferObjectManager()); 
     this.mBoundChaseCamera.setChaseEntity(player); 
     /********************/ 

     this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1); 

     //this.initRacetrack(); 
     //this.initRacetrackBorders(); 

     this.initCar(); 
     this.initObstacles(); 
     this.initOnScreenControls(); 

     this.mScene.registerUpdateHandler(this.mPhysicsWorld); 

} 

답변

2

문제의 원인이 카메라의 크기가 너무 그러므로 전체 카메라 사각형이 표시됩니다에서 1024x786입니다이며 경계를 사용할 수 있기 때문에, 카메라가 차를 수행하지 않습니다.

행을 생략하십시오. this.mBoundChaseCamera.setBoundsEnabled(true);.

또 다른 문제는 카메라가 onCreateScene 실행이 완료되면 참조를 잃어 버린 player 개체를 따릅니다. PhysicsConnector 클래스를 사용하여 player 객체를 물리 본문에 연결하지 않으므로 이동할 이유가 없습니다. 차 몸 & 엔티티가 initCar 방법에 작성하는 경우

그렇지 않으면, 당신은 체이스 엔티티로 차를 설정하지 않습니다.

+0

그래, 분명히 내가 물리 구조 바디를 플레이어 오브젝트에 연결하지 않았어. 그것은 위대한 지금 작동합니다. 감사합니다 –

+0

체이스 카메라는 TMXTiledMap에서만 작동합니까? 나는 쫓는 엔티티를 설정하려고하지만 성공하지 못합니다. – Dharmendra

관련 문제