2014. 11. 18. 11:30
[물리 엔진] 기본 예제 DATA_OLD/Cocos2d-x Study2014. 11. 18. 11:30
내장 물리엔진 이용한 기본 예제
- 물리 엔진이 적용되는 Scene을 생성
- 물리 효과를 갖는 바디와 값을 설정하여 스프라이트에 적용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include "AppDelegate.h" #include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("My Game"); director->setOpenGLView(glview); glview->setFrameSize(480, 320); glview->setDesignResolutionSize(480, 320, ResolutionPolicy::NO_BORDER); } // turn on display FPS director->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = HelloWorld::createScene(); // run director->runWithScene(scene); return true; } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" /** @brief The cocos2d Application. The reason for implement as private inheritance is to hide some interface call by Director. */ class AppDelegate : private cocos2d::Application { public: AppDelegate(); virtual ~AppDelegate(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief The function be called when the application enter background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual void applicationWillEnterForeground(); }; #endif // _APP_DELEGATE_H_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // auto scene = Scene::create(); // auto layer = HelloWorld::create(); // scene->addChild(layer); Size visibleSize = Director::getInstance()->getVisibleSize(); Vect gravity = Vect(0.0f, -300.f); auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); scene->getPhysicsWorld()->setGravity(gravity); auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); auto edgeNode = Node::create(); edgeNode->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2)); edgeNode->setPhysicsBody(body); scene->addChild(edgeNode); auto layer = HelloWorld::create(); layer->setPhyWorld(scene->getPhysicsWorld()); scene->addChild(layer); return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255)) ) { return false; } auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); return true; } bool HelloWorld::onTouchBegan(Touch *touch, Event *event) { return true; } void HelloWorld::onTouchEnded(Touch *touch, Event *event) { auto location = touch->getLocation(); addNewSpriteAtPosition(location); } void HelloWorld::addNewSpriteAtPosition(Point p) { auto sprite = Sprite::create("CloseSelected.png"); sprite->setPosition(p); auto material1 = PhysicsMaterial(0.0f, 0.0f, 0.0f); auto material2 = PhysicsMaterial(1.0f, 1.0f, 0.5f); auto body = PhysicsBody::createCircle(sprite->getContentSize().width / 2, material1); body->setMass(100.0f); sprite->setPhysicsBody(body); this->addChild(sprite); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::LayerColor { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); //add PhysicsWorld* m_world; void setPhyWorld(PhysicsWorld* world){ m_world = world; } virtual bool onTouchBegan(Touch* touch, Event* event); virtual void onTouchEnded(Touch* touch, Event* event); void addNewSpriteAtPosition(Point p); }; #endif // __HELLOWORLD_SCENE_H__ | cs |
'DATA_OLD > Cocos2d-x Study' 카테고리의 다른 글
[Cocos2d-x] 안드로이드 포팅 시 Android.mk 파일 수정 관련 (0) | 2015.02.14 |
---|---|
[Cocos2d-x] AppActivity에 Google Play랑 Admob 더하기 (0) | 2015.02.14 |
[Cocos2d-x] Google Play 연동법(1) - 라이브러리 추가 (0) | 2015.02.09 |
[Cocos2d-X] perhaps java_home does not point to the jdk (0) | 2015.01.19 |
[Cocos2d-x] Move액션의 속도, 움직임 관련하여 (0) | 2015.01.19 |