HelloWorldScene.cpp
#include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //添加一个背景颜色图层
CCSize s = CCDirector::sharedDirector()->getWinSize(); CCLayer *colorlayer = CCLayerColor::create(ccc4(0x00, 0xff, 0xff, 0xff), s.width, s.height); colorlayer->ignoreAnchorPointForPosition(false); colorlayer->setPosition(s.width / , s.height / ); scene->addChild(colorlayer, , colorlayer->getTag()); // add layer as a child to scene
//将主图层添加都背景图层中
colorlayer->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} //取得屏幕的大小
screenSize = CCDirector::sharedDirector()->getVisibleSize(); //初始化世界
initWorld(); //添加小鸟、障碍物容器及地面
addBird();
addBarContainer();
addGround(); //每隔一秒添加一组障碍物
scheduleUpdate();
schedule(schedule_selector(HelloWorld::addBar), 1.0f); //设置允许触摸
setTouchEnabled(true); return true;
} void HelloWorld::initWorld()
{
//重力加速度:纵向9.8,横向 0
world = new b2World(b2Vec2(, -9.8f));
//添加监视
world->SetContactListener(this);
} void HelloWorld::startGame(float dt){
scheduleUpdate();
schedule(schedule_selector(HelloWorld::addBar), );
} void HelloWorld::stopGame(){
unscheduleUpdate();
unschedule(schedule_selector(HelloWorld::addBar));
} void HelloWorld::addBird()
{
bird = B2Sprite::create("bird.png");
CCSize size = bird->getContentSize(); //添加小鸟
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(screenSize.width//RATIO, screenSize.height//RATIO);
b2Body * birdBody = world->CreateBody(&bodyDef); //设置边界
b2PolygonShape birdShape;
birdShape.SetAsBox(size.width//RATIO, size.height//RATIO); //碰撞
b2FixtureDef birdFixtureDef;
birdFixtureDef.shape= &birdShape;
birdBody->CreateFixture(&birdFixtureDef); bird->setPTMRatio(RATIO);
bird->setB2Body(birdBody);
addChild(bird);
} void HelloWorld::addBar(float dt)
{
float offset = -rand()%; //downbar
B2Sprite *downBar = B2Sprite::create("down_bar.png");
CCSize downBarSize = downBar->getContentSize(); b2BodyDef downBarBodyDef;
downBarBodyDef.type = b2_kinematicBody;
downBarBodyDef.position = b2Vec2(screenSize.width/RATIO + , downBarSize.height/RATIO/ + offset);
downBarBodyDef.linearVelocity = b2Vec2(-5.0f, ); b2PolygonShape downBarShape;
downBarShape.SetAsBox(downBarSize.width/RATIO/, downBarSize.height/RATIO/); b2FixtureDef downBarFixtureDef;
downBarFixtureDef.shape = &downBarShape; b2Body *downBarBody = world->CreateBody(&downBarBodyDef);
downBarBody->CreateFixture(&downBarFixtureDef); downBar->setB2Body(downBarBody);
downBar->setPTMRatio(RATIO); //upbar
B2Sprite *upBar = B2Sprite::create("up_bar.png");
CCSize upBarSize = upBar->getContentSize(); b2BodyDef upBarBodyDef;
upBarBodyDef.type = b2_kinematicBody;
upBarBodyDef.position = b2Vec2(screenSize.width/RATIO + , upBarSize.height/RATIO/ + offset + downBarSize.height/RATIO + 2.5f);
upBarBodyDef.linearVelocity = b2Vec2(-, ); b2PolygonShape upBarShape;
upBarShape.SetAsBox(upBarSize.width/RATIO/, upBarSize.height/RATIO/); b2FixtureDef upBarFixtureDef;
upBarFixtureDef.shape = &upBarShape; b2Body *upBarBody = world->CreateBody(&upBarBodyDef);
upBarBody->CreateFixture(&upBarFixtureDef); upBar->setB2Body(upBarBody);
upBar->setPTMRatio(RATIO); barContainer->addChild(upBar);
barContainer->addChild(downBar);
} void HelloWorld::addBarContainer()
{
//将所有的障碍物包裹在一个容器中
barContainer = CCSprite::create();
addChild(barContainer);
} void HelloWorld::addGround()
{
B2Sprite *ground = B2Sprite::create("ground.png");
CCSize size = ground->getContentSize(); b2BodyDef bDef;
bDef.type = b2_staticBody;
bDef.position = b2Vec2(size.width//RATIO, size.height//RATIO);
b2Body *groundBody = world->CreateBody(&bDef); b2PolygonShape groundShape;
groundShape.SetAsBox(size.width//RATIO, size.height//RATIO); b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundBody->CreateFixture(&groundFixtureDef); ground->setPTMRatio(RATIO);
ground->setB2Body(groundBody);
addChild(ground);
} void HelloWorld::update(float dt)
{
world->Step(dt, , ); CCSprite *sprite; //取得世界中的所有的body
b2Body *node = world->GetBodyList(); //遍历世界中的body
while(node)
{
b2Body *body = node;
node = node->GetNext();
sprite = (CCSprite*)body->GetUserData(); //超出屏幕左边界,销毁
if (body->GetPosition().x < -)
{
if (sprite != NULL)
{
//在屏幕上清除
sprite->removeFromParent();
}
//在内存中销毁
world->DestroyBody(body);
}
}
} void HelloWorld::BeginContact(b2Contact *contact){
//碰撞检测,有一个为鸟游戏就结束
if (contact->GetFixtureA()->GetBody()->GetUserData()==bird || contact->GetFixtureB()->GetBody()->GetUserData()==bird)
{
//游戏结束,弹出dialog
stopGame();
CCMessageBox("Game Over", "Alert");
}
} void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
//触碰屏幕时,小鸟向上运动,重力加速度:纵向 5,横向 0
bird->getB2Body()->SetLinearVelocity(b2Vec2(, 5.0f));
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
#endif
}
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "Box2D\Box2D.h"
#include "B2Sprite.h" #define RATIO 72.0f class HelloWorld : public cocos2d::CCLayer,public b2ContactListener
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld); virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void BeginContact(b2Contact* contact); virtual void update(float dt); b2World *world;
B2Sprite *bird; CCSize screenSize; CCSprite *barContainer; private:
void addBird();
void addGround();
void initWorld();
void addBar(float dt);
void addBarContainer();
void startGame(float dt);
void stopGame();
}; #endif // __HELLOWORLD_SCENE_H__