Skip to content

Commit

Permalink
20150114 JSON通信とBODY削除の方法を試行錯誤中
Browse files Browse the repository at this point in the history
  • Loading branch information
headrokoko committed Jan 15, 2015
1 parent 50dede5 commit fbb7ac0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
10 changes: 7 additions & 3 deletions Classes/ContactListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void ContactListener::BeginContact(b2Contact* contact)
b2Fixture* FixtuerA = contact->GetFixtureA();
b2Fixture* FixtuerB = contact->GetFixtureB();


b2Vec2 gravity = b2Vec2(0.0f, 0.0f); //_worldの重力を設定
b2World* testworld = new b2World(gravity);

//FixtureのNullチェック
CCAssert(FixtuerA != NULL,"FixtureA is NULL");
CCAssert(FixtuerB != NULL,"FixtureB is NULL");
Expand Down Expand Up @@ -46,7 +50,7 @@ void ContactListener::BeginContact(b2Contact* contact)
{
InvisibleSprite(SpriteA); //SpritAを非表示
HeWorld->AddScore(10); //scoreに10加算
//DeleteBody(BodyA);
DeleteBody(BodyA, thisWorld);

}
if(TagB == TAG_ENEMY)
Expand Down Expand Up @@ -105,7 +109,7 @@ void ContactListener::InvisibleSprite(CCSprite* sprite)
}

//Body削除メソッド
void ContactListener::DeleteBody(b2Body* body)
void ContactListener::DeleteBody(b2Body* body, b2World* world)
{
thisWorld->DestroyBody(body);
world->DestroyBody(body);
}
2 changes: 1 addition & 1 deletion Classes/ContactListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ContactListener: public b2ContactListener
public:
ContactListener(b2World* world, HelloWorld* HW);
virtual void InvisibleSprite(CCSprite* sprite);
virtual void DeleteBody(b2Body* body);
virtual void DeleteBody(b2Body* body, b2World* world);
virtual void BeginContact(b2Contact* contact);

b2World* thisWorld;
Expand Down
46 changes: 21 additions & 25 deletions Classes/HelloWorldScene.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "HelloWorldScene.h"
#include "network/HttpClient.h"
#include "SimpleAudioEngine.h"
#include "Option.h"
#include "PhysiSprite.h"
#include "picojson.h"
#include <stdio.h>

USING_NS_CC;
USING_NS_CC_EXT;

HelloWorld::HelloWorld(){
HelloWorld::HelloWorld()
{
}

CCScene* HelloWorld::scene()
Expand Down Expand Up @@ -129,7 +133,18 @@ void HelloWorld::TouchPosLabelRenewal(CCPoint point)
}

//JSON要求メソッドの予定
void HelloWorld::onHttpRequestCompleted(CCHttpClient sender, CCHttpResponse response)
//void HelloWorld::onHttpRequestCompleted(CCHttpClient sender, CCHttpResponse response)

//JSONデータをサーバー側に送信する予定のメソッド
void HelloWorld::HttpPostDate(string Pname, int score)
{
CCDictionary* postdate = CCDictionary::create();
postdate->setObject(CCString::create(Pname),"name"); //PlayerNameをセット
postdate->setObject(CCInteger::create(score),"score"); //Scoreをセット
}

//受信したJSONデータを整理するメソッド
void HelloWorld::SortDate()
{
}

Expand All @@ -139,8 +154,6 @@ void HelloWorld::severMessageLabel()
request = new CCHttpRequest();
request->setUrl("http://localhost:8080/helo"); //リクエストを行うURL
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this,
callfuncND_selector(HelloWorld::onHttpRequestCompleted));

CCLabelTTF* severMessage = CCLabelTTF::create("SeverMessage: ", "arial", 20);
severMessage->setPosition(ccp(ScreenSize.width * 0.2f, ScreenSize.height * 0.6f));
Expand Down Expand Up @@ -281,6 +294,10 @@ void HelloWorld::initPhysics()
//ワールドの物理設定
gravity = b2Vec2(0.0f, 0.0f); //_worldの重力を設定
_world = new b2World(gravity); //_worldにgravityを入力

//衝突判定を設定
_contactListener = new ContactListener(_world, this);
_world->SetContactListener(_contactListener);

//ゲーム画面端の設定
ScreenSize = CCDirector::sharedDirector()->getWinSize();
Expand Down Expand Up @@ -315,9 +332,6 @@ void HelloWorld::initPhysics()

worldBody->CreateFixture(&worldBox,0);

//衝突判定を設定
_contactListener = new ContactListener(_world, this);
_world->SetContactListener(_contactListener);

//debugDrawの設定
_debugDraw = new GLESDebugDraw( PTM_RATIO );
Expand Down Expand Up @@ -380,25 +394,7 @@ bool HelloWorld::ccTouchBegan(CCTouch *touch, CCEvent *event)
return true;
}

void HelloWorld::JsonInit(string PlayerName, int ScorePoint)
{
const char* json = "{\"Name\":{\"PlayerName\":\"Test\"}}";
picojson::value v;
std::string err;
picojson::parse(v, json, json + strlen(json), &err);
if(err.empty())
{
picojson::object& o = v.get<picojson::object>();

//P1の名前の取得
string P1name = o["Name"].get<std::string>();

//P1のスコアの取得
//int P1score = o["Score"].get<int>();


}
}

void HelloWorld::setPlayerPosition(CCPoint position)
{
Expand Down
5 changes: 3 additions & 2 deletions Classes/HelloWorldScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class HelloWorld : public cocos2d::CCLayer, public b2ContactListener
virtual void draw(); //debugDraw作成メソッド 
virtual void ElapsedTime(float dt); //経過時間表示メソッド
virtual void AddScore(int point); //スコア加算メソッド
virtual void onHttpRequestCompleted(CCHttpClient sender,CCHttpResponse reponse);
virtual void JsonInit(string PlayerName, int ScorePoint); //Jsonの読み込み
virtual void HttpPostDate(string Pname, int score); //サーバー側への送信メソッド
virtual void SortDate(); //サーバーから受信したデータを整理するメソッド
//virtual void onHttpRequestCompleted(CCHttpClient sender, CCHttpResponse reponse);


virtual void severMessageLabel();
Expand Down
23 changes: 23 additions & 0 deletions Resources/Test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"1": {
"id": 1,
"name": "skelton",
"hp": 10,
"attack": 2,
"def": 1
},
"2": {
"id": 2,
"name": "tori",
"hp": 20,
"attack": 5,
"def": 3
},
"3": {
"id": 3,
"name": "miira",
"hp": 30,
"attack": 10,
"def": 5
}
}
5 changes: 0 additions & 5 deletions Test.json

This file was deleted.

0 comments on commit fbb7ac0

Please sign in to comment.