Cocos2d-x簡單遊戲程式碼實現|第三部分:引導層
#ifndef __ShootPlane__InstroLayer__
#define __ShootPlane__InstroLayer__
#include <iostream>
#include "commonHeader.h"
//引導層
class IntroLayer:publicCCLayer {
private:
//設定背景
CCSprite *background1;
CCSprite *backgroudd2;
//自適應背景
int adjustmentBg;
//更新
void update(float delate);
//載入背景
void loadBackground();
//背景輪換
void backgroundScrollRelpace();
public:
//建構函式及解構函式
IntroLayer();
~IntroLayer();
//建立場景
CREATE_FUNC(IntroLayer);
virtual bool init();//初始化場景
//
SCENE_FUNC(IntroLayer);
virtual void onEnter();//系統函式
};
#endif /* defined(__ShootPlane__InstroLayer__) */
#include "InstroLayer.h"
#include "HelloWorldScene.h"
IntroLayer::IntroLayer(){
}
IntroLayer ::~IntroLayer(){
}
//載入背景
voidIntroLayer ::loadBackground(){
//設定背景1屬性並新增到檢視上
this->background1 =CCSprite::createWithSpriteFrameName("background_2.png");
this->background1->setAnchorPoint
this->background1->setPosition(ccp(160,adjustmentBg));
this->addChild(background1,0);
//設定背景2
this->backgroudd2 =CCSprite::createWithSpriteFrameName("background_2.png");
this->backgroudd2->setAnchorPoint(ccp(0.5,0));
this->backgroudd2->setPosition(ccp(160,568 + adjustmentBg));
this->addChild(backgroudd2,0);
}
//背景輪換滾動
voidIntroLayer::backgroundScrollRelpace(){
adjustmentBg--;
if (adjustmentBg <=0) {
adjustmentBg = 568;
}
background1->setPosition(ccp(160,adjustmentBg));
backgroudd2->setPosition(ccp(160,adjustmentBg - 568));
}
//更新
voidIntroLayer::update(float delate){
//呼叫背景更換
this->backgroundScrollRelpace();
}
boolIntroLayer::init(){
kCCLAYER_IS_INIT;
//載入貼圖快取
CCTexture2D *texture =CCTextureCache::sharedTextureCache()->textureForKey("gameArts.png");
//載入批量幀
CCSpriteBatchNode *spriteBatch =CCSpriteBatchNode::createWithTexture(texture);
this->addChild(spriteBatch);
//呼叫載入檢視方法
this->loadBackground();
this->scheduleUpdate();//呼叫定時器更新
return true;
}
voidIntroLayer::onEnter(){
//例項化helloWorld場景
CCScene *pScene = HelloWorld::scene();
//建立切換場景模式
CCTransitionFade *transitionScene =CCTransitionFade::create(1.0,pScene,ccWHITE);
//切換場景
CCDirector::sharedDirector()->replaceScene(transitionScene);
}