#include "PlayScene.h"
USING_NS_CC;
Scene* PlayScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
//添加重力场
scene->getPhysicsWorld()->setGravity(Vec2(0, -200));
// 'layer' is an autorelease object
auto layer = PlayScene::create();
//绑定重力场
layer->setPhysicWorld(scene->getPhysicsWorld());
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool PlayScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
//播撒随机种子
srand(time(NULL));
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
winSize = Director::getInstance()->getWinSize();
/*gameLayer = Layer::create();
this->addChild(gameLayer);*/
land1 = Sprite::create("land.png");
land1->setAnchorPoint(Vec2(0, 0));
land1->setPosition(Vec2::ZERO);
this->addChild(land1);
auto width = land1->getContentSize().width;
land2 = Sprite::create("land.png");
land2->setAnchorPoint(Vec2(0, 0));
land2->setPosition(Vec2(width, 0));
this->addChild(land2);
birdSprite = Sprite::create("bird_0.png");
birdSprite->setPosition(visibleSize.width / 4, visibleSize.height / 2);
//为小鸟绑定刚体
auto birdBody = PhysicsBody::createBox(birdSprite->getContentSize());
birdBody->setDynamic(true);
birdBody->setContactTestBitmask(1);
birdBody->setRotationEnable(true);
//birdBody->setGravityEnable(true);
birdSprite->setPhysicsBody(birdBody);
auto animate = Animate::create(createAnimation("bird", 3, 0.1));
auto flyAction = RepeatForever::create(animate);
auto moveTo = MoveTo::create(1, Vec2(visibleSize.width, visibleSize.height / 2));
birdSprite->runAction(flyAction);
this->addChild(birdSprite,10);
this->schedule(schedule_selector(PlayScene::addEnemy),1.0f);
this->scheduleUpdate();
// this->addEnemy();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(PlayScene::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool PlayScene::onTouchBegan(Touch *touch, Event *unused_event) {
birdSprite->getPhysicsBody()->setVelocity(Vec2(0, 250));
return false;
}
void PlayScene::update(float dt) {
// addEnemy();
land1->setPositionX(land1->getPositionX() - 5);
land2->setPositionX(land2->getPositionX() - 5);
checkBgSprite();
}
void PlayScene::addEnemy(float dt) {
auto enemy = Sprite::create("enemy_flower.png");
int curheight = getRand(0, enemy->getContentSize().height );
enemy->setPosition(winSize.width - 60, curheight);
if (enemy == NULL) {
log("enemy", "nulllllllllllllllllllllll");
}
this->addChild(enemy,10);
}
void PlayScene::checkBgSprite() {
if (land1->getPositionX() <= -land1->getContentSize().width) {
log("should change land1-----------------");
land1->setPositionX(land2->getContentSize().width);
}
if (land2->getPositionX() <= -land2->getContentSize().width) {
log("should change land2-----------------");
land2->setPositionX(land1->getContentSize().width);
}
}
int PlayScene::getRand(int start,int end) {
int i = CCRANDOM_0_1(end - start + 1) + start;
return (int)i;
}
Animation* PlayScene::createAnimation(std::string prefixName, int framesNum, float delay) {
auto animation = Animation::create();
for (int i = 0; i < framesNum; i++) {
char buffer[20] = { 0 };
sprintf(buffer, "_%i.png", i);
std::string str = prefixName + buffer;
animation->addSpriteFrameWithFileName(str);
}
return animation;
}
void PlayScene::setPhysicWorld(PhysicsWorld* world) {
this->m_world = world;
}
void PlayScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}