How to get "polyline points" of tmx?

part of tmx about objectgroup
*************************************************************************************+

<object name=“Object” x=“0” y=“992” width=“352” height=“32”/>
<object name=“Object” x=“224” y=“928” width=“160” height=“32”/>
<object name=“platform” type=“platform” x=“2” y=“833” width=“125” height=“60”>



</object>
<object gid=“38” x=“181” y=“818”/>
<object gid=“38” x=“155” y=“763”/>
<object x=“693” y=“891”>

</object>
<object x=“469” y=“783”>

</object>

*************************************************+
I want get value of “polygon points”
cocos2d-x code
+
+
+
+
++
CCTMXTiledMap map = getChildByTag;
CCTMXObjectGroup
group = map~~>objectGroupNamed;
CCArray *array = group~~>getObjects();
CCDictionary* dict;
CCObject* pObj = NULL;
CCARRAY_FOREACH(array, pObj)
{
dict = (CCDictionary
)pObj;
if
{
break;
}
const char
key = “x”;
int x = ((CCString*)dict~~>objectForKey)>intValue();
key = “y”;
int y = ((CCString*)dict
>objectForKey)>intValue();
key = “width”;
int width = ((CCString*)dict
>objectForKey)>intValue();
key = “height”;
int height = ((CCString*)dict
>objectForKey)~~>intValue();

}

I can get “X” “Y” “width” “height” but I don’t know how to get polygon points
I am a new leaner ,please help me ,thank you !

you can use the following code piece to retrieve the polygon points:

CCDictionary* dict = (CCDictionary**)objectGroup~~>getObjects~~>lastObject;
CCArray* points = dict~~>objectForKey;
for ; ++i) {
CCDictionary* dict2 = points~~>objectAtIndex;
const char** key = “x”;
int x = ((CCString**)dict2~~>objectForKey)>intValue;
key = “y”;
int y = dict2
>objectForKey)>intValue;
CCLOG (“x=%d, y=%d”,x,y);
}
Warning:
It seems you can’t retrieve polyline points with the newest version of cocos2d-x, you can refer to CCTMXXMLParse.cpp , line 623 for more infomation.
the parse codes of polyline are missing, if you want wo retrieve the polyline points, you can refer to this thread:
http://www.cocos2d-x.org/boards/6/topics/12122
I don’t test the above code either, so be careful and wish you good luck.
Update:
In the above code, you’d better add name property to each polygon and polyline object, so you can use objectGroup
>objectName to retrieve the desired object.
then you can use the following code to retrieve polyline & polygon:
CCTMXObjectGroup objectGroup = tiledMap~~>objectGroupNamed;
CCDictionary* dict = objectGroup~~>objectNamed;
CCArray
points = dict~~>objectForKey;
for ; ++i) {
CCDictionary** dict2 = (CCDictionary*)points~~>objectAtIndex;
const char* key = “x”;
int x = dict2~~>objectForKey(“x”))>intValue;
key = “y”;
int y = dict2
>objectForKey(key))>intValue;
CCLOG (“x=%d, y=%d”,x,y);
}
CCDictionary **polyline = objectGroup
>objectNamed;
CCLOG (“polyline=%s”, ((CCString*)polyline~~>objectForKey)>getCString);
CCArray *array = objectGroup
>getObjects;
CCObject** pObj = NULL;
CCARRAY_FOREACH
{
dict = pObj;
if
{
break;
}
const char* key = “x”;
int x = dict~~>objectForKey(key))>intValue;
key = “y”;
int y = dict
>objectForKey(key))>intValue;
key = “width”;
int width = dict
>objectForKey(key))>intValue;
key = “height”;
int height = dict
>objectForKey(key))~~>intValue;
CCLOG (“x=%d, y= %d, width=%d, height=%d”,x,y,width,height);
}

This is my tmx file:
<pre>

<?xml version="1.0" encoding="UTF-8"?>

<map version=“1.0” orientation=“orthogonal” width=“20” height=“10” tilewidth=“32” tileheight=“32”>











H4sIAAAAAAAAA2NlYGBgA2J2IOYAYk40zAXE3EDMA8S8WORJVc8PxAJALAjEQkSYR6r6kYjFsLDF8KjFJidOQC2MFsGBhaEYWUwUB8ZlBjLmg2Ji1NLSPACAEDAIIAMAAA==




H4sIAAAAAAAAA2NgGBmAhQJ9MExts5H14jKDkN3UBsT4dyQBAIF16DMgAwAA



<object name=“StartPoint” x=“14” y=“151” width=“18” height=“18”/>
<object name=“first” x=“213” y=“47”>

</object>
<object name=“second” x=“418” y=“52”>

</object>

</map>
</pre>

and my output:
<pre>
Cocos2d: x=0, y=0
Cocos2d: x=96, y=96
Cocos2d: x=137, y=5
Cocos2d: polyline=0,0 79,~~4
Cocos2d: x=14, y= 151, width=18, height=18
Cocos2d: x=213, y= 273, width=0, height=0
Cocos2d: x=418, y= 268, width=0, height=0

if you want to retrieve x,y property from polyline, you can refer to CCTMXXMLParse.cpp , line 623 for more infomation.

Your solution is very good Guanghui, but this solution doesn’t work unless we modify the CCTMXXMLParser.cpp class file.

I had to copy code from the condition else if (elementName == "polygon") to else if (elementName == "polyline")

Then your code works perfect!

I hope cocos2d-x team will add the code in their future release of sdk. :slight_smile: