• AndEngine学习:LevelLoaderExample(加载关卡)


      我们写游戏的时候需要有地图,比如写迷宫游戏,地图就占了游戏数据的一大部分,AndEngine给我们提供了一个非常实用的类:LevelLoader。我们直接看一个例子。

      首先在assets目录下新建一个目录,取名为level,该目录下我们要存储关卡数据。然后新建文件:example.lvl,输入以下数据:

    <?xml version="1.0" encoding="utf-8"?>
    <level width="480" height="320">
    <entity x="0" y="0" width="16" height="16" type="rectangle"/>
    <entity x="100" y="0" width="16" height="16" type="rectangle"/>
    <entity x="200" y="0" width="16" height="16" type="line"/>
    </level>

      然后我们直接看程序:

    public class LevelLoaderTestActivity extends BaseGameActivity
    {
    // Constants
    private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 320;

    private static final String TAG_ENTITY = "entity";
    private static final String TAG_ENTITY_ATTRIBUTE_X = "x";
    private static final String TAG_ENTITY_ATTRIBUTE_Y = "y";
    private static final String TAG_ENTITY_ATTRIBUTE_WIDTH = "width";
    private static final String TAG_ENTITY_ATTRIBUTE_HEIGHT = "height";
    private static final String TAG_ENTITY_ATTRIBUTE_TYPE = "type";

    private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_RECTANGLE = "rectangle";

    @Override
    public Engine onLoadEngine()
    {
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
    new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));

    }

    @Override
    public void onLoadResources()
    {

    }

    @Override
    public Scene onLoadScene()
    {
    final Scene scene = new Scene();
    scene.setBackground(new ColorBackground(0, 0, 0));

    final LevelLoader levelLoader = new LevelLoader();
    levelLoader.setAssetBasePath("level/");

    levelLoader.registerEntityLoader(LevelConstants.TAG_LEVEL, new IEntityLoader()
    {

    @Override
    public void onLoadEntity(String pEntityName, Attributes pAttributes)
    {
    final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_WIDTH);
    final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_HEIGHT);
    Toast.makeText(LevelLoaderTestActivity.this, "Loaded level with width=" + width + " and height=" + height + ".", Toast.LENGTH_LONG).show();
    }});

    levelLoader.registerEntityLoader(TAG_ENTITY, new IEntityLoader()
    {

    @Override
    public void onLoadEntity(String pEntityName, Attributes pAttributes)
    {
    final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
    final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
    final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_WIDTH);
    final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_HEIGHT);
    final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE);

    addToMap(scene, x, y, width, height, type);

    }});

    try {
    levelLoader.loadLevelFromAsset(this, "example.lvl");
    } catch (final IOException e) {
    Debug.e(e);
    }

    return scene;
    }

    private void addToMap(Scene scene, int x, int y, int width, int height, String type)
    {
    if(type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_RECTANGLE))
    {
    Rectangle rectangle = new Rectangle(x, y, width, height);
    scene.attachChild(rectangle);
    }
    }

    @Override
    public void onLoadComplete()
    {

    }

    }

      程序依然很简单,就不一句一句地解释了,有问题请留言。

  • 相关阅读:
    poj 1321 棋盘问题 (dfs)
    poj 3274 Gold Balanced Lineup(哈希 )
    poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
    zoj 3351 Bloodsucker(概率 dp)
    poj 1840 Eqs (hash)
    poj 2418 Hardwood Species (map)
    poj 2151 Check the difficulty of problems(概率dp)
    poj 2442 Sequence(优先队列)
    poj 1442 Black Box(堆 优先队列)
    两个STL网址 总结的很好 && c++堆的网址
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2229548.html
Copyright © 2020-2023  润新知