使用Physics_Body_Editor获得json文件的类

【转自】:http://www.cocoachina.com/bbs/read.php?tid=209290 

 

 工具介绍,json文件获得方法,请参考原帖

 MyBodyParser.h

 1 //
 2 //  MyBodyParser.h
 3 //
 4 //  Created by Jason Xu.
 5 //
 6 //
 7
 8 #pragma once
 9
10 #include <string>
11 #include "cocos2d.h"
12 USING_NS_CC;
13 #include "json/document.h"
14
15 class MyBodyParser {
16     MyBodyParser(){}
17     rapidjson::Document doc;
18 public:
19     static MyBodyParser* getInstance();
20     bool parseJsonFile(const std::string& pFile);
21     bool parse(unsigned char* buffer, long length);
22     void clearCache();
23     PhysicsBody* bodyFormJson(Node* pNode, const std::string& name);
24 };

 MyBodyParser.cpp

 

 1 //
 2 //  MyBodyParser.cpp
 3 //
 4 //  Created by Jason Xu.
 5 //
 6 //
 7
 8 #include "MyBodyParser.h"
 9
10 MyBodyParser* MyBodyParser::getInstance()
11 {
12     static MyBodyParser* sg_ptr = nullptr;
13     if (nullptr == sg_ptr)
14     {
15         sg_ptr = new MyBodyParser;
16     }
17     return sg_ptr;
18 }
19
20 bool MyBodyParser::parse(unsigned char *buffer, long length)
21 {
22     bool result = false;
23     std::string js((const char*)buffer, length);
24     doc.Parse<0>(js.c_str());
25     if(!doc.HasParseError())
26     {
27         result = true;
28     }
29     return result;
30 }
31
32 void MyBodyParser::clearCache()
33 {
34     doc.SetNull();
35 }
36
37 bool MyBodyParser::parseJsonFile(const std::string& pFile)
38 {
39     auto content = FileUtils::getInstance()->getDataFromFile(pFile);
40     bool result = parse(content.getBytes(), content.getSize());
41     return result;
42 }
43
44 //从json文件加载正确的body
45 PhysicsBody* MyBodyParser::bodyFormJson(cocos2d::Node *pNode, const std::string& name)
46 {
47     PhysicsBody* body = nullptr;
48     rapidjson::Value &bodies = doc["rigidBodies"];
49     if (bodies.IsArray())
50     {
51         //遍历文件中的所有body
52         for (int i=0; i<bodies.Size(); ++i)
53         {
54             //找到了请求的那一个
55             if (0 == strcmp(name.c_str(), bodies[i]["name"].GetString()))
56             {
57                 rapidjson::Value &bd = bodies[i];
58                 if (bd.IsObject())
59                 {
60                     //创建一个PhysicsBody, 并且根据node的大小来设置
61                     body = PhysicsBody::create();
62                     float width = pNode->getContentSize().width;
63                     float offx = - pNode->getAnchorPoint().x*pNode->getContentSize().width;
64                     float offy = - pNode->getAnchorPoint().y*pNode->getContentSize().height;
65
66                     Point origin( bd["origin"]["x"].GetDouble(), bd["origin"]["y"].GetDouble());
67                     rapidjson::Value &polygons = bd["polygons"];
68                     for (int i = 0; i<polygons.Size(); ++i)
69                     {
70                         int pcount = polygons[i].Size();
71                         Point* points = new Point[pcount];
72                         for (int pi = 0; pi<pcount; ++pi)
73                         {
74                             points[pi].x = offx + width * polygons[i][pcount-1-pi]["x"].GetDouble();
75                             points[pi].y = offy + width * polygons[i][pcount-1-pi]["y"].GetDouble();
76                         }
77                         body->addShape(PhysicsShapePolygon::create(points, pcount, PHYSICSBODY_MATERIAL_DEFAULT));
78                         delete [] points;
79                     }
80                 }
81                 else
82                 {
83                     CCLOG("body: %s not found!", name.c_str());
84                 }
85                 break;
86             }
87         }
88     }
89     return body;
90 }

 HelloWorldScene.cpp (测试cpp)

  1 #include "HelloWorldScene.h"
  2 #include "MyBodyParser.h"
  3 USING_NS_CC;
  4
  5 Scene* HelloWorld::createScene()
  6 {
  7     // ‘scene‘ is an autorelease object
  8     auto scene = Scene::createWithPhysics();
  9
 10     //enable debug draw
 11     scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
 12
 13     // ‘layer‘ is an autorelease object
 14     auto layer = HelloWorld::create();
 15
 16     // add layer as a child to scene
 17     scene->addChild(layer);
 18
 19     // return the scene
 20     return scene;
 21 }
 22
 23 // on "init" you need to initialize your instance
 24 bool HelloWorld::init()
 25 {
 26     //////////////////////////////
 27     // 1. super init first
 28     if ( !Layer::init() )
 29     {
 30         return false;
 31     }
 32
 33     Size visibleSize = Director::getInstance()->getVisibleSize();
 34     Point origin = Director::getInstance()->getVisibleOrigin();
 35
 36     /////////////////////////////
 37     // 2. add a menu item with "X" image, which is clicked to quit the program
 38     //    you may modify it.
 39
 40     // add a "close" icon to exit the progress. it‘s an autorelease object
 41     auto closeItem = MenuItemImage::create(
 42                                            "CloseNormal.png",
 43                                            "CloseSelected.png",
 44                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
 45
 46     closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
 47                                 origin.y + closeItem->getContentSize().height/2));
 48
 49     // create menu, it‘s an autorelease object
 50     auto menu = Menu::create(closeItem, NULL);
 51     menu->setPosition(Point::ZERO);
 52     this->addChild(menu, 1);
 53
 54     /////////////////////////////
 55     // 3. add your codes below...
 56
 57     // add a label shows "Hello World"
 58     // create and initialize a label
 59
 60     auto label = LabelTTF::create("Physics Body Loader Demo", "Arial", 24);
 61
 62     // position the label on the center of the screen
 63     label->setPosition(Point(origin.x + visibleSize.width/2,
 64                             origin.y + visibleSize.height - label->getContentSize().height));
 65
 66     // add the label as a child to this layer
 67     this->addChild(label, 1);
 68
 69     status_label = LabelTTF::create("Touch anywhere!", "Arial", 36);
 70     status_label->setPosition(Point(origin.x + visibleSize.width/2, 1.2*status_label->getContentSize().height));
 71     this->addChild(status_label);
 72
 73     // add "2dx.png"
 74     sp_2dx = Sprite::create("2dx.png");
 75
 76     // position the sprite on the center of the screen
 77     sp_2dx->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
 78
 79     //load
 80     MyBodyParser::getInstance()->parseJsonFile("bodies.json");
 81
 82     //bind physicsbody to sprite
 83     auto _body = MyBodyParser::getInstance()->bodyFormJson(sp_2dx, "2dx");
 84     if (_body != nullptr) {
 85         _body->setDynamic(false); //set it static body.
 86         _body->setCollisionBitmask(0x000000); //don‘t collision with anybody.
 87         sp_2dx->setPhysicsBody(_body);
 88     }
 89
 90     // add the sprite as a child to this layer
 91     this->addChild(sp_2dx, 0);
 92
 93     //add touchListener
 94     auto touchListener = EventListenerTouchOneByOne::create();
 95     touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
 96     touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
 97     touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
 98     _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
 99
