2014-09-30 4 views
1

andengine Gles2의 TMXmap 예제에서 모든 선인장의 cactus-property 항목에 충돌 감지를 설정하려고합니다. 저는 다양한 방법을 시도해 왔습니다.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

이봐 요, 그래서 당신은 기본적으로 특수 타일의 좌표 배열을 만들고 충돌을 처리하기 위해 사각형 위에 오브젝트를 생성합니까? –

+0

종류입니다. 특수 타일의 좌표 배열입니다. 직사각형? 아니. 실제로이 코드에는 box2d 물리가 전혀 없습니다. isBlocked()를 사용하여 대상 타일이 충돌 객체인지 알아낼 수 있습니다. 그러나 일단 설정되면 타일링에서 충돌 레이어를 만들 수 있으며 코드는 플레이어가 걷는 동안 타일의 충돌을 처리합니다. –

+0

실례가 있습니다. 다운로드/체크 아웃 –

0

나는 안드로이드 개발에 익숙하지 않은 해요,하지만 오류가 PhysicsFactory가 수입되지 않았 음을 나타내는 것으로 보인다. 어쩌면이 파일과 같은 import 문을 파일의 맨 위에 추가 해볼까요?

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

내가 할 때이 테스트됩니다

이 충돌에 대한 코드의 메인 블록이다 home –