Android笔记--Libgdx游戏框架之TiledMap地图Obeject的读取

TieldMap中除了地图图块层意外,还有对象层,在对象层中,你可以创建常见几何形状的对象,并赋予自定义属性值。包括矩形对象,多边形对象,圆/椭圆对象,折线等等。

首先我们创建一个地图,并添加一定的对象,这里我们使用了45度斜地图,并添加了9个圆形对象,他们代表不同的树,以及玩家的位置:

QQ截图20160602224046.jpg

在代码中,我们首先建立地图实例,渲染器实例,相机实例,然后显示它(这里的相机是从Stage中获取的):

OrthographicCamera camera;
IsometricStaggeredTiledMapRenderer mapRenderer;
TiledMap map;
public void show() {
    map =new TmxMapLoader().load("map/01.tmx");
    camera = (OrthographicCamera) stage.getCamera();
    camera.setToOrtho(false,7f,7f* (float) Gdx.graphics.getHeight()/(float) Gdx.graphics.getWidth());
    mapRenderer = new IsometricStaggeredTiledMapRenderer(map,1/92f);
    mapRenderer.setView(camera);
}
public void render(float delta) {
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      Gdx.gl.glClearColor(1f,1f,1f,0f);
      camera.update();
        mapRenderer.setView(camera);
        mapRenderer.render();
}

可以看到,显示正常:

QQ截图20160602224757.jpg

我们用Actor45代表每一个树和玩家,为了将地图的对象转换成实际的Actor45,编写一个MaptoActor类,类中定义一个属性类和一个Actor45的map:

public static class Aliases {

    /** the aliases */
    public String actor45id = "actor45id";
    public String actor45type = "actor45type";
    public String tree="tree";
    public String ren="ren";
    public String jianzhu="jianzhu";
}
ObjectMap<String,Actor45> actor45s=new ObjectMap<String, Actor45>();;

定义load函数,传入对象层,与资源管理器及舞台,它转换Map对象为Actor45并添加至舞台中,每一个Actor45的属性,定义在一个json文件中,包含Actor的ID,name,texture等等。

public void load(MapLayer layer, ResourceManager resourceManager, Stage stage){
    JsonValue jsonValue =new JsonReader().parse(Gdx.files.internal("1.json"));
    JsonValue jsonValuetmp0;
    MapObjects objects=layer.getObjects();
    for (int i=0;i<layer.getObjects().getCount();i++){
        String actor45type = MapUtils.getProperty(objects.get(i).getProperties(),aliases.actor45type,"null");
        if (!actor45type.equals("null")){
            int actor45id = MapUtils.getProperty(objects.get(i).getProperties(),aliases.actor45id,0);
            if (actor45id!=0){
                float x=MapUtils.getProperty(objects.get(i).getProperties(),"x",0.0f);
                float y=MapUtils.getProperty(objects.get(i).getProperties(),"y",0.0f);
                String actorname=objects.get(i).getName();//MapUtils.getProperty(objects.get(i).getProperties(),"name","actor");
                if (actorname==null||actorname.equals("")){
                    actorname="actor";
                }
                x=(x-46f)/92f;
                y=y/92f;
                actorname=getaname(actorname);
                jsonValuetmp0 = jsonValue.get(actor45type).get(""+actor45id);
                float width=jsonValuetmp0.getFloat("width");
                float width45=jsonValuetmp0.getFloat("width45");
                float height45 = jsonValuetmp0.getFloat("height45");
                if (jsonValuetmp0.getString("actor45type").equals("static")){
                    TextureRegionWithOffset textureRegionWithOffset=resourceManager.getTextureRegions().get(jsonValuetmp0.getString("texture"));
                    System.out.println("add:"+actorname);
                    addAStaticActor45(actorname,textureRegionWithOffset,width45,height45,width,x,y,stage);
                }else if (jsonValuetmp0.getString("actor45type").equals("animation")){
                    AnimationActor45 animationActor45=new AnimationActor45();
                    animationActor45.setXY(x,y);
                    animationActor45.setWidth45(width45);
                    animationActor45.setWidth(width);
                    animationActor45.setHeight45(height45);
                    if (jsonValuetmp0.get("animation")!=null){
                        JsonValue jsonValue1=jsonValuetmp0.get("animation");
                        if (jsonValue1.size>0){
                            for (int h=0;h<jsonValue1.size;h++){
                                Animation animation=resourceManager.getAnimations().get(jsonValue1.getString(""+h));
                                animationActor45.addAnimation(animation);
                            }
                        }
                    }
                    if (jsonValuetmp0.get("texture")!=null){
                        JsonValue jsonValue1=jsonValuetmp0.get("texture");
                        if (jsonValue1.size>0){
                            for (int h=0;h<jsonValue1.size;h++){
                                TextureRegionWithOffset textureRegionWithOffset=resourceManager.getTextureRegions().get(jsonValue1.getString(""+h));
                                animationActor45.addTextureRegion(textureRegionWithOffset);
                            }
                        }
                    }
                    actor45s.put(actorname,animationActor45);
                    stage.addActor(animationActor45);
                }
            }
        }
    }
}

在GameView中实例化MaptoActor,并让它读取map的对象层:

maptoActor45.load(map.getLayers().get("objects"),resourceManager,stage)

在render()中绘制stage:

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();

运行:

QQ截图20160602230355.jpg