100     return true;
101 }
102
103 Node* HelloWorld::nodeUnderTouch(cocos2d::Touch *touch)
104 {
105     Node* node = nullptr;
106     //转换到layer内的坐标
107     auto location = this->convertTouchToNodeSpace(touch);
108     //得到当前点下方的物理shapes
109     auto scene = Director::getInstance()->getRunningScene();
110     auto arr = scene->getPhysicsWorld()->getShapes(location);
111
112     //遍历当前点击到的所有shapes, 看看有没有我们的2dx!
113     for (auto& obj : arr)
114     {
115         //find it
116         if ( obj->getBody()->getNode() == sp_2dx)
117         {
118             node = obj->getBody()->getNode();
119             break;
120         }
121     }
122     return node;
123 }
124
125 bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
126 {
127     auto current_node = nodeUnderTouch(touch);
128
129     //get it!
130     if (current_node == sp_2dx)
131     {
132         status_label->setColor(Color3B::GREEN);
133         status_label->setString("Ohoo, U catch me!");
134     }
135     else
136     {
137         status_label->setColor(Color3B::RED);
138         status_label->setString("Haha, touch outside!");
139     }
140
141     return true;
142 }
143
144 void HelloWorld::onTouchMoved(Touch* touch, Event* event)
145 {
146 }
147
148 void HelloWorld::onTouchEnded(Touch* touch, Event* event)
149 {
150     status_label->setColor(Color3B::WHITE);
151     status_label->setString("Touch anywhere!");
152 }
153
154 void HelloWorld::menuCloseCallback(Ref* pSender)
155 {
156 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
157     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
158     return;
159 #endif
160
161     Director::getInstance()->end();
162
163 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
164     exit(0);
165 #endif
166 }

 

