- 메이플 현재 3황 중 캐슈에 대해서 알아보자 [247]
- 메이플 OTP 뚫림 예비군 다녀왔더니 알몸이 된 건에 대하여, (긴글주의 , 메린이 주의) [389]
- FCO 비비 밤양갱 최대 피해자.jpg [10]
- 차벤 안전벨트 한채로 앞유리 뚫고 나가는줄 ㄷㄷ [10]
- 차벤 블랙박스 제보자님...할많하않.... [1]
Document Version : V1.3 - 2017.07.19 with cocos2d-x 3.15.1
Document Version : V1.2 - 2015.06.08 with cocos2d-x 3.6
제 책인 "시작하세요! Cocos2d-x 3.0 프로그래밍" 내용을 3.15.1 버전에 맞게 수정하여 올리고 있습니다.
이 글은 네이버카페 "Cocos2d-x 사용자 모임"에 동시에 게재되고 있습니다.
개발환경 :
c:> cocos new Box2dEx18 -p com.study.box18 -l cpp ↵
Box2dEx02의 모든 코드를 방금 만든 Box2dEx18에 적용시킵니다.
Box2dEx02의 Classes 폴더의 다음 파일들을 Box2dEx18의 Classes 폴더에 덮어 쓰면 됩니다.
■ HelloWorldScene.h
■ HelloWorldScene.cpp
■ GLES-Render.h
■ GLES-Render.cpp
그러고 나서 다음의 디렉터리에서
{Cocos2d-x가 설치된 디렉터리} / tests / cpp-tests / Resources / Images
아래의 파일을 찾아 리소스 폴더에 추가합니다.
[ HelloWorldScene.h – 박스2D 조인트 제거 ]
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#pragma execution_character_set("utf-8")
#endif
#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include <GLES-Render.h>
#define PTM_RATIO 32
using namespace cocos2d;
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
Size winSize;
Texture2D* texture;
b2World* _world;
// For debugging
GLESDebugDraw* m_debugDraw;
cocos2d::CustomCommand _customCmd;
bool createBox2dWorld(bool debug);
void setBox2dWorld();
~HelloWorld();
virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform,
uint32_t flags) override;
void onDraw(const cocos2d::Mat4& transform, uint32_t flags);
void onEnter();
void onExit();
void tick(float dt);
b2Body* addNewSprite(Vec2 point, Size size, b2BodyType bodytype,
const char* spriteName, int type);
b2Body* getBodyAtTab(Vec2 p);
bool onTouchBegan(Touch* touch, Event* event);
};
#endif // __HELLOWORLD_SCENE_H__
[ HelloWorldScene.cpp – 박스2D 조인트 제거 ]
#include "HelloWorldScene.h"
Scene* HelloWorld::createScene()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
bool HelloWorld::init()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
bool HelloWorld::createBox2dWorld(bool debug)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::setBox2dWorld()
{
b2RevoluteJointDef revJointDef1;
b2RevoluteJointDef revJointDef2;
b2RevoluteJointDef revJointDef3;
b2RevoluteJointDef revJointDef4;
b2Body* body1 = this->addNewSprite(Vec2(160, 260), Size(34, 34),
b2_staticBody, "test", 0);
b2Body* body2 = this->addNewSprite(Vec2(200, 260), Size(34, 34),
b2_dynamicBody, "test", 0);
b2Body* body3 = this->addNewSprite(Vec2(240, 260), Size(34, 34),
b2_dynamicBody, "test", 0);
b2Body* body4 = this->addNewSprite(Vec2(280, 260), Size(34, 34),
b2_dynamicBody, "test", 0);
b2Body* body5 = this->addNewSprite(Vec2(320, 260), Size(34, 34),
b2_staticBody, "test", 0);
revJointDef1.Initialize(body1, body2, body1->GetPosition());
revJointDef2.Initialize(body2, body3, body2->GetPosition());
revJointDef3.Initialize(body4, body3, body4->GetPosition());
revJointDef4.Initialize(body5, body4, body5->GetPosition());
_world->CreateJoint(&revJointDef1);
_world->CreateJoint(&revJointDef2);
_world->CreateJoint(&revJointDef3);
_world->CreateJoint(&revJointDef4);
}
HelloWorld::~HelloWorld()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::onDraw(const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::onEnter()
{
Scene::onEnter();
// 싱글터치모드로 터치리스너 등록
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onExit()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::tick(float dt)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
b2Body* HelloWorld::addNewSprite(Vec2 point, Size size, b2BodyType bodytype,
const char* spriteName, int type)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
b2Body* HelloWorld::getBodyAtTab(Vec2 p)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
Vec2 touchPoint = touch->getLocation();
if (b2Body* b = this->getBodyAtTab(touchPoint))
{
b2JointEdge* joints = b->GetJointList();
while (joints)
{
b2Joint* joint = joints->joint;
joints = joints->next;
_world->DestroyJoint(joint);
}
}
return true;
}
if (b2Body* b = this->getBodyAtTab(touchPoint))
{
b2JointEdge* joints = b->GetJointList();
while (joints)
{
b2Joint* joint = joints->joint;
joints = joints->next;
_world->DestroyJoint(joint);
}
}
코드를 완성했으면 실행시켜 봅니다.
내폰젤무거워