为什么Player在AndEngine中没有显示TMX文件?

时间:2015-11-22 06:09:50

标签: java andengine tmx

我正在使用AndEngine GLES2-AnchorCenter开发一款2d平台游戏......我有6级关卡,任何级别都有Tmx文件(Tiled)......我可以加载TMX文件但播放器未显示在屏幕上...我希望玩家在第一个平台上,然后在另一个平台上跳,但我不知道该怎么做!?

这是我的代码...... 请帮我。 感谢...

TMX文件:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="500" height="50" tilewidth="20" tileheight="20" nextobjectid="16">
<tileset firstgid="1" name="map_tiles" tilewidth="20" tileheight="20" tilecount="48">
<image source="gfx/game/map_tiles.png" width="128" height="160"/>
</tileset>
<layer name="Tile Layer 1" width="500" height="50">
<data encoding="base64" compression="gzip">
   H4sIAAAAAAAAC+3YOxLCMAxAQU8qE44AZwDuf7qoYUhpM/nIky22tjrpeSqlTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEm8wjt8EswCAAAAnMcfAQAAAPzoZACglbsBAAAA+mhpAODLXQAAAADbaelsLQ4AZFbDLczhnmAeANjKaD1uJwMAAMA+NDcAjMHOBgAycJMAcCVH7r26sn6zNrCbAQAAGJ2+BQAAgP3pbwAAADiGBgcAAAAAgH7+1wEAAAAA4D+P8EwwBwAAAFyJHgcAAAAAgH7+1wEAAOA8C3BZGU2ghgEA
</data>
</layer>
<objectgroup name="Object Layer 1" width="500" height="50">
<properties>
<property name="sp" value="true"/>
</properties>
<object id="1" x="19" y="280" width="61" height="20"/>
<object id="2" x="80" y="361" width="60" height="20"/>
<object id="13" x="419" y="760"/>
</objectgroup>
<objectgroup name="Object Layer 2" width="500" height="50">
<properties>
<property name="su" value="true"/>
</properties>
<object id="6" x="441" y="479" width="59" height="21"/>
<object id="7" x="541" y="439" width="59" height="21"/>
</objectgroup>
</map>

播放器:

public abstract class Player extends AnimatedSprite {

// ---------------------------------------------
// VARIABLES
// ---------------------------------------------

public Body body;

public boolean canRun = false;

private int footContacts = 0;

// ---------------------------------------------
// CONSTRUCTOR
// ---------------------------------------------

public Player(float pX, float pY, VertexBufferObjectManager vbo,
        Camera camera, PhysicsWorld physicsWorld) {
    super(pX, pY, ResourceManager.getInstance().player_region, vbo);
    createPhysics(camera, physicsWorld);
    camera.setChaseEntity(this);
}

// ---------------------------------------------
// CLASS LOGIC
// ---------------------------------------------

private void createPhysics(final Camera camera, PhysicsWorld physicsWorld) {
    body = PhysicsFactory.createBoxBody(physicsWorld, this,
            BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, 0));

    body.setUserData("player");
    body.setFixedRotation(true);

    physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body,
            true, false) {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            super.onUpdate(pSecondsElapsed);
            camera.onUpdate(0.1f);

            if (getY() <= 0) {
                onDie();
            }

            if (canRun) {
                body.setLinearVelocity(new Vector2(5, body
                        .getLinearVelocity().y));
            }
        }
    });
}

public void setRunning() {
    canRun = true;

    final long[] PLAYER_ANIMATE = new long[] { 100, 100, 100 };

    animate(PLAYER_ANIMATE, 0, 2, true);
}

public void jump() {
    if (footContacts < 1) {
        return;
    }
    body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 11));
}

public void increaseFootContacts() {
    footContacts++;
}

public void decreaseFootContacts() {
    footContacts--;
}

public abstract void onDie();

}