使用Physics_Body_Editor获得json文件的类

时间: 2024-08-09 22:00:52

使用Physics_Body_Editor获得json文件的类的相关文章

DNC读取配置Json文件到类中并在程序使用

ConfigurationBuilder 这个类提供了配置绑定,在dnc中 Program中WebHost提供了默认的绑定(appsettings文件) 如果我们需要加载我们自己的json配置文件怎么处理 var builder = new ConfigurationBuilder(); 这里builder 提供了很多添加的方式 1.第一种:直接添加json文件路径,这里需要注意的json文件地址问题 builder.AddJsonFile("path").Build(); 2.第二种

Code片段 : .properties属性文件操作工具类 &amp; JSON工具类

摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 java.util.Properties 是一个属性集合.常见的api有如下: load(InputStream inStream)  从输入流中读取属性 getProperty(String key)  根据key,获取属性值 getOrDefault(Object key, V defaultValue)

.net core2.0添加json文件并转化成类注入控制器使用

上一篇,我们介绍了如何读取自定义的json文件,数据是读取出来了,只是处理的时候太麻烦,需要一遍一遍写,很枯燥.那么有没有很好的办法呢?经过钻研,办法有了. 既然一个一个读取比较麻烦,那么可以把它放入一个类里面么?当然可以.这样直接注入这个类,让控制器去读取,那么就不需要每次加载它了,一次加载就可以在任何地方使用了. 由于本文是接这上一篇文章的更新介绍,请先读取上一篇文章再浏览此篇文章 (1):在Models文件夹中添加类Class,并填写如下代码: namespace CoreDemo.Mod

Json.NET读取和写入Json文件

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Text; using Newtonsoft.Json; using com.zjpx.model; using System.Collections; usin

Python 基础 - Json文件读写

JSON介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C.C++.Java.JavaScript.Perl.Python等).这些特性使JSON成为理想的数据交换语言.易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率). 在python中,JSON 由列表和字典组成. 序列化的两个模块中,json模块是用于

Cocos2d-x的生成Json文件的方法(续)

本文承接自前文:http://blog.csdn.net/yuxikuo_1/article/details/39155335 1.JsonMake类 //.h #include "cocos2d.h" #include "../cocos2d/external/json/document.h" #include "../cocos2d/external/json/writer.h" #include "../cocos2d/exter

json 对c++类的序列化(自动生成代码)

[动机] 之前写网络协议的时候,使用的是google protobuf,protobuf不但在性能和扩展性上有很好的优势,protoc自动生成c++类代码的工具,这点确实给程序员带来了很多便利. 做后面一项目使用的json格式来传输,然后就萌生了实现像protoc这样的工具,根据json文件来生成c++类代码,并且生成序列化代码,这样在写网络的时候就无需把jsonvalue序列化散落在各处. [思路] 之前写object-c的时候,如果你要对类的序列化,你必须实现NSCoding协议(接口),

Json文件读写

namespace web { public partial class testJson : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ConfigFileModel cfm = new ConfigFileModel(); //实体模型类 cfm.CreateDate = "2012-02-23"; cfm.FileName = "test.txt";

Unity基础 用C#脚本读取JSON文件数据

读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON,使用之前要先导入SimpleJSON的相关文件. JSON文件名:achieve.json,文件内容: { "30002":{"achieve_id":30002,"achieve_name":"连胜","achiev