New issue
 Projects > js > Issues > Bug #3187

TMXLayer.getProperty(propertyName) doesn't work

Bug #3187 [Closed]
ZippoLag 2013-11-14 19:46 . Updated almost 12 years ago

It returns “undefined” regardless of what string is passed as parameter.

linshun 2013-11-22 02:51
  • Assignee set to ludingping
  • Target version set to v2.2.2
ludingping 2013-12-10 06:44

Sebastián Vansteenkiste wrote:

It returns “undefined” regardless of what string is passed as parameter.

Hi,

This problem has been tested, it works well. step as follow:
1. add some properties to a layer in a tmx file:
@ <layer name="tlBackground" width="10" height="6">
<properties>
<property name="PointsValue" value="{345,543}"/>
<property name="myTileSize" value="200"/>
</properties>
<data encoding="base64" compression="zlib">
eJzjYGBg4BhBGADlsAHh
</data>
</layer>@

  1. add some codes to myApp.js in HellowHTML5World app:
    @ var myMap = cc.TMXTiledMap.create(“res/DtMaps.tmx”);
    this.addChild(myMap, 9);
    myMap.setAnchorPoint(cc.p(0.5,0.5));
    myMap.setPosition(size.width / 2, size.height / 2);
    var selTMXLayer = myMap.getLayer(“tlBackground”);
    console.log(“myTileSize:” + selTMXLayer.getProperty(“myTileSize”));
    console.log(“PointsValue:” + selTMXLayer.getProperty(“PointsValue”));@

  2. Console outputs the result:
    myTileSize:200
    PointsValue:{345,543}

Here is the codes for parsing TMXLayer’s properties in CCTMXXMLParser.js:
@ var layerProps = selLayer.querySelectorAll(“properties > property”);
if (layerProps) {
var layerProp = {};
for (j = 0; j < layerProps.length; j++) {
layerProp[layerProps[j].getAttribute(‘name’)] = layerProps[j].getAttribute(‘value’);
}
layer.setProperties(layerProp);
}@

ludingping 2013-12-10 06:46
  • Status changed from New to Closed
  • % Done changed from 0 to 100
ZippoLag 2013-12-10 18:13

I see..

I was misusing the code now that I look at it.

You see, Tiled allows for giving properties to the tmx file itself (which is the logical place to store some properties, and not on some arbitrary layer) and that was the property I wanted to get.

The xml would look like:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="3" height="7" tilewidth="64" tileheight="64">
<properties>
<property name="optimo" value="5"/>
</properties>
<tileset firstgid="1" name="piso" tilewidth="64" tileheight="64">
<image source="piso.png" width="64" height="64"/>
</tileset>
...
...

Is there a way to get the value of “optimo” in my example?

ludingping 2013-12-11 02:28

Yes,

The cc.TMXTiledMap has a function “getProperty”.

for example:
var myMap = cc.TMXTiledMap.create("res/DtMaps.tmx");
myMap.getProperty("optimo");

ZippoLag 2013-12-12 03:19

And that’s the method that keeps returning “undefined” no matter what.

ludingping 2013-12-13 02:54

Thanks for your feedback.

This bug has been fixed. more details at: https://github.com/cocos2d/cocos2d-html5/pull/1315

Best regards
David

ZippoLag 2013-12-13 22:18

Thank you, and sorry for the extra work from me not specifying the bug properly.

Atom PDF

Status:Closed
Start date:2013-11-14
Priority:Low
Due date:
Assignee:ludingping
% Done:

100%

Category:-
Target version:v2.2.2