2014-09-30 30 views
1

我试图设置来自andengine Gles2的TMXmap示例中所有仙人掌上仙人掌属性项目的碰撞检测。我尝试了各种方法 - 任何人都可以给我一个有效的方法吗?andengine gles2中的碰撞检测

原始代码 Tmxmaps andengine

一个建议解决方案: collision detection

另一种建议的解决方案: from andengine.org

我已经试过:

if(pTMXTileProperties.containsTMXProperty("cactus", "true")) { 
    final Rectangle rect = new Rectangle(pTMXTile.getTileX()+10, pTMXTile.getTileY(),14, 14); 
    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f); 
    PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef); 
    rect.setVisible(false); 
    mScene.attachChild(rect); 
} 

这是AndEngine: Handling collisions with TMX Objects

但我得到这个错误:

Physicsfactory not found 

回答

1

我正在使用TMX示例,您有作为我的游戏的基础。

这是代码冲突的主要块:

//定义块行为

mPathFinderMap = new IPathFinderMap<TMXLayer>(){ 

      private boolean mCollide; 

      @Override 
      public boolean isBlocked(final int pX, final int pY, final TMXLayer pTMXLayer) { 
       /* 
       * This is where collisions happen and are detected 
       */ 
       mCollide = false; 
       //Null check. Used since not all tiles have properties 
       if(pTMXLayer.getTMXTile(pX, pY).getTMXTileProperties(mTiledMap) != null){ 
        //Get tiles with collision property 
        if(pTMXLayer.getTMXTile(pX, pY).getTMXTileProperties(mTiledMap).containsTMXProperty("COLLISION", "true")) 
         mCollide = true;           
       } 

       if(mTMXmapLoader.getCollideTiles().contains(pTMXLayer.getTMXTile(pX, pY))) 
        mCollide = true; 

       return mCollide; 
      } 

    }; 

/* 
* This method moves the sprite to the designated location 
*/ 
public void walkTo(TMXTile pFinalPosition) { 
    if(mHasFinishedPath){ 
     mHasFinishedPath = false;//This prevents overlapping paths when the user double clicks. Used to prevent stutter 
     //Player coordinates 
     final float[] lPlayerCordinates = mPlayerSprite.convertLocalToSceneCoordinates(mPlayerSprite.getWidth()/2, mPlayerSprite.getHeight()/2); 
     // Get the tile the center of the player are currently waking on. 
     TMXTile lPlayerPosition = SceneManager.mWorldScene.getTouchLayer().getTMXTileAt(lPlayerCordinates[Constants.VERTEX_INDEX_X], lPlayerCordinates[Constants.VERTEX_INDEX_Y]); 
     mFinalPosition = pFinalPosition; 

     // Sets the A* path from the player location to the touched location. 
     if(mPathFinderMap.isBlocked(pFinalPosition.getTileColumn(), pFinalPosition.getTileRow(), SceneManager.mWorldScene.getTouchLayer())){  
      pFinalPosition = getNextTile(lPlayerPosition, pFinalPosition); 
     } 

     // These are the parameters used to determine the 
     int lFromCol = lPlayerPosition.getTileColumn(); int lFromRow = lPlayerPosition.getTileRow(); 
     int lToCol = pFinalPosition.getTileColumn(); int lToRow = pFinalPosition.getTileRow(); 
     boolean lAllowDiagonal = false; 
     // Find the path. This needs to be refreshed 
     AStarPath = mAStarPathFinder.findPath(MAX_SEARCH_DEPTH, mPathFinderMap, 0, 0, mTiledMap.getTileColumns() - 1, mTiledMap.getTileRows() - 1, SceneManager.mWorldScene.getTouchLayer(), 
       lFromCol, lFromRow, lToCol, lToRow, lAllowDiagonal, mHeuristic, mCostCallback); 

     //Log.i("AstarPath", "AStarPath " + AStarPath); 
     //Only loads the path if the AStarPath is not null 
     Path lPlayerPath = loadPathFound(); 
     //Log.i("AstarPath", "lPlayerPath " + lPlayerPath); 
     if(lPlayerPath != null) 
      moveSprite(lPlayerPath);//Moves the sprite along the path 
     else 
      mHasFinishedPath = true;//If the path is null the player has not moved. Set the flag to true allows input to effect the sprite 
    }else{ 
     //Update parameters 
     mFinalPosition = pFinalPosition; 
     mWaypointIndex = 0; 
    } 
} 

