cocos2d-x初步

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);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值