cocos2d-x游戏开发之一


             在code4app中看到一个cocos2d-xdemo觉得很适合初学者,原demo是用cocos2d-iphone写的,在工作之余我改用cocos2d-x完成那个小游戏。


今天首先把游戏主界面和关于界面搭建出来,日后会逐步完善。先上两张图看一下游戏主界面和游戏帮助界面、


首页

cocos2d-x游戏开发之一_第1张图片


帮助界面



看到这两张图是不是喜欢逛code4app的屌丝会很熟悉啊


很简单的上代码了


首页

#include "StartGameSecene.h"
#include "AboutGameSecene.h"
#include "ChapterSelect.h"
#include "Constants.h"
 
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
 

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* StartGame::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    StartGame *layer = StartGame::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool StartGame::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
 
    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();
   
    CCLog("设备屏幕尺寸大小 ==  %f",size.height);
 
    
    //判断设备
    StartGame::judgeDeviceType();
 
    CCSprite* pSprite_bg = NULL;
    
    CCMenuItemImage* startItem = NULL;
    
    CCMenuItemImage* aboutItem = NULL;
    
    if(Noiphone5_hd_winsize == deviceType)
    {
        
        pSprite_bg = CCSprite::create("bg_main-hd.png");
        
        startItem = CCMenuItemImage::create(
                                            "button_start-hd.png", "button_start-hd.png", this, menu_selector(StartGame::imageMenuStartGame));
        
        aboutItem = CCMenuItemImage::create(
                                "button_about-hd.png", "button_about-hd.png", this, menu_selector(StartGame::imageMenuAboutGame));
        
    }
    else if(Iphone5_winsize == deviceType)
    {
        pSprite_bg = CCSprite::create("bg_main-iphone5.png");
        
        startItem = CCMenuItemImage::create(
                                            "button_start-hd.png", "button_start-hd.png", this, menu_selector(StartGame::imageMenuStartGame));
        
        aboutItem = CCMenuItemImage::create(
                                "button_about-hd.png", "button_about-hd.png", this, menu_selector(StartGame::imageMenuAboutGame));
    }
    else
    {
        pSprite_bg = CCSprite::create("bg_main.png");
        
        startItem = CCMenuItemImage::create(
                                            "button_start.png", "button_start.png", this, menu_selector(StartGame::imageMenuStartGame));
        
        aboutItem = CCMenuItemImage::create(
                                            "button_about.png", "button_about.png", this, menu_selector(StartGame::imageMenuAboutGame));
    }
    
    // position the sprite on the center of the screen
    pSprite_bg->setPosition( ccp(size.width/2, size.height/2) );
    
  
    //创建一个菜单,并设置位置
    CCMenu* homeMenu = CCMenu::create(startItem,aboutItem,NULL);
    
    startItem->setPosition(ccp(size.width/2-120,size.height/2+70));
    
    aboutItem->setPosition(ccp(size.width/2+120,size.height/2+70));
  
    homeMenu->setPosition(CCPointZero);
    
    //将图片菜单加入到布景中,第二个参数表示在Z轴的层次,屏幕由里到外从小到大额
    
    pSprite_bg->addChild(homeMenu,1);
    
    //背景音乐
    
    StartGame::MplayBackgroundMusic();
  
    // add the sprite as a child to this layer
    this->addChild(pSprite_bg, 0);
    
    return true;
}

//开始游戏
void StartGame::imageMenuStartGame(CCObject* pSender)
{
 
    CCDirector::sharedDirector()->pushScene(CCTransitionFade::create(1, ChapterSelect::scene()));
  
}

//关于游戏
void StartGame::imageMenuAboutGame(CCObject* pSender)
{
    
   CCDirector::sharedDirector()->pushScene( CCTransitionFade::create(1, AboutGame::scene()) );
 
}

//判断设备
void StartGame::judgeDeviceType()
{
    
    CCSize result = CCDirector::sharedDirector()->getWinSize();
     
    if(result.width ==960.f){
 
        deviceType = Noiphone5_hd_winsize;
    }else if(result.width ==1136.f){
 
        deviceType = Iphone5_winsize;
    }else if(result.width == 480.f)
    {
        deviceType = Noiphone5_winsize;
    }
    
}

#pragma mark 播放背景音乐

//添加背景音乐
void StartGame::MplayBackgroundMusic()
{
    
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("soft.mp3",true);
   
}

关于游戏界面



#include "AboutGameSecene.h"

using namespace cocos2d;


CCScene* AboutGame::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    AboutGame *layer = AboutGame::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

bool AboutGame::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
   
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "button_back-hd.png",
                                                          "button_back-hd.png",
                                                          this,
                                                          menu_selector(AboutGame::BackToRootScene) );
    pCloseItem->setPosition( ccp(size.width/2+400 , size.height/2+230) );
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);
    
    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("bg_common-iphone5.png");
   
    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(size.width/2, size.height/2) );
    
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    AboutGame::addTextLabel();
    
    return true;
}

void AboutGame::BackToRootScene(CCObject* pSender)
{
    CCDirector::sharedDirector()->popScene();
 
}

//添加字幕
void AboutGame::addTextLabel(){
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
   
    CCLabelTTF *label1 = CCLabelTTF::create("Copyright Happy Bubsy 2013", "ChalkboardSE-Bold", 36);
    
    label1->setPosition(ccp(size.width/2,0));
    
    label1->setColor(ccc3(16,174,231));
    
    this->addChild(label1);
 
    
    CCMoveTo *moveTo = CCMoveTo::create(4, ccp(size.width/2,480));
    
    label1->runAction(moveTo);
     
 
    CCLabelTTF *label2 = CCLabelTTF::create("Designed by Bubsy Brother", "ChalkboardSE-Bold", 36);
   
    label2->setPosition(ccp(size.width/2,-65));
    
    label2->setColor(ccc3(16,174,231));
    
    this->addChild(label2);
    
    CCMoveTo *moveTo1 = CCMoveTo::create(4, ccp(size.width/2,480-65));
    
    label2->runAction(moveTo1);
   
   
    CCLabelTTF *label3 = CCLabelTTF::create("Arts by Bubsy Sister", "ChalkboardSE-Bold", 36);
    
    label3->setPosition(ccp(size.width/2,-130));
    
    label3->setColor(ccc3(16,174,231));
    
    this->addChild(label3);
    
    CCMoveTo *moveTo2 = CCMoveTo::create(4, ccp(size.width/2,480-130));
    
    label3->runAction(moveTo2);
    
    //添加分享按钮
    CCMenuItemImage *pShareItem = CCMenuItemImage::create(
                                                          "button_share-hd.png",
                                                          "button_share-hd.png",
                                                          this,
                                                          menu_selector(AboutGame::BackToRootScene) );
    
    pShareItem->setPosition( ccp(size.width/2 , -285) );
    
    this->addChild(pShareItem);
    
    CCMoveTo *moveTo3 = CCMoveTo::create(4, ccp(size.width/2,480-285));
    
    pShareItem->runAction(moveTo3);
   
    
}


OK 很简单的两个界面,写这个的目的是工作之余写点简单的代码找点


乐趣,同样也希望可以帮助初学者,大牛们误吐槽哦~    有什么错误或


建议可以给我留言哦

~


你可能感兴趣的:(游戏,action,menu,游戏开发,cocos2d-x)