mX7Q:
2017-07-08
mX7QSh http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com
8awP:
2017-07-10
8awPdP <a href="http://ajctfowlrfnu.com/">ajctfowlrfnu</a>, [url=http://gufjforupmpx.com/]gufjforupmpx[/url], [link=http://xclxormfcbbw.com/]xclxormfcbbw[/link], http://dobtycho
RQKz:
2017-07-10
RQKzEt <a href="http://dkcqfhxaydfv.com/">dkcqfhxaydfv</a>, [url=http://kfahxjyriihf.com/]kfahxjyriihf[/url], [link=http://hlvvtaudcabr.com/]hlvvtaudcabr[/link], http://smrotmqh
9Zcw:
2017-07-10
9ZcwMp <a href="http://tzsuwxbnuhnb.com/">tzsuwxbnuhnb</a>, [url=http://rlzdjyjuhvnn.com/]rlzdjyjuhvnn[/url], [link=http://wdjhssmakuqu.com/]wdjhssmakuqu[/link], http://spzpfalz
gJ3z:
2017-07-10
gJ3zR8 <a href="http://gozihoayfhzg.com/">gozihoayfhzg</a>, [url=http://iuhdmgzkcclp.com/]iuhdmgzkcclp[/url], [link=http://ysqmsgujjjew.com/]ysqmsgujjjew[/link], http://dhbclxga
NcIQ:
2017-07-10
NcIQ4K <a href="http://qnwqxuvenrsx.com/">qnwqxuvenrsx</a>, [url=http://azmvmiygopjf.com/]azmvmiygopjf[/url], [link=http://oulehzrqpmxw.com/]oulehzrqpmxw[/link], http://nfcncshs
Jonn:
2017-07-19
Jonny was here <a href=" http://buyviagracom.cn/#closet ">buy viagra</a> On a positive note, the report revealed that rates of high blood pressure were 50% lower among people wi
Lost:
2017-07-19
Lost credit card <a href=" http://buyviagracom.cn/ ">buy viagra online china</a> The desire for entertainment and amusement seems greater than the desire/need for truth and fact
Will:
2017-07-19
Will I get travelling expenses? <a href=" http://buyviagracom.cn/#gift ">buy real viagra online</a> Geisler points out that it&rsquo;s sometimes easy to peg managers as &
A pe:
2017-07-19
A pension scheme <a href=" http://buyviagracom.cn/#letter ">viagra online</a> Stephen Morrison, an expert in global health policy at theCenter for Strategic and International St
What:
2017-07-19
What do you study? <a href=" http://buyviagracom.cn/#tasted ">china viagra sale</a> Chuck Grassley that ATF "makes every effort to interdict weapons that have been purchase
I qu:
2017-07-19
I quite like cooking <a href=" http://buyviagracom.cn/ ">order viagra</a> Under the interim agreement reached in Geneva last November, Iran has agreed not to install any more ma
Wher:
2017-07-19
Whereabouts are you from? <a href=" http://buyviagracom.cn/ ">order viagra</a> "While we have had conversations with many parties about this transaction, we do not have an
I qu:
2017-07-19
I quite like cooking <a href=" http://buyviagracom.cn/#amy ">buy real viagra online</a> There is no doubt, unless Lynch&rsquo;s Republican opponents get even dumber in the n
How:
2017-07-19
How long are you planning to stay here? <a href=" http://buyviagracom.cn/viagra-and-antiviral-drugs.php ">viagra trademark china </a> What Tommy did was simply to set me thinkin
Chil:
2017-07-19
Children with disabilities <a href=" http://buyviagracom.cn/ ">viagra online </a> According to the findings, 70% of people with type 2 diabetes believe that they are in control
I ca:
2017-07-19
I came here to work <a href=" http://buyviagracom.cn/generic-viagra.php ">viagra </a> All the key returning and new pieces of the Packers defense were on the field for the drive
I li:
2017-07-19
I like it a lot <a href=" http://buyviagracom.cn/ ">buy generic viagra 25mg online </a> Malakoff has an effective capacity of 5,350 megawattcomprising six power plants that run
How:
2017-07-19
How many more years do you have to go? <a href=" http://buyviagracom.cn/ ">buy generic viagra 25mg </a> His tongue-in-cheek reproach about being made to clean bedpans on a visit
Spec:
2017-07-19
Special Delivery <a href=" http://buyviagracom.cn/ ">buy viagra 50mg online </a> "This isn&#039;t to point a finger at the editorial team, the writers or the illustrato
Very:
2017-07-19
Very funny pictures <a href=" http://buyviagracom.cn/generic-viagra.php ">buy viagra </a> Still, it is a black eye for a governor who once vowed to clean up Albany to be under i
Just:
2017-07-19
Just over two years <a href=" http://buyviagracom.cn/viagra-in-a-liquid-form.php ">buy viagra pills online </a> Ms Gath, a thalidomide survivor who was born in Newry, has been a
ojKp:
2017-07-31
ojKpws <a href="http://ywwqywbmkdgv.com/">ywwqywbmkdgv</a>, [url=http://gvlshbniuewt.com/]gvlshbniuewt[/url], [link=http://opmsaikzkmvj.com/]opmsaikzkmvj[/link], http://rqsoxwiy
HtZS:
2017-08-01
HtZSKE <a href="http://geybbgdttfsa.com/">geybbgdttfsa</a>, [url=http://sfmnhnnqsdea.com/]sfmnhnnqsdea[/url], [link=http://bztdzleursif.com/]bztdzleursif[/link], http://lqmffxlj
OuE7:
2017-08-01
OuE73M <a href="http://lnuqmsiqwskf.com/">lnuqmsiqwskf</a>, [url=http://knkpibplaocc.com/]knkpibplaocc[/url], [link=http://gyawbsjprhfo.com/]gyawbsjprhfo[/link], http://htkphbfc
发表评论