/* 
* Updates the path 
*/ 
public void updatePath(TMXTile pFinalPosition) {  
    //Player coordinates 
    final float[] lPlayerCordinates = mPlayerSprite.convertLocalToSceneCoordinates(mPlayerSprite.getWidth()/2, mPlayerSprite.getHeight()/2); 
    // Get the tile the feet of the player are currently waking on. 
    TMXTile lPlayerPosition = SceneManager.mWorldScene.getTouchLayer().getTMXTileAt(lPlayerCordinates[Constants.VERTEX_INDEX_X], lPlayerCordinates[Constants.VERTEX_INDEX_Y]); 

    // Sets the A* path from the player location to the touched location. 
    if(mPathFinderMap.isBlocked(pFinalPosition.getTileColumn(), pFinalPosition.getTileRow(), SceneManager.mWorldScene.getTouchLayer())){  
     pFinalPosition = getNextTile(lPlayerPosition, pFinalPosition); 
    } 

    // Determine the tile locations 
    int FromCol = lPlayerPosition.getTileColumn(); 
    int FromRow = lPlayerPosition.getTileRow(); 
    int ToCol = pFinalPosition.getTileColumn(); 
    int ToRow = pFinalPosition.getTileRow(); 
    // Find the path. This needs to be refreshed 
    AStarPath = mAStarPathFinder.findPath(MAX_SEARCH_DEPTH, mPathFinderMap, 0, 0, mTiledMap.getTileColumns()-1, mTiledMap.getTileRows()-1, SceneManager.mWorldScene.getTouchLayer(), 
      FromCol, FromRow, ToCol, ToRow, false, mHeuristic, mCostCallback); 

    //Loads the path with the astar specifications 
    Path lPlayerPath = loadPathFound(); 
    //Moves the sprite along the path 
    if(lPlayerPath != null){ 
     moveSprite(lPlayerPath); 
    }else{ 
     //If the path is still null after the path manipulation then the path is finished 
     mHasFinishedPath = true; 
     mWaypointIndex = 0; 
     //mPlayerSprite.stopAnimation(); 
     //AStarPath = null; 
    } 
} 

的TMXmapLoader没有休息:

//Get the collision, ext, and changing tiles from the object sets on the map 
    mCollideTiles = this.getObjectGroupPropertyTiles("COLLIDE", TMXGroupObjects); 
    mExitTiles = this.getObjectPropertyTiles("EXIT", mTMXObjects); 
    mChangingTiles = this.getObjectGroupPropertyTiles("CHANGE", TMXGroupObjects); 
... 
public ArrayList<TMXTile> getCollideTiles(){ 
    return mCollideTiles;  
} 
... 
public ArrayList<TMXTile> getObjectGroupPropertyTiles(String pName, final int pLayer, ArrayList<TMXObjectGroup> pTMXObjectGroups){ 
    ArrayList<TMXTile> ObjectTile = new ArrayList<TMXTile>(); 
    for (final TMXObjectGroup pObjectGroups : pTMXObjectGroups) { 
     // Iterates through the properties and assigns them to the new variable 
     for (final TMXObjectGroupProperty pGroupProperties : pObjectGroups.getTMXObjectGroupProperties()) { 
      //Sees if any of the elements have this condition 
      if (pGroupProperties.getName().contains(pName)) { 
       for (final TMXObject pObjectTiles : pObjectGroups.getTMXObjects()) { 
        int ObjectX = pObjectTiles.getX(); 
        int ObjectY = pObjectTiles.getY(); 
        // Gets the number of rows and columns in the object 
        int ObjectRows = pObjectTiles.getHeight()/WorldActivity.TILE_HEIGHT; 
        int ObjectColumns = pObjectTiles.getWidth()/WorldActivity.TILE_WIDTH; 

        for (int TileRow = 0; TileRow < ObjectRows; TileRow++) { 
         for (int TileColumn = 0; TileColumn < ObjectColumns; TileColumn++) { 
          float lObjectTileX = ObjectX + TileColumn * WorldActivity.TILE_WIDTH; 
          float lObjectTileY = ObjectY + TileRow * WorldActivity.TILE_HEIGHT; 
          ObjectTile.add(mTMXTiledMap.getTMXLayers().get(pLayer).getTMXTileAt(lObjectTileX, lObjectTileY));      
         }        
        } 
       } 
      }   
     } 
    } 
    return ObjectTile; 
} 
+0

嘿,你基本上是做一个特殊瓷砖的坐标数组,并在他们创建矩形对象,以处理碰撞? – 2014-10-03 04:55:00

+0

种。是的,特殊瓷砖的坐标数组。矩形?没有。在这个代码中实际上没有box2d物理。您可以使用isBlocked()来确定目标图块是否为碰撞对象。但是一旦建立起来,你就可以在Tiled中创建一个碰撞层,并且代码将在玩家四处走动时处理这些瓦片的碰撞。 – 2014-10-03 05:08:33

+0

你有这样一个工作的例子,我可以下载/结帐 – 2014-10-03 14:08:15

0

我不熟悉Android的发展,但这个错误似乎表明PhysicsFactory尚未导入。也许尝试添加像这样的导入语句到您的文件的顶部?

import org.anddev.andengine.extension.physics.box2d.PhysicsFactory; 
+0

当我将测试这一点家 – 2014-09-30 19:05:18