AndEngine:处理碰撞折线TMX地图

时间:2015-05-18 10:24:44

标签: android andengine

我正在使用TMX地图中的Tiled地图在andEngine中开发游戏 如何检查碰撞折线() 我检查与sucuess的碰撞。但折线,我不知道碰撞。 可以帮助我。

是map.tmx:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="15" height="15" tilewidth="32" tileheight="32" nextobjectid="13">
 <tileset firstgid="1" name="images" tilewidth="32" tileheight="32">
  <image source="images.png" width="512" height="384"/>
 </tileset>
 <layer name="Tile Layer 1" width="15" height="15">
  <data encoding="base64" compression="gzip">
   H4sIAAAAAAAAC2NiYGBgGsWjeBQPGAYASiKnxoQDAAA=
  </data>
 </layer>
 <layer name="Tile Layer 2" width="15" height="15">
  <data encoding="base64" compression="gzip">
   H4sIAAAAAAAAC2NgGFggCsRiSPzVQLwGjzwyUAViNST+biDeg0eeXqAQiIvIkAOBRiBuIkOOlqAUiMuQ+Ohx5ArEbjj0tgJxGxIfPY5CgTiMCm7EBqSBmAcHlqGRneQAAE0g83SEAwAA
  </data>
 </layer>
 <objectgroup name="Object Layer 1" width="15" height="15">
  <properties>
   <property name="wall" value="true"/>
  </properties>
  <object id="1" x="69" y="68" width="54" height="50"/>
  <object id="2" x="326" y="69" width="53" height="49"/>
  <object id="3" x="328" y="293" width="50" height="48"/>
  <object id="4" x="70" y="293" width="53" height="48"/>
  <object id="5" x="111" y="392" width="228" height="15"/>
  <object id="8" x="69" y="199" width="49" height="50"/>
  <object id="12" x="192" y="127">
   <polyline points="0,0 61,125 179,112 230,149"/>
  </object>
 </objectgroup>
</map>

// --------------------碰撞---------------------

   private void createUnwalkableObjects(TMXTiledMap map){
        // Loop through the object groups
        for(final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {
            if(group.getTMXObjectGroupProperties().containsTMXProperty("wall", "true")){
                // This is our "wall" layer. Create the boxes from it
                for(final TMXObject object : group.getTMXObjects()) {
                    final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
                    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
                    PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                    rect.setVisible(false);
                    mScene.attachChild(rect);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

你可以查看:https://github.com/buivanlien/DemoImportAndengine 和文件* /

public class TMXObject implements TMXConstants {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    private final String mName;
    private final String mType;
    private final int mX;
    private final int mY;
    private final int mWidth;
    private final int mHeight;
    private Vector2[] mPolyLines;
    private final TMXProperties<TMXObjectProperty> mTMXObjectProperties = new TMXProperties<TMXObjectProperty>();

    // ===========================================================
    // Constructors
    // ===========================================================

    public TMXObject(final Attributes pAttributes) {
        this.mName = pAttributes.getValue("",
                TMXConstants.TAG_OBJECT_ATTRIBUTE_NAME);
        this.mType = pAttributes.getValue("",
                TMXConstants.TAG_OBJECT_ATTRIBUTE_TYPE);
        this.mX = SAXUtils.getIntAttributeOrThrow(pAttributes,
                TMXConstants.TAG_OBJECT_ATTRIBUTE_X);
        this.mY = SAXUtils.getIntAttributeOrThrow(pAttributes,
                TMXConstants.TAG_OBJECT_ATTRIBUTE_Y);
        this.mWidth = SAXUtils.getIntAttribute(pAttributes,
                TMXConstants.TAG_OBJECT_ATTRIBUTE_WIDTH, 0);
        this.mHeight = SAXUtils.getIntAttribute(pAttributes,
                TMXConstants.TAG_OBJECT_ATTRIBUTE_HEIGHT, 0);
        this.mPolyLines = new Vector2[]{};
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public Vector2[] getmPolyLines() {
        return mPolyLines;
    }

    public int[][] getmPolyLines(float pixelToMeterRatio) {
        Log.v("FEBI2",String.valueOf(this.getmPolyLines().length));
        int[][] polyLines = new int[this.getmPolyLines().length][2];
        for (int i = 0; i < this.getmPolyLines().length; i++) {

            polyLines[i][0] = (int) (this.getmPolyLines()[i].x
                    / pixelToMeterRatio);
            polyLines[i][1] = (int) (this.getmPolyLines()[i].y
                    / pixelToMeterRatio);

        }
        return polyLines;
    }

    public void setmPolyLines(Vector2[] mPolyLines) {
        this.mPolyLines = mPolyLines;
    }

    public String getName() {
        return this.mName;
    }

    public String getType() {
        return this.mType;
    }

    public int getX() {
        return this.mX;
    }

    public int getY() {
        return this.mY;
    }

    public int getWidth() {
        return this.mWidth;
    }

    public int getHeight() {
        return this.mHeight;
    }

    public void addTMXObjectProperty(final TMXObjectProperty pTMXObjectProperty) {
        this.mTMXObjectProperties.add(pTMXObjectProperty);
    }

    public TMXProperties<TMXObjectProperty> getTMXObjectProperties() {
        return this.mTMXObjectProperties;
    }

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

获取Polyline:

 // This is our "wall" layer. Create the boxes from it
                for (final TMXObject object : group.getTMXObjects()) {
                    //add polyline
                    float []mangX = new float[object.getmPolyLines().length];
                    float []mangY = new float[object.getmPolyLines().length];
                    for (int i = 0; i < object.getmPolyLines().length; i++) {

                        mangX[i] = (float) (object.getmPolyLines()[i].x);
                        mangY[i] = (float) (object.getmPolyLines()[i].y);
                        Rectangle polyLine = new Rectangle(object.getX(),object.getY(),mangX[i],mangY[i],getVertexBufferObjectManager());

                        final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0,
                                                                                         0,
                                                                                         1f);
                        PhysicsFactory.createBoxBody(this.mPhysicsWorld,
                                                     polyLine,
                                                     BodyDef.BodyType.StaticBody,
                                                     boxFixtureDef);
                        polyLine.setVisible(false);
                        scene.attachChild(polyLine);

                    }