2012-08-17 73 views
1

我想限制只能在此区域移动精灵对象的区域(例如区域尺寸200x200)。通过toucharea限制要移动精灵的区域

我会创造一个Box2D的200×200,其中精灵可以在此区域

你怎么做,请仅感动?

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

    final Scene scene = new Scene(); 
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f)); 

    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegionLetterOne 
      .getWidth())/2; 
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegionLetterOne 
      .getHeight())/2; 
    final Sprite letterOne = new Sprite(centerX - centerX/2, centerY 
      - centerY/2, this.mFaceTextureRegionLetterOne, 
      this.getVertexBufferObjectManager()) { 
     @Override 
     public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, 
       final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
      this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/2, 
        pSceneTouchEvent.getY() - this.getHeight()/2); 
      return true; 
     } 


    }; 

    final Sprite letterTwo = new Sprite(centerX - centerX/2, centerY, 
      this.mFaceTextureRegionLetterTwo, 
      this.getVertexBufferObjectManager()) { 
     @Override 
     public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, 
       final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
      this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/2, 
        pSceneTouchEvent.getY() - this.getHeight()/2); 
      //int count = scene.getChildCount(); 
      //for(int i = 0; i < count; i++) { 

      IEntity entity = scene.getChildByIndex(1); 
      if (entity instanceof Sprite) { 
       if (entity.getUserData().equals("sprite")) 
        if (((Sprite) entity).collidesWith(letterOne)) 
         Log.v("colission", "face_box is collised on google plus -> letterTwo on letterOne"); 
      } 
      //} 
      return true; 
     } 
    }; 
    letterTwo.setUserData("sprite"); 

    final Sprite boxArea = new Sprite(centerX, centerY, 
      this.mFaceTextureRegionBox, this.getVertexBufferObjectManager()); 
    letterOne.setScale(2); 
    scene.attachChild(letterOne); 
    scene.registerTouchArea(letterOne); 


    letterTwo.setScale(2); 
    scene.attachChild(letterTwo); 

    scene.registerTouchArea(letterTwo); 

    boxArea.setScale(2); 
    scene.attachChild(boxArea); 

    scene.setTouchAreaBindingOnActionDownEnabled(true); 

    return scene; 
} 

谢谢。

回答

0

我不知道box2D,但通常你会检查每个运动精灵的边缘,如果他们不重叠区域的边缘。

如果您的精灵由矩形表示,并且您可以移动的区域也由矩形表示,则可以轻松完成。

但首先,检查Box2D的API,也许它早已一些辅助的方法来缓解这一任务,是这样的:

obect.overlaps(object) 
0

您应该创建的200X200矩形,那么你应该使用创建的主体使用Box2D物理的矩形。对于矩形的夹具定义,您可以设置密度,弹性和摩擦力,以便在与矩形的边界碰撞时相应地处理您的精灵。

要创建矩形,您可以参考Andengine的示例。

0

用你的代码,你似乎试图做你的精灵碰撞检查?!

当你尝试在没有物理机构的情况下做事时,它会变得复杂而且不好。所以让我们用你的精灵来使用物理机构。但请注意,请勿为您的区域(包含您的精灵)创建一个坚实的箱体,让我们使用4个分开的体壁(左,上,右,下)来形成一个封闭的箱子;因为游戏引擎只能用固体形状来检查碰撞。

以下是参考代码:

/** 
* @param pScene 
*  Sence of the game, get from class member 
* @param pWorld 
*  physics world of the game, get from class member 
*/ 
public void CreateSprites(final Scene pScene, final PhysicsWorld pWorld) 
{ 
    final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(10, 1.1f, 0.0f);//should be placed as member of class 
    final Sprite letterTwo = new Sprite(centerX - centerX/2, centerY, 
      this.mFaceTextureRegionLetterTwo, 
      this.getVertexBufferObjectManager()); 

    final Body letterTwoBody = PhysicsFactory.createBoxBody(pWorld, letterTwo, BodyType.DynamicBody, mFixtureDef); 
    letterTwo.setUserData(letterTwoBody); // for later sprite-body attachment access 

    pScene.attachChild(letterTwo); 
    pWorld.registerPhysicsConnector(new PhysicsConnector(letterTwo, letterTwoBody, true, true)); 
} 

/** Create the walls, in these boudaries sprites will move */ 
public void InitBoxWalls(Scene pScene, PhysicsWorld pWorld) 
{ 
    final float WALL_MARGIN_WIDTH = 5f; 
    final float WALL_MARGIN_HEIGHT = 10f; 
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager(); 

    mWallGround = new Rectangle(-WALL_MARGIN_WIDTH, mCameraHeight + WALL_MARGIN_HEIGHT - 2, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager); 
    mWallRoof = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager); 
    mWallLeft = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager); 
    mWallRight = new Rectangle(mCameraWidth + WALL_MARGIN_WIDTH - 2, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager); 

    PhysicsFactory.createBoxBody(pWorld, mWallGround, BodyType.StaticBody, mWallFixtureDef); 
    PhysicsFactory.createBoxBody(pWorld, mWallRoof, BodyType.StaticBody, mWallFixtureDef); 
    PhysicsFactory.createBoxBody(pWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef); 
    PhysicsFactory.createBoxBody(pWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef); 

    pScene.attachChild(mWallGround); 
    pScene.attachChild(mWallRoof); 
    pScene.attachChild(mWallLeft); 
    pScene.attachChild(mWallRight); 
} 

而且你应该做的最后一件事是查找例如物理/ MouseJoint在AndEngine例子。