本系列博客使用的图片仅仅是学习使用
现在开始代码:
1. 创建一个项目,使用的cocos2dx 2.2版本。
首先在main中更改显示窗口为竖屏的比例:
eglView->setFrameSize(320, 640);
2. 在框架入口函数:applicationDidFinishLaunching中 添加代码, 配置分辨率, 使游戏能够使用多种分辨率。
项目使用的背景图片是768x2560, 定义BASIC_WIDTH 为 768, 然后计算当前屏幕的高度就可以了
//适配分辨率
pDirector->setOpenGLView(pEGLView);
CCSize sizeView = pEGLView->getFrameSize();
if(sizeView.width == 0)
{
CCMessageBox("can't open the device! ","");
pDirector->end();
}
float newHight = BASIC_WIDTH * sizeView.height /sizeView.width;
pEGLView->setDesignResolutionSize(BASIC_WIDTH,newHight,kResolutionExactFit);
运行一下,图片ok!
我使用的是vs2012,可以在项目中增加过滤器,添加开始场景和公共
#ifndef __MT_PUBLIC_
#define __MT_PUBLIC_
#include "cocos2d.h"
USING_NS_CC;
#define SCREEN_WIDTH CCDirector::sharedDirector()->getWinSize().width
#define SCREEN_HIGHT CCDirector::sharedDirector()->getWinSize().height
#endif // __MT_PUBLIC_
在开始场景中,搭建好框架:
#include "MTPublic.h"
#include "MTBeginScene.h"
USING_NS_CC;
CCScene* MTBiginSceneLayer::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
MTBiginSceneLayer *layer = MTBiginSceneLayer::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 MTBiginSceneLayer::init()
{
bool Ret = false;
do
{
//1. super init first
CC_BREAK_IF(!CCLayer::init());
//开启安卓设备支持
setKeypadEnabled(true);
//创建背景图片//
CCSprite *pBackGround=CCSprite::create("background.png");
CC_BREAK_IF(pBackGround==NULL);
pBackGround->setPosition(ccp(SCREEN_WIDTH/2,SCREEN_HIGHT/2));
addChild(pBackGround,enZOrderBackGround);
//添加logo图片
CCSprite* pLogo=CCSprite::create("loadgo.png");
CC_BREAK_IF(pLogo==NULL);
pLogo->setPosition(ccp(SCREEN_WIDTH/2,SCREEN_HIGHT/2));
this->addChild(pLogo,enZOrderLogo);
//play按钮//
CCMenuItemImage *pPlayItem=CCMenuItemImage::create(
"btn-play.png",
"btn-play-down.png",
this,
menu_selector(MTBiginSceneLayer::menuPlayCallback));
CC_BREAK_IF(!pPlayItem);
pPlayItem->setPosition(ccp(SCREEN_WIDTH/2,SCREEN_HIGHT/2-40.f));
//about按钮//
CCMenuItemImage *pAboutItem=CCMenuItemImage::create(
"btn-about.png",
"btn-about-down.png",
this,
menu_selector(MTBiginSceneLayer::menuAboutCallback));
CC_BREAK_IF(!pAboutItem);
pAboutItem->setPosition(ccp(SCREEN_WIDTH/2,SCREEN_HIGHT/2-150.f));
//退出按钮
CCMenuItemImage *pExitItem=CCMenuItemImage::create(
"exit_game_n.png",
"exit_game_d.png",
this,
menu_selector(MTBiginSceneLayer::menuExitCallback));
CC_BREAK_IF(!pExitItem);
pExitItem->setPosition(ccp(SCREEN_WIDTH/2,SCREEN_HIGHT/2-260.f));
//将menu添加到场景
CCMenu *pMenu=CCMenu::create(pPlayItem,pAboutItem,pExitItem,NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(!pMenu);
this->addChild(pMenu,enZOrderBtn);
Ret = true;
} while (0);
return Ret;
}
//点击按钮跳转到游戏场景
void MTBiginSceneLayer::menuPlayCallback(CCObject* pSender)
{
}
//跳转到关于场景
void MTBiginSceneLayer::menuAboutCallback(CCObject* pSender)
{
}
//是否退出游戏
void MTBiginSceneLayer::menuExitCallback(CCObject* pSender)
{
}
运行结果就出来了,显示这个界面有5个节点。(就是我们添加了5个sprite)。
源码: http://download.csdn.net/detail/u012735451/6931173