GameScene

    public void loadLevel(int levelID) {

    try {
        mTmxLoader = new TMXLoader(activity.getAssets(),
                engine.getTextureManager(), TextureOptions.NEAREST, vbom);
        mTMXTiledMap = mTmxLoader.loadFromAsset("tmx/level" + levelID
                + ".tmx");
    } catch (final TMXLoadException tmxle) {
        Debug.e(tmxle);
    }
    createObjects(mTMXTiledMap);
    final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
    mTMXTiledMap.detachChild(tmxLayer);
    this.attachChild(tmxLayer);

    /* Make the camera not exceed the bounds of the TMXEntity. */
    resourceManager.camera.setBounds(0, 0, tmxLayer.getHeight(),
            tmxLayer.getWidth());
    resourceManager.camera.setBoundsEnabled(true);

    /*
     * Calculate the coordinates for the face, so its centered on the
     * camera.
     */
    final float centerX = (resourceManager.CAMERA_WIDTH - resourceManager.player_region
            .getWidth()) / 2;
    final float centerY = (resourceManager.CAMERA_HEIGHT - resourceManager.player_region
            .getHeight()) / 2;

    /* Create the sprite and add it to the scene. */
    player = new Player(centerX, centerY, vbom, camera, physicsWorld) {

        @Override
        public void onDie() {
            // TODO Auto-generated method stub
            // if (!gameOverDisplayed
            // && playerDie == false) {
            //
            // displayGameOverText();
            // }
        }
    };

    this.attachChild(player);

}

    public void createObjects(TMXTiledMap map) {

    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0,
            0.01f, 0.5f);

    // Loop through the object groups
    for (final TMXObjectGroup group : map.getTMXObjectGroups()) {
        if (group.getTMXObjectGroupProperties().containsTMXProperty(
                "sp", "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(), vbom);

                final Body body = PhysicsFactory.createBoxBody(
                        physicsWorld, rect, BodyType.StaticBody,
                        boxFixtureDef);
                rect.setVisible(true);
                this.attachChild(rect);
                body.setUserData("sp");
            }
        } else if (group.getTMXObjectGroupProperties().containsTMXProperty(
                "su", "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(), vbom);

                final Body body = PhysicsFactory.createBoxBody(
                        physicsWorld, rect, BodyType.StaticBody,
                        boxFixtureDef);
                rect.setVisible(true);
                this.attachChild(rect);
                body.setUserData("su");
            }
        } else if (group.getTMXObjectGroupProperties().containsTMXProperty(
                "fa", "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(), vbom);

                final Body body = PhysicsFactory.createBoxBody(
                        physicsWorld, rect, BodyType.StaticBody,
                        boxFixtureDef);
                rect.setVisible(true);
                this.attachChild(rect);
                body.setUserData("fa");
            }
        } else if (group.getTMXObjectGroupProperties().containsTMXProperty(
                "win", "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(), vbom);

                final Body body = PhysicsFactory.createBoxBody(
                        physicsWorld, rect, BodyType.StaticBody,
                        boxFixtureDef);
                rect.setVisible(true);
                this.attachChild(rect);
                body.setUserData("win");
            }
        }

    }

}

    private ContactListener contactListener() {
    ContactListener contactListener = new ContactListener() {
        public void beginContact(Contact contact) {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null
                    && x2.getBody().getUserData() != null) {
                if (x2.getBody().getUserData().equals("player")) {
                    player.increaseFootContacts();
                }

                if (x1.getBody().getUserData().equals("su")
                        && x2.getBody().getUserData().equals("player")) {
                    engine.registerUpdateHandler(new TimerHandler(0.4f,
                            new ITimerCallback() {
                                public void onTimePassed(
                                        final TimerHandler pTimerHandler) {
                                    pTimerHandler.reset();
                                    engine.unregisterUpdateHandler(pTimerHandler);
                                    x1.getBody().setType(
                                            BodyType.DynamicBody);
                                }
                            }));
                }
                if (x1.getBody().getUserData().equals("fa")
                        && x2.getBody().getUserData().equals("player")) {
                    engine.registerUpdateHandler(new TimerHandler(0.2f,
                            new ITimerCallback() {
                                public void onTimePassed(
                                        final TimerHandler pTimerHandler) {
                                    pTimerHandler.reset();
                                    engine.unregisterUpdateHandler(pTimerHandler);
                                    x1.getBody().setType(
                                            BodyType.DynamicBody);
                                }
                            }));
                }

                if (x1.getBody().getUserData().equals("wi")
                        && x2.getBody().getUserData().equals("player")) {

                    engine.registerUpdateHandler(new TimerHandler(0.1f,
                            new ITimerCallback() {
                                public void onTimePassed(
                                        final TimerHandler pTimerHandler) {
                                    pTimerHandler.reset();
                                    engine.unregisterUpdateHandler(pTimerHandler);
                                    x1.getBody().setType(
                                            BodyType.DynamicBody);
                                }
                            }));
                }
                if (x1.getBody().getUserData().equals("levelDeadEnd")
                        && x2.getBody().getUserData().equals("player")) {
                    x2.getBody().setType(BodyType.StaticBody);
                }
            }
        }

        public void endContact(Contact contact) {
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null
                    && x2.getBody().getUserData() != null) {
                if (x2.getBody().getUserData().equals("player")) {
                    player.decreaseFootContacts();
                }
            }
        }

        public void preSolve(Contact contact, Manifold oldManifold) {

        }

        public void postSolve(Contact contact, ContactImpulse impulse) {

        }
    };
    return contactListener;
}

0 个答案:

没有答案