参考blog : http://blog.csdn.net/column/details/jackyairplane.html
环境 xp + cocos2d-x-2.2.0 + vs2010
源码下载地址:点击打开链接
关于svn的简单使用:点击打开链接
cocos2d应用的大致启动流程
main函数最后一句:CCApplication::sharedApplication()->run()------>>pDirector->runWithScene(pScene);(AppDelegate.cpp)
就启动了应用程序的场景。
接着就是各个精灵在场景中表演动作以及与用户的互动表演,为了方便管理控制对精灵的处理往往采用分层来分别管理。
例如在本游戏中就有背景层(GameLayer)主角飞机层(PlaneLayer)子弹层(BulletLayer)等。
1.新建项目
在cocos2d源码中有个目录\tools\project-creator\
我在这里加了个project-creator.bat文件
里面内容如下:
@echo off
set /p projectName=请输入项目名称:
if "%projectName%"=="" goto inputError
set /p packageName=请输入包标示名:
if "%packageName%"=="" goto inputError
create_project.py -project %projectName% -package %packageName% -language cpp
pause
exit
:inputError
@echo 项目名称或者包标示名不能为空!
pause
回车--回车
在cocos2d源码目录projects中就建好了项目AirPlane12
用vs2010打开编译运行
右上角怎么是HelloCpp?我要改为AirPlane。
将main.cpp下的改为eglView->setViewName("AirPlane");
编译运行一下,怎样是不是变了。
顺便提示哈,如果要改分辨率就改其下一句
eglView->setFrameSize(480, 320);
将里面的值改为你所需要的。
2.增加此游戏场景(GameScene)
// GameScene.h
#ifndef __GAMESCENE_H__
#define __GAMESCENE_H__
#include "cocos2d.h"
#include "GameLayer.h"
USING_NS_CC;
class GameScene : public CCScene
{
public:
GameScene(void);
~GameScene(void);
CREATE_FUNC(GameScene);
virtual bool init();
GameLayer *_gameLayer;
};
#endif
// GameScene.cpp
#include "GameScene.h"
GameScene::GameScene(void)
{
_gameLayer=NULL;
}
GameScene::~GameScene(void)
{
}
bool GameScene::init()
{
bool bRet=false;
do
{
CC_BREAK_IF(!CCScene::init());
_gameLayer=GameLayer::create();
CC_BREAK_IF(!_gameLayer);
this->addChild(_gameLayer);
bRet=true;
}while(0);
return bRet;
}
// GameLayer.h
#ifndef __GAMELAYER_H__
#define __GAMELAYER_H__
#include "cocos2d.h"
USING_NS_CC;
class GameLayer :public CCLayer
{
public:
GameLayer(void);
~GameLayer(void);
CREATE_FUNC(GameLayer);
virtual bool init();
void backgroundMove(float dt);
private:
CCSprite* background1;
CCSprite* background2;
};
#endif
// GameLayer.cpp
#include "GameLayer.h"
GameLayer::GameLayer(void)
{
background1=NULL;
background2=NULL;
}
GameLayer::~GameLayer(void)
{
}
bool GameLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/shoot_background.plist");
background1=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png"));
background1->setAnchorPoint(ccp(0,0));
background1->setPosition(ccp(0,0));
this->addChild(background1);
background2=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png"));
background2->setAnchorPoint(ccp(0,0));
background2->setPosition(ccp(0,background2->getContentSize().height-2));
this->addChild(background2);
this->schedule(schedule_selector(GameLayer::backgroundMove),0.01f);
bRet=true;
}while(0);
return bRet;
}
void GameLayer::backgroundMove(float dt)
{
background1->setPositionY(background1->getPositionY()-2);
background2->setPositionY(background1->getPositionY()+background1->getContentSize().height-2);
if (background2->getPositionY()==0)
background1->setPositionY(0);
}
增加头文件 #include "GameScene.h"
将 CCScene *pScene = HelloWorld::scene(); 改为 CCScene *pScene = GameScene::create();
最后编译运行得到如下截图,背景动起来了。