Search code examples
androidcollision-detectionandengine

AndEngine: Handling collisions with TMX Objects


I managed to load a tmx map now I would like to create the obstacle that the sprite can not move, I recovered the obstacle like this :

try {
        final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, 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 "cactus=true" set. */
                if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
                    //TMXTiledMapExample.this.mCactusCount++;
                    //coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();

                }
            }
        });

How do I handle collisions with obstacles so as to prevent the player from walking through the obstacle (i.e., like a wall)?


Solution

  • since I use this

    if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
                        //TMXTiledMapExample.this.mCactusCount++;
                        //coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
                        //initRacetrackBorders2();
                         // This is our "wall" layer. Create the boxes from it
                            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);
                    }
    

    Have fun !