接下我们来学习cocos2dx 3.2制作Flappy Bird实践建立Land陆地层。首先设置AppDelegate.cpp,修改屏幕大小,并将第一个场景设置为WelcomeScene:
#include “AppDelegate.h”
#include “WelcomeScene.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::createWithRect(“FlappyBird”,Rect(0,0,286,512),1.0f);
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don’t call this
director->setAnimationInterval(1.0 / 60);
//将WelcomeScene作为游戏的第一个场景
auto welcomeScene = WelcomeScene::createScene();
// run
director->runWithScene(welcomeScene);
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();
}
创建Land类,继承Layer:
#include “cocos2d.h”
USING_NS_CC;
class Land : public Layer
{
public:
Land(void);
~Land(void);
virtual bool init();
CREATE_FUNC(Land);
//地面开始滚动
void landMove(float);
//地面停止滚动
void landStop();
private:
Sprite *land1;
Sprite *land2;
};#include “Land.h”
Land:and(void)
{
}
Land::~Land(void)
{
}
bool Land::init()
{
if (!Layer::init())
{
return false;
}
auto origin=Director::getInstance()->getVisibleOrigin();
auto visibleSize=Director::getInstance()->getVisibleSize();
//创建2个land,实现滚动效果
land1 = Sprite::createWithSpriteFrameName(“land.png”);
land1->setPosition(Point(origin.x+land1->getContentSize().width/2,origin.y+land1->getContentSize().height/2));
//下面这个接口是防止两个精灵叠加时出现黑边
land1->getTexture()->setAliasTexParameters();
auto land1_body = PhysicsBody::create();
auto land1_shape = PhysicsShapeBox::create(land1->getContentSize());
land1_body->addShape(land1_shape);
land1_body->setDynamic(false);//设置为静态物体
land1_body->setGravityEnable(false);//不受重力影响
land1_body->setCategoryBitmask(1);
land1_body->setCollisionBitmask(-1);
land1_body->setContactTestBitmask(-1);
land1_body->setLinearDamping(0.7f);//线性阻尼
land1->setPhysicsBody(land1_body); //绑定land1和lan1_body
addChild(land1);
land2 = Sprite::createWithSpriteFrameName(“land.png”);
land2->setPosition(Point(origin.x+land1->getPositionX()+land1->getContentSize().width-2,land1->getPositionY()));
land2->getTexture()->setAliasTexParameters();
auto land2_body = PhysicsBody::create();
auto land2_shape = PhysicsShapeBox::create(land2->getContentSize());
land2_body->addShape(land2_shape);
land2_body->setDynamic(false);
land2_body->setGravityEnable(false);
land2_body->setCategoryBitmask(1);
land2_body->setCollisionBitmask(-1);
land2_body->setContactTestBitmask(-1);
land2_body->setLinearDamping(0.7f);
land2->setPhysicsBody(land2_body);
addChild(land2);
schedule(schedule_selector(Land::landMove), 1/60);//启动计时器
return true;
}
void Land::landMove(float)
{
land1->setPositionX(land1->getPositionX()-2);
land2->setPositionX(land1->getPositionX()+land1->getContentSize().width-2);
if (land2->getPositionX()==land2->getContentSize().width/2)
{
land1->setPositionX(land2->getContentSize().width/2);
}
}
void Land::landStop()
{
unschedule(schedule_selector(Land::landMove));
}