2013-09-27 34 views
0

我正在開發andengine應用程序。在這我插入的精靈用戶點擊並使用小矩形繪製一個圓圈。我通過循環創建了許多矩形,並將它們排列在一個圓的路徑上,因此它看起來像一個圓圈。但問題是屏幕響應緩慢,看起來像掛。任何人都可以建議我如何使它更平滑。 這是我的代碼。android andengine現場很慢,因爲很多孩子都喜歡它嗎?

//變量

private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 


    private BitmapTextureAtlas mBitmapTextureAtlas; 

    private TiledTextureRegion mBoxFaceTextureRegion; 

    private Scene mScene; 

    private PhysicsWorld mPhysicsWorld; 

//現場方法

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

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

     this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); 

     final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager(); 
     final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 
     final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 

     final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef); 

     this.mScene.attachChild(ground); 
     this.mScene.attachChild(roof); 
     this.mScene.attachChild(left); 
     this.mScene.attachChild(right); 
     drawLine(); 
     this.mScene.registerUpdateHandler(this.mPhysicsWorld); 

     return this.mScene; 
    } 

//在DrawLine的方法:

private void drawLine() { 


     float x1,y1,x2=1,y2=1; 
     float r=100; 
     float xCenter=300,yCenter=300; 
     Rectangle rectTest; 

     for(double i=0;i<5;i+=.01){ 
       x1 = (float) (xCenter + r * Math.cos(i)); 
       y1 = (float) (yCenter + r * Math.sin(i)); 

       System.out.println("RECT x1="+x1); 
       System.out.println("RECT y1="+y1); 
       System.out.println("RECT x2="+x2); 
       System.out.println("RECT y3="+y2); 

      rectTest = new Rectangle(x1,y1,1,1,getVertexBufferObjectManager()); 

      rectTest.setColor(Color.RED); 
      FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 
        0.1f, 0.1f); 
      PhysicsFactory.createBoxBody(this.mPhysicsWorld, rectTest, 
        BodyType.StaticBody, wallFixtureDef); 
      this.mScene.attachChild(rectTest); 

     } 
} 

回答

1

這個問題可能是你正在創建500個動感的車身對象使用Box2D。 Box2D或任何物理引擎在移動平臺上的成本相對較高。模擬500個碰撞物體正在推動移動處理器可以處理的極限。嘗試使用50個對象來定義您的圈子。

+0

感謝鱘魚的快速反應。其實我試圖畫出一個圓圈,並具有檢測碰撞和牆體屬性(如矩形)的功能。因此,我可以檢測到球與牆的碰撞。所以我決定用矩形繪製一個圓。有沒有辦法做同樣的任務。請建議。我被卡住了。 – puneetbhardwaj

+0

我不確切地知道你在描述中想要做什麼,但我會建議2件事:1 - 在Box2D中可以使用圓形。和2:你可以使用一個叫做PHYSICS EDITOR程序的程序從一個PNG圖像爲box2d項目生成任何形狀多邊形:我已經將它用於我自己的項目並強烈推薦它。 http://www.codeandweb.com/physicseditor –

+0

是否可用於Ubuntu? – puneetbhardwaj