在用Cocos Studio 创建好场景后导出,然后把导出的所有资源复制到cocos2D-x工程的Resources 文件夹下。
(SceneJson 文件名: MyScene.json)
导入场景Json以及获取场景内object 代码:
( 用create-project 创建的默认helloworld工程)
修改 CCScene* Hello World::scene()
CCScene* HelloWorld::scene()
{
CCScene scene = CCScene::create();
//导入场景JSON文件
CCNode MyScene = SceneReader::shareSceneReader()->ceateNodeWithSceneFile(“MyScene.json”);
//添加场景
scene->addChild(MyScene)
//获取场景内的对象
CCArmature Object = (CCArmature)(MyNode->getChildByTag(10005)->getChildByTag(10007)->getComponent(“MyHero”)->getNode());
// Object 即我在这个场景中添加的一个Sprite,如果要对此Sprite进行操控
Object->getParent()->runAction(CCMoveBy::create(2,ccp(100,0)));
return scene;
}
对象存储在CCArmature Object 变量中. 根据我的Json文件 可以看到最外层的Tag 是10005,所以要先getChildByTag(10005) 而我添加的精灵所在层的Tag 是10007所以要继续获取getChildByTag(10007) 然后就
能通过getComponent跟名字获取到我添加的精灵.
同理要获取到我添加的UILayer的话就应该
CCUILayer MyLayer=(CCUILayer*)(MyNode->getChildByTag(10005)->getComponent(“GUIComponent”)->getNode());
{
“classname”: “CCNode”,
“name”: “Main”,
“canedit”: true,
“objecttag”: 10000,
“rotation”: 0,
“scalex”: 1,
“scaley”: 1,
“visible”: 1,
“x”: 0,
“y”: 0,
“zorder”: 0,
“gameobjects”:
{
“__type”: “ComGameObjectSurrogate:#EditorCommon.JsonModel”,
“classname”: “CCNode”,
“name”: null,
“canedit”: true,
“objecttag”: 10005,
“rotation”: 0,
“scalex”: 1,
“scaley”: 1,
“visible”: 1,
“x”: 0,
“y”: 0,
“zorder”: 1,
“gameobjects”:
{
“__type”: “ComGameObjectSurrogate:#EditorCommon.JsonModel”,
“classname”: “CCNode”,
“name”: null,
“canedit”: true,
“objecttag”: 10007,
“rotation”: 0,
“scalex”: 0.6999999,
“scaley”: 0.6999999,
“visible”: 1,
“x”: 154.39990234375,
“y”: 105.59994506835938,
“zorder”: 1,
“gameobjects”: ],
“components”:
{
“__type”: “ComSpriteSurrogate:#EditorCommon.JsonModel.Component”,
“classname”: “CCSprite”,
“name”: “MyHero”,
“file”: null,
“fileData”: {
“path”: “aang.png”,
“plistFile”: “Mysprite.plist”,
“resourceType”: 1
}
}
]
}
],
“components”:
{
“__type”: “ComGUIAdapterSurrogate:#EditorCommon.JsonModel.Component”,
“classname”: “GUIComponent”,
“name”: “GUIComponent”,
“file”: null,
“fileData”: {
“path”: “Ralfrpg.json”,
“plistFile”: null,
“resourceType”: 0
}
}
]
}
],
“CanvasSize”: {
“_height”: 640,
“_width”: 960
},
“Version”: “1.0.0.0”,
“components”:
{
“__type”: “ComSceneSurrogate:#EditorCommon.JsonModel.Component”,
“classname”: “CCScene”,
“name”: “CCScene”,
“scenename”: “NewProject”
},
{
“__type”: “ComSimpleAudioSurrogate:#EditorCommon.JsonModel.Component”,
“classname”: “CCBackgroundAudio”,
“name”: “CCBackgroundAudio”,
“file”: null,
“fileData”: {
“path”: null,
“plistFile”: null,
“resourceType”: 0
},
“loop”: 0
}
]
}
