大家看到了cocos2d-x引擎提供的5种可选择的模板。
cocos2dx:标准模板(无物理类库,无脚本类库)
cocos2dx_box2d(box2d物理游戏模板)
cocos2dx_chipmunk(chipmunk物理游戏模板)
cocos2dx_js(含js脚本类库)
cocos2dx_lua(含lua脚本类库)
我们选择cocos2dx标准模板即可。
如下图输入product name和company identifier
点击NEXT,选好目录地址后即可完成创建,新项目如下图。
首先我们来看一下cocos2dx的项目结构。
大家看上图的左边,主要有以下几个目录:
Resources: 资源存放目录
ios:项目运行的入住口(main函数 :))
libs:cocos2dx引擎的库以及其他扩展的一些类库
classes:cocos2dx引擎入口
support files:其他文件
framework:ios 的sdk
products:产品存放的目录
我们常用的入口就是classes目录了,下面我们来看下AppDelegate.cpp的代码,
我在重要的地方都加了注释
在看代码之前,我稍微介绍几个类的概念。
CCDirector:顾名思义,导演就是控制不同场景之间的切换
CCScene:一个游戏由游戏场景,暂停菜单场景,主菜单场景等多个场景组成。
CCLayer:每个场景中由多个层来组成,比如游戏中有远景地图,精灵层,前景装饰,HUD界面等层组成。
CCSprite:如雷电游戏由一个飞机精灵,多个敌人精灵等较色,并且有不同的状态。
//
// Hello_worldAppDelegate.cpp
// Hello_world
//
// Created by wenbin.cai on 13-5-30.
// Copyright __MyCompanyName__ 2013年. All rights reserved.
//
#include "AppDelegate.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "HelloWorldScene.h"
USING_NS_CC;
using namespace CocosDenshion;
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
}
bool AppDelegate::applicationDidFinishLaunching() //三个生命周期函数之一:游戏启动时执行
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector(); //获得唯一的一个导演对象
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// turn on display FPS
pDirector->setDisplayStats(true); //设置显示状态,看运行图中的左下角(对象数,time per second,FPS帧数)
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);//设置游戏帧数FPS
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();//创建一个场景
// run
pDirector->runWithScene(pScene);//导演展现一个场景
return true;
}
// 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(); //整个游戏停止动画
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //停止播放背景音乐
SimpleAudioEngine::sharedEngine()->pauseAllEffects();//展厅所有特效
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()//三个生命周期函数之三,程序从后台切回到前台时执行。
{
CCDirector::sharedDirector()->startAnimation();//开始所有动画
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); //继续播放背景音乐
SimpleAudioEngine::sharedEngine()->resumeAllEffects();//继续所有特效
}
看完代码,让我们运行下我们的第一个cocos2dx项目把~注意左下角3个数字
大概了解了项目的结构,我们可以正式开始学习cocos2dx了~
大家一起加油~~
大小: 234.4 KB
大小: 65 KB
大小: 78.4 KB
大小: 55.4 KB
分享到:
2013-05-30 22:44
浏览 1797
评论