#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;
}