使用AndEngine未正确显示TMX文件

时间:2014-07-02 12:26:29

标签: android andengine tmx

我已经为室内定位编写了位置定位器应用程序。我已经使用TMX文件来渲染我的楼层地图并使用AndEngine加载它们。问题是该程序没有正确显示TMX文件。

这是原始文件: enter image description here

以下是我的应用程序显示的内容: enter image description here

我该如何解决这个问题?这是代码:

public class TiledViewer extends SimpleBaseGameActivity implements
    IOnSceneTouchListener, IScrollDetectorListener,
    IPinchZoomDetectorListener {

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private SmoothCamera camera;

private TMXTiledMap mTMXTiledMap;

private SurfaceScrollDetector mScrollDetector;
private PinchZoomDetector mPinchZoomDetector;
private float mPinchZoomStartedCameraZoomFactor;

@Override
public EngineOptions onCreateEngineOptions() {
    this.camera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 400,
            400, 10f);
    final CroppedResolutionPolicy canvasSurface = new CroppedResolutionPolicy(
            CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true,
            ScreenOrientation.PORTRAIT_FIXED, canvasSurface, this.camera);

    if (MultiTouch.isSupported(this)) {
        if (MultiTouch.isSupportedDistinct(this)) {
            Toast.makeText(
                    this,
                    "MultiTouch detected --> Both controls will work properly!",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(
                    this,
                    "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.",
                    Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(
                this,
                "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.",
                Toast.LENGTH_LONG).show();
    }

    return engineOptions;
}

@Override
public void onCreateResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
}

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

    final Scene scene = new Scene();
    scene.setOnAreaTouchTraversalFrontToBack();

    this.mScrollDetector = new SurfaceScrollDetector(this);
    this.mPinchZoomDetector = new PinchZoomDetector(this);

    scene.setOnSceneTouchListener(this);
    scene.setTouchAreaBindingOnActionDownEnabled(true);

    try {
        final TMXLoader tmxLoader = new TMXLoader(this.getAssets(),
                this.mEngine.getTextureManager(), TextureOptions.DEFAULT,
                this.getVertexBufferObjectManager(),
                new ITMXTilePropertiesListener() {
                    @Override
                    public void onTMXTileWithPropertiesCreated(
                            final TMXTiledMap pTMXTiledMap,
                            final TMXLayer pTMXLayer,
                            final TMXTile pTMXTile,
                            final TMXProperties<TMXTileProperty> pTMXTileProperties) {
                    }
                });
        this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/m03.tmx");

    } catch (final TMXLoadException e) {
        Debug.e(e);
    }

    final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
    scene.attachChild(tmxLayer);

    this.camera.setBounds(0, 0, tmxLayer.getHeight(), tmxLayer.getWidth());
    this.camera.setBoundsEnabled(true);

    return scene;
}

@Override
public void onScrollStarted(final ScrollDetector pScollDetector,
        final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY
            / zoomFactor);
}

@Override
public void onScroll(final ScrollDetector pScollDetector,
        final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY
            / zoomFactor);
}

@Override
public void onScrollFinished(final ScrollDetector pScollDetector,
        final int pPointerID, final float pDistanceX, final float pDistanceY) {
    final float zoomFactor = this.camera.getZoomFactor();
    this.camera.offsetCenter(-pDistanceX / zoomFactor, -pDistanceY
            / zoomFactor);
}

@Override
public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent) {
    this.mPinchZoomStartedCameraZoomFactor = this.camera.getZoomFactor();
}

@Override
public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent, final float pZoomFactor) {
    this.camera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor
            * pZoomFactor);
}

@Override
public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector,
        final TouchEvent pTouchEvent, final float pZoomFactor) {
    this.camera.setZoomFactor(this.mPinchZoomStartedCameraZoomFactor
            * pZoomFactor);
}

@Override
public boolean onSceneTouchEvent(final Scene pScene,
        final TouchEvent pSceneTouchEvent) {
    this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

    if (this.mPinchZoomDetector.isZooming()) {
        this.mScrollDetector.setEnabled(false);
    } else {
        if (pSceneTouchEvent.isActionDown()) {
            this.mScrollDetector.setEnabled(true);
        }
        this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
    }

    return true;
}
}

1 个答案:

答案 0 :(得分:0)

我在实现室内计划方面遇到了同样的问题。 我找到了一种通过直接编辑.tmx文件来改善显示效果的方法。 在tileset中,您应该添加这些属性间距和边距。 例如:     tileset spacing =&#34; 3&#34;余量=&#34; 2&#34; 如果你把好的值放在不同的字段中,你应该有一个更好的显示。

我的问题只是通过在此tileset中添加margin = 10来解决。

我希望能帮到你,