Allen 学习 cocos2dx (一) 创建游戏项目 Hello World

开发环境 xcode5

开发引擎 cocos2dx  版本 2.2.1


基础概念介绍——导演、场景、层、精

http://www.tuicool.com/articles/ZRRNza

大体对游戏开发思想的了解。

一创建 myGame

终端cd到cocos2d-x-2.2.1/tools/project-creator

./create_project.py -project myGame -package com.lengshengren.Game -language cpp

使用脚本./create_project.py会提示命令格式


Usage: create_project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE

在projects 文件夹下找到自己helloword 的工程


在 AppDelegate.cpp 里  我们会看见 #include "HelloWorldScene.h"  头文件


二 编写自己的类

创建一个

GameStart.cpp 的c++ 文件 


在GameStart.h 中创建自己的类


#include <cocos2d.h>

 class MyGameStart:public cocos2d::CCLayer

{

   public:   

    //虚函数 创建个初始化函数

    virtual bool init();

    // 创建一个 scene 函数 (场景)

    static cocos2d::CCScene *scene();

     CREATE_FUNC(MyGameStart);




通过观察这下面个宏 我们发现这个宏给这个类创建了对象

//#define CREATE_FUNC(__TYPE__) \

//static __TYPE__* create() \

//{ \

//__TYPE__ *pRet = new __TYPE__(); \

//if (pRet && pRet->init()) \

//{ \

//pRet->autorelease(); \

//return pRet; \

//} \

//else \

//{ \

//delete pRet; \

//pRet = NULL; \

//return NULL; \

//} \

//}

//    


};

#endif /* defined(__myGame__GameStart__) */



在 .cpo 文件里实现类的方法


// 头文件里场景方法的实现

CCScene* MyGameStart::scene()

{

    // 'scene' is an autorelease object

   // 创建个场景对象

    CCScene *scene =CCScene::create();

    // '创建当前类的布景

    MyGameStart *layer =MyGameStart::create();

   // add layer as a child to scene

      将布景层加到场景层

     scene->addChild(layer);

     // return the scene

     return scene;

}

// 进行初始化 在 布景层 添加想要的精灵 等

boolMyGameStart::init()

{

    

    // 1. super init first

    if ( !CCLayer::init() )

    {

        returnfalse;

    }

     

  // 设置背景图

    CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize();

    CCSize size =CCDirector::sharedDirector()->getWinSize();

    CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin();


  // 创建一个精灵 CCSprite

    CCSprite* pSprite =CCSprite::create("test.jpg");

    pSprite->setScaleX(size.width/pSprite->getContentSize().width);   // setScaleX  x 轴缩放系数      getContentSize  获得节点的位置

    pSprite->setScaleY(size.height/pSprite->getContentSize().height);    //setScaleY  y 轴缩放系数

    pSprite->setAnchorPoint(ccp(0.0,0.0));

    this->addChild(pSprite,0);

    CCLabelTTF *pLabel =CCLabelTTF::create("老湿的游戏","Arial", 25); //创建一个标签

    pLabel->setColor(ccc3(255,255, 0));  //字体颜色

    pLabel->setPosition(ccp(origin.x+visibleSize.width/2,origin.y + visibleSize.height -pLabel->getContentSize().height)); // 标签的坐标 居中

    // position the label on the center of the screen

    // pLabel->setPosition(ccp(origin.x + visibleSize.width/2,

    //                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    

    //#define ccp(__X__,__Y__) cocos2d::CCPointMake((float)(__X__), (float)(__Y__))


  this->addChild(pLabel,1);

  return true;


};


三 导演 控制场景

注释掉HelloWorldScene.h 头文件 引入  #include"GameStart.h" 头文件


就想基础中说到一个电影要有个导演 ,导演是核心当然放在工程AppDelegate 这个核心类里

 

AppDelegate::AppDelegate() {


}


AppDelegate::~AppDelegate() 

{

}


//这段代码出自 AppDelegate.cpp文件中的 applicationDidFinishLaunching 函数,首先获得导演类指针,然后设置 OpenGL 视图,设置是否显示每帧时间,设置每帧时间,然后创建并运行场景。这样初始化工作就完成了。

bool AppDelegate::applicationDidFinishLaunching() {

    // initialize director

    

    // 创建导演对象

    CCDirector* pDirector =CCDirector::sharedDirector();

    //  设置openGl 试图

    CCEGLView* pEGLView =CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);

    // turn on display FPS

    //是否显示每一帧时间

    pDirector->setDisplayStats(false);


    // set FPS. the default value is 1.0/60 if you don't call this

    //设置每一帧的时间

    pDirector->setAnimationInterval(1.0 /60);


    // create a scene. it's an autorelease object

    // 创建一个 场景

    CCScene *pScene =MyGameStart::scene();



    // run

    // 运行场景

    pDirector->runWithScene(pScene);


    returntrue;

}




//在游戏进入后台或者从后台返回时,分别调用相应的方法停止动画和开始动画

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too

void AppDelegate::applicationDidEnterBackground() {

    CCDirector::sharedDirector()->stopAnimation();


    // if you use SimpleAudioEngine, it must be pause

    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

}

// 当游戏再次启动时执行

// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground() {

    CCDirector::sharedDirector()->startAnimation();


    // if you use SimpleAudioEngine, it must resume here

    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

}


执行工程我们会看见一个全屏的图片 和一个标签 。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值