cocos2d-x 3.0sprite轮换

主要是看了 偶尔e网事 童鞋提出的一篇,主要是改下代码风格,coco2d-x设计木有风格,是copy的,所以努力走自己的风格。

就是sence里面加layer,运行sence渲染layer。

最讨厌那么多static函数了,这根objected programming有毛关系?

果断修改:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto eglView = EGLView::getInstance();

    director->setOpenGLView(eglView);
	
    // 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);

    // create a scene. it's an autorelease object
    auto scene = new GameSence();

    // run
    director->runWithScene(scene);

    return true;
}

然后sence类很简单,很清爽:

class GameSence : public cocos2d::Scene
{
public:
	GameSence();

private:
	GameLayer* m_pGameLayer;
};
GameSence::GameSence()
{
	m_pGameLayer = new GameLayer;
	addChild(m_pGameLayer);
}

GameLayer也很简单:

class GameLayer : public cocos2d::Layer
{
public:
	GameLayer();
	void backGroupMove(float dt);

private:
	cocos2d::Sprite* m_pBackground1;
	cocos2d::Sprite* m_pBackground2;
};
GameLayer::GameLayer()
{
	cocos2d::Layer::init();

	auto spriteFrameCache = cocos2d::SpriteFrameCache::sharedSpriteFrameCache(); 
	spriteFrameCache->addSpriteFramesWithFile("shoot_background.plist");

	m_pBackground1= Sprite::createWithSpriteFrame(spriteFrameCache->getSpriteFrameByName("background.png"));
	m_pBackground1->setPosition(ccp(0,0));
	m_pBackground1->setAnchorPoint(ccp(0,0));
	addChild(m_pBackground1);

	m_pBackground2= Sprite::createWithSpriteFrame(spriteFrameCache->getSpriteFrameByName("background.png"));
	m_pBackground2->setPosition(ccp(0,m_pBackground2->getContentSize().height-5));
	m_pBackground2->setAnchorPoint(ccp(0,0));
	addChild(m_pBackground2);

	schedule(schedule_selector(GameLayer::backGroupMove), 0.01f);
}

void GameLayer::backGroupMove(float dt)
{
	m_pBackground1->setPositionY(m_pBackground1->getPositionY()-2);
	m_pBackground2->setPositionY(m_pBackground1->getPositionY() + m_pBackground1->getContentSize().height -2);

	if (m_pBackground2->getPositionY() == 0)
	{
		m_pBackground1->setPositionY(0);
	}
}

然后显示就是打飞机背景一直在动,飞机飞啊,飞到 啦啦啦!

你可能感兴趣的:(cocos2d-x 3.0sprite轮换)