TMXLayer.getProperty(propertyName) doesn't work
TMXLayer.getProperty(propertyName) doesn't work
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>@
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”));@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);
}@
- Status changed from New to Closed
- % Done changed from 0 to 100
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?
Yes,
The cc.TMXTiledMap has a function “getProperty”.
for example:
var myMap = cc.TMXTiledMap.create("res/DtMaps.tmx");
myMap.getProperty("optimo");
Thanks for your feedback.
This bug has been fixed. more details at: https://github.com/cocos2d/cocos2d-html5/pull/1315
Best regards
David
Status: | Closed | |
---|---|---|
Start date: | 2013-11-14 | |
Priority: | Low | |
Due date: | ||
Assignee: | ludingping | |
% Done: | 100% |
|
Category: | - | |
Target version: | v2.2.2 |
It returns “undefined” regardless of what string is passed as parameter.