-
-
Save tks2shimizu/d8c3df1f72d75d2b21ca to your computer and use it in GitHub Desktop.
Pinball (2) 物理空間の生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HelloWorldScene.h" | |
USING_NS_CC; | |
CCScene* HelloWorld::scene() | |
{ | |
CCScene *scene = CCScene::create(); | |
HelloWorld *layer = HelloWorld::create(); | |
scene->addChild(layer); | |
return scene; | |
} | |
bool HelloWorld::init() | |
{ | |
if ( !CCLayer::init() ) | |
{ | |
return false; | |
} | |
// 物理空間の生成 | |
initPhysics(); | |
// 背景を追加 | |
createBackground(); | |
// 毎フレーム処理の開始 | |
scheduleUpdate(); | |
return true; | |
} | |
void HelloWorld::createBackground() | |
{ | |
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); | |
CCSprite* background = CCSprite::create("background.png"); | |
background->setPosition(ccp(winSize.width / 2, winSize.height / 2)); | |
this->addChild(background); | |
} | |
void HelloWorld::initPhysics() | |
{ | |
b2Vec2 gravity; | |
gravity.Set(0.0f, -0.8f); | |
world = new b2World(gravity); | |
} | |
void HelloWorld::update(float dt) | |
{ | |
int velocityIterations = 8; | |
int positionIterations = 1; | |
world->Step(dt, velocityIterations, positionIterations); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELLOWORLD_SCENE_H__ | |
#define __HELLOWORLD_SCENE_H__ | |
#include "cocos2d.h" | |
#include "Box2D.h" | |
class HelloWorld : public cocos2d::CCLayer | |
{ | |
protected: | |
void createBackground(); | |
b2World* world; | |
void initPhysics(); | |
public: | |
virtual bool init(); | |
static cocos2d::CCScene* scene(); | |
CREATE_FUNC(HelloWorld); | |
void update(float dt); | |
}; | |
#endif // __HELLOWORLD_SCENE_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment