1、cocos2d-x 基本類介紹
CCScence:場景,一個遊戲有多個場景組成,比如載入場景,遊戲主場景,通關動畫場景,結束場景等
。場景由舞臺佈景和演員(精靈)組成
CCLayer:繼承自CCNode,舞臺佈景,每個場景至少有一個舞臺,舞臺上包括幕布,音樂等環境。
CCSprite:精靈,相當於舞臺上表演的演員
CCDirector:導演,控制整個遊戲過程,包括開始,暫停,結束,指揮場景切換等
CCAction:動作
2、代碼
遊戲情境類:
#include "HelloWorldScene.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "GameOverSence.h"
USING_NS_CC;
using namespace std;
CCScene* HelloWorld::scene()
{
CCScene* scene = CCScene::create();
HelloWorld* layer = HelloWorld::create();
layer->setTag(100);
scene->addChild(layer);
return scene;
}
//根據給定圖片,座標 生成一個精靈
CCSprite* HelloWorld::newSprite(char* png,
float x,
float y)
{
CCSprite* sprite = CCSprite::create(png);
//ccp(float x,float y):產生座標
sprite->setPosition(ccp(x,y));
return sprite;
}
bool HelloWorld::init()
{
HelloWorld::countBullet1=0;
HelloWorld::countBullet2=0;
HelloWorld::bullets = new
CCArray(); //CCArray要new出來,不能調用create
bullets->retain();
if ( !CCLayerColor::initWithColor(ccc4(34,34,23,
255)))
{
return false;
}
//定時器
this->schedule(schedule_selector(
HelloWorld::update),0.3f);
//碰撞檢測,每幀執行一次
this->schedule(schedule_selector(
HelloWorld::collision));
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
char* name = "bird.png";
bird=newSprite(name,20.0f,size.height/2);
this->addChild(bird);
//精靈執行一組動作
//旋轉
CCRotateBy* rotate = CCRotateBy::create(4.0f,
360.0f*8);
//移動
CCMoveTo* to = CCMoveTo::create(2.0f,
ccp(size.width,size.height/2));
CCMoveTo* back = CCMoveTo::create(2.0f,
ccp(20.0f,size.height/2));
//順序執行
CCSequence* sequence = CCSequence::create(to,back,
NULL);
//同步執行
CCSpawn* spawn = CCSpawn::create(rotate,sequence,
NULL);
//重複執行
CCRepeatForever* repeate =
CCRepeatForever::create(spawn);
bird->runAction(repeate);
//菜單操作
CCMenuItemImage* start =
CCMenuItemImage::create("CloseNormal.png",
"CloseSelected.png",this,
menu_selector(HelloWorld::menuChose));
CCMenu* menu = CCMenu::create(start,NULL);
menu->setPosition(size.width-15,size.height-15);
this->addChild(menu);
addLabel();
return true;
}
void HelloWorld::addLabel()
{
HelloWorld::ttf1 =
CCLabelTTF::create("Bullet1=:0",
"Helvetica",
20.0f);
HelloWorld::ttf2 =
CCLabelTTF::create("Bullet2=:0",
"Helvetica",
20.0f);
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
ttf1->setPosition(ccp(
ttf1->getContentSize().width/2,
size.height-ttf1->getContentSize().height));
ttf2->setPosition(ccp(size.width
-ttf2->getContentSize().width,
size.height-ttf2->getContentSize().height));
ttf1->setTag(11);
ttf2->setTag(22);
this->addChild(ttf1);
this->addChild(ttf2);
}
void HelloWorld::collision(float time)
{
CCObject* temp = NULL;
CCARRAY_FOREACH(HelloWorld::bullets,temp)
{
CCSprite* sprite = (CCSprite*)temp;
CCRect bulletRect = CCRectMake(
sprite->getPositionX(),
sprite->getPositionY(),
sprite->getContentSize().width,
sprite->getContentSize().height);
CCRect birdRect = CCRectMake(
bird->getPositionX(),
bird->getPositionY(),
bird->getContentSize().width,
bird->getContentSize().height);
if(birdRect.intersectsRect(bulletRect))
{
//從數組中移除bullet,從場景中移除bullet
HelloWorld::bullets->removeObject(sprite);
int tag = sprite->getTag();
setSenceColor(tag);
sprite->removeFromParentAndCleanup(true);
}
}
}
void HelloWorld::setSenceColor(int tag)
{
if(tag%2!=0)
{
//切換背景色1
CCLog("color1");
CCLayerColor::initWithColor(ccc4(194,194,194,
255));
HelloWorld::countBullet1++;
}
else
{
//切換背景色2
CCLog("color2");
CCLayerColor::initWithColor(ccc4(255,127,24,
255));
HelloWorld::countBullet2++;
}
setLabelTtf(HelloWorld::countBullet1,
HelloWorld::countBullet2);
}
void HelloWorld::setLabelTtf(int countBullet1,
int countBullet2)
{
char strValue[100],strValue2[100];
itoa(countBullet1,strValue,10);
itoa(countBullet2,strValue2,10);
std::string str1 = "Bullet1="+string(strValue);
std::string str2 = "Bullet2="+string(strValue2);
char gs[100],gs2[100];
strcpy(gs, str1.c_str());
strcpy(gs2, str2.c_str());
CCLog(gs);
CCLog(gs2);
ttf1->setString(gs);
ttf2->setString(gs2);
cocos2d::CCScene* gameOverSence =NULL;
if(countBullet1>countBullet2+2)
{
gameOverSence =GameOver::scene(1);
CCDirector::sharedDirector()->replaceScene(
gameOverSence);
}
else
if(countBullet2>countBullet1+2)
{
gameOverSence =GameOver::scene(2);
CCDirector::sharedDirector()->replaceScene(
gameOverSence);
}
}
void HelloWorld::update(float time)
{
//產生一個怪物隨機出現在頂部或者底部,向對面運動,碰到bird消失
int randValue = rand()%2;
char strValue[100];
itoa(randValue,strValue,10);
CCLog(strValue);
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
int x = rand()%(int)size.width;
int y = 0;
CCMoveTo* move=CCMoveTo::create(1.0f,ccp(x,0));
CCSprite* bullet = NULL;
if(randValue==0)
{
//頂部,bullet1,向下運動
y = size.height;
bullet=newSprite("bullet1.png",x,y);
bullet->setTag(1);
}
else
{
//底部,bullet2,向上運動
y = 0;
bullet=newSprite("bullet2.png",x,y);
move=CCMoveTo::create(1.0f,ccp(x,size.height));
bullet->setTag(2);
}
bullet->setPosition(ccp(x,y));
this->addChild(bullet);
HelloWorld::bullets->addObject(bullet);
CCCallFuncN* call = CCCallFuncN::create(this,
callfuncN_selector(HelloWorld::dispear));
CCSequence* sequence = CCSequence::create(move,
call,NULL);
bullet->runAction(sequence);
}
void HelloWorld::dispear(CCNode* who)
{
who->removeFromParentAndCleanup(true);
HelloWorld::bullets->removeObject(who);
}
void HelloWorld::menuChose(CCObject* sender)
{
CCDirector* director =
CCDirector::sharedDirector();
bool isPause = director->isPaused();
if(!isPause)
{
director->pause();
CCLog("pause");
}
else
{
director->resume();
CCLog("restart");
}
}
//HelloWorld::~HelloWorld()
//{
// if(bird)
// {
// HelloWorld::bird->release();
// }
//}
void HelloWorld::menuCloseCallback(
CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.",
"Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
遊戲情境頭文件
#define __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#if 0
CCScence:場景,一個遊戲有多個場景組成,比如載入場景,遊戲主場景,通關動畫場景,結束場景等
。場景由舞臺佈景和演員(精靈)組成
CCLayer:繼承自CCNode,舞臺佈景,每個場景至少有一個舞臺,舞臺上包括幕布,音樂等環境。
CCSprite:精靈,相當於舞臺上表演的演員
CCDirector:導演,控制整個遊戲過程,包括開始,暫停,結束,指揮場景切換等
CCAction:動作
#endif
class HelloWorld : public cocos2d::CCLayerColor
{
public:
//聲明cocos2d變量必須制定命名空間
cocos2d::CCLabelTTF* ttf1;
cocos2d::CCLabelTTF* ttf2;
int countBullet1;
int countBullet2;
cocos2d::CCArray* bullets;
cocos2d::CCSprite* bird;
//初始化
virtual bool init();
//產生場景對象
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
//調用宏,目的爲在create的時候自動init並將對象加入到自動回收機制
CREATE_FUNC(HelloWorld);
cocos2d::CCSprite* newSprite(
char* png,float x,
float y);
void HelloWorld::menuChose(CCObject* sender);
// HelloWorld::~HelloWorld();
void HelloWorld::update(float time);
void HelloWorld::dispear(CCNode* who);
void HelloWorld::collision(float time);
void HelloWorld::setSenceColor(int tag);
void HelloWorld::setLabelTtf(int countBullet1,
int countBullet2);
void HelloWorld::addLabel();
};
遊戲結束情境類
#include "GameOverSence.h"
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string>
USING_NS_CC;
using namespace std;
bool GameOver::init()
{
ttf = CCLabelTTF::create();
if(!CCLayer::init())
{
return false;
}
CCLog("-----init----is called");
}
cocos2d::CCScene* GameOver::scene(int temp)
{
CCLog("------scence-----is called");
CCScene* scene = CCScene::create();
GameOver* layer = GameOver::create();
layer->setTag(temp);
layer->pagenum = temp;
layer->initString();
scene->addChild(layer);
return scene;
}
void GameOver::initString()
{
CCSize size =
CCDirector::sharedDirector()->getVisibleSize();
ttf->setPosition(ccp(size.width/2,
size.height/2));
if(this->pagenum==1)
{
ttf->setString("GameOver,bullet1 win");
}
else
if(this->pagenum==2)
{
ttf->setString("GameOver,bullet2 win");
}
else
{
ttf->setString("Nobody win");
}
addChild(ttf);
}
結束情境頭文件
#define __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameOver:public cocos2d::CCLayer
{
public:
int pagenum;
cocos2d::CCLabelTTF* ttf;
virtual bool init();
void initString();
//產生場景對象
static cocos2d::CCScene* scene(int temp);
CREATE_FUNC(GameOver);
};