- 디아4 시즌 5 1시간43분 내용 17분안에 요약해봤습니다. [35]
- FCO 봉훈 유튜브에 올라온 현직 게임 개발자피셜 [48]
- 와우 로아에서 와우 넘어온 사람이 보는 진입장벽(로아지인 의견o) [86]
- 걸그룹 1황 장원영 [18]
- 차벤 제보 했다가 역관광 중인 영상 [15]
- 걸그룹 머리에 뭐라도 얹으면 실패없는 김채원 [4]
Document Version : V1.3 - 2017.07.18 with cocos2d-x 3.15.1
Document Version : V1.2 - 2015.06.08 with cocos2d-x 3.6
Document Version : V1.1 - 2014.03.14 with cocos2d-x 3.0beta2
Document Version : V1.0 - 2013.07.10 with cocos2d-x 2.1.4
제 책인 "시작하세요! Cocos2d-x 3.0 프로그래밍" 내용을 3.15.1 버전에 맞게 수정하여 올리고 있습니다.
이 글은 네이버카페 "Cocos2d-x 사용자 모임"에 동시에 게재되고 있습니다.
개발환경 :
c:> cocos new Box2dEx17 -p com.study.box17 -l cpp ↵
Box2dEx02의 모든 코드를 방금 만든 Box2dEx17에 적용시킵니다.
Box2dEx02의 Classes 폴더의 다음 파일들을 Box2dEx17의 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);
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()
{
// 바디를 생성해서 월드에 추가한다.
// 물리 객체의 바디와 해당 스프라이트를 추가한다.
this->addNewSprite(Vec2(80, 160), Size(32, 32), b2_dynamicBody, "test", 0);
this->addNewSprite(Vec2(160, 160), Size(32, 32), b2_dynamicBody, "test", 0);
this->addNewSprite(Vec2(240, 160), Size(32, 32), b2_dynamicBody, "test", 0);
this->addNewSprite(Vec2(320, 160), Size(32, 32), b2_dynamicBody, "test", 0);
this->addNewSprite(Vec2(400, 160), Size(32, 32), b2_dynamicBody, "test", 0);
}
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의 코드와 같음 …
}
bool HelloWorld::onTouchBegan(Touch *touch, Event * event)
{
Vec2 touchPoint = touch->getLocation();
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
auto spriteData = (Sprite *)b->GetUserData();
Rect rect = spriteData->getBoundingBox();
if (rect.containsPoint(touchPoint)) {
// 스프라이트 삭제
this->removeChild(spriteData, true);
// 물리객체 삭제
_world->DestroyBody(b);
break;
}
}
}
return true;
}
Vec2 touchPoint = touch->getLocation();
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
auto spriteData = (Sprite *)b->GetUserData();
Rect rect = spriteData->getBoundingBox();
if (rect.containsPoint(touchPoint)) {
// 스프라이트 삭제
this->removeChild(spriteData, true);
// 물리객체 삭제
_world->DestroyBody(b);
break;
}
}
}
코드를 완성했으면 실행시켜 봅니다.
내폰젤무거워