#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
startflag = true;
score = 0;
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
scorelabel = LabelTTF::create("score :0", "Arial", 24);
scorelabel->setPosition(Point(5, visibleSize.height - 5));
scorelabel->setAnchorPoint(Vec2(0, 1));
this->addChild(scorelabel, 10);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("tex.plist");
setTouchEnabled(true);
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
//调用方法
this->initPhysics();
this->addground();
this->addbird();
return true;
}
void HelloWorld::initPhysics()
{
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
world = new b2World(gravity);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
world->SetContactListener(this);
}
void HelloWorld::addbar(float t)
{
float offset = -rand() % 5;
//down
auto down = bird::create("down_bar.png");
down->setPosition(Vec2(visibleSize.width + 2 * RATIO, visibleSize.height / 2 + offset*RATIO));
Size size = down->getContentSize();
b2BodyDef bodydef;
bodydef.type = b2_kinematicBody;
bodydef.position = b2Vec2(visibleSize.width / RATIO + 2, size.height / RATIO / 2 + offset);
b2Vec2 a;
a.Set(-5.0f, 0.0f);
bodydef.linearVelocity = a;
b2Body *downbody = world->CreateBody(&bodydef);
b2PolygonShape shape;
shape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef fixtexdef;
fixtexdef.shape = &shape;
downbody->CreateFixture(&fixtexdef);
downbody->SetUserData(down);
this->addChild(down,2);
//up
auto up = bird::create("up_bar.png");
up->setPosition(Vec2(visibleSize.width + 2 * RATIO, size.height + offset*RATIO + 2 * RATIO));
Size upsize = up->getContentSize();
b2BodyDef upbodydef;
upbodydef.type = b2_kinematicBody;
upbodydef.position = b2Vec2(visibleSize.width / RATIO + 2,
size.height/RATIO
+
upsize.height / 2 / RATIO
+
2
+
offset / RATIO
);
upbodydef.linearVelocity = a;
b2Body *upbody = world->CreateBody(&upbodydef);
b2PolygonShape upshape;
upshape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef upfixtexdef;
upfixtexdef.shape = &upshape;
upbody->CreateFixture(&upfixtexdef);
upbody->SetUserData(up);
this->addChild(up, 2);
score++;
char chara[10];
char endc[20]="score :";
sprintf(chara, "%d", score-1);
strcat(endc, chara);
scorelabel->setString(endc);
}
void HelloWorld::addbird()
{
H_bird = Sprite::createWithSpriteFrameName("bird.png");
H_bird->setPosition(visibleSize.width/ 2, visibleSize.height/ 2);
this->addChild(H_bird);
Size size = H_bird->getContentSize();
b2BodyDef bodydef;
bodydef.type = b2_dynamicBody;
bodydef.position = b2Vec2(visibleSize.width / 2 / RATIO, visibleSize.height / 2 / RATIO);
body = world->CreateBody(&bodydef);
b2PolygonShape shape;
shape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef birdfixturedef;
birdfixturedef.shape = &shape;
body->CreateFixture(&birdfixturedef);
body->SetUserData(H_bird);
}
void HelloWorld::update(float dt)
{
world->Step(dt, 8, 1);
for (b2Body* b = world->GetBodyList(); b!=nullptr; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
Sprite* sprite = (Sprite*)b->GetUserData();
sprite->setPosition(Vec2(b->GetPosition().x *
RATIO, b->GetPosition().y * RATIO));
sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
}
void HelloWorld::addground()
{
auto ground = bird::create("ground.png");
//this->addChild(ground);
Size size = ground->getContentSize();
ground->setPosition(size.width / 2, size.height / 2);
b2BodyDef bodydef;
bodydef.type = b2_staticBody;
bodydef.position = b2Vec2(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2PolygonShape shape;
shape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef fixdef;
fixdef.shape = &shape;
auto body = world->CreateBody(&bodydef);
body->CreateFixture(&fixdef);
ground->setUserData(body);
this->addChild(ground,100);
}
bool HelloWorld::onTouchBegan(Touch *touch, Event *event)
{
if (startflag)
start();
else
body->SetLinearVelocity(b2Vec2(0, 5));
return true;
}
void HelloWorld::start()
{
schedule(schedule_selector(HelloWorld::addbar), 1);
this->scheduleUpdate();
startflag = false;
}
void HelloWorld::stop()
{
unschedule(schedule_selector(HelloWorld::addbar));
unscheduleUpdate();
auto image = MenuItemImage::create("start-nor.png", "start-pre.png", CC_CALLBACK_0(HelloWorld::menurestart,this));
auto m = Menu::create(image, NULL);
m->setAnchorPoint(Vec2::ZERO);
this->addChild(m,50);
}
void HelloWorld::BeginContact(b2Contact *contact)
{
auto SpriteA = contact->GetFixtureA()->GetBody()->GetUserData();;
auto SpriteB = contact->GetFixtureB()->GetBody()->GetUserData();
if (SpriteA == H_bird || SpriteB == H_bird)
{
stop();
}
}
void HelloWorld::menurestart()
{
auto sc = HelloWorld::createScene();
Director::getInstance()->replaceScene(sc);
}
flappy bird 基于cocos2d-x 3.8
需积分: 9 50 浏览量
2015-10-06
14:01:17
上传
评论
收藏 514KB ZIP 举报
王小北爱搬砖
- 粉丝: 34
- 资源: 28