如何在WP8上搭建cocos2d x权威指南-x的游戏开发环境

Cocos2d-x v2.x
MacWindowsLinux
Cocos2d-x v3.x
MacWindows
Quick-Cocos2d-x
MacWindows
Cocos2d-JS
Cocos Studio
MacWindows
Cocos Code IDE粒子系统是指计算机图形学中模拟特定现象的技术,它在模仿自然现象、物理现象及空间扭曲上具备得天独厚的优势,为我们实现一些真实自然而又带有随机性的特效(如爆炸、烟花、水流)提供了方便。Cocos2d-x引擎中就为我们提供了强大的粒子系统,以下是粒子系统的继承关系图:
一个强大的粒子系统它必然具备了多种多样的属性,这样才能配置出多样的粒子。下面就来看看粒子系统的主要属性吧。
主要属性:
发射器生存时间,即它可以发射粒子的时间,注意这个时间和粒子生存时间不同。单位秒,-1表示永远;粒子发射结束后可点击工具栏的播放按钮再次发射
_emissionRate
每秒喷发的粒子数目
_emitterMode
喷发器模式,有重力模式(GRAVITY)和半径模式(RADIUS,也叫放射模式)两种
_totalParticles
场景中存在的最大粒子数目,往往与_emissionRate配合起来使用
_isAutoRemoveOnFinish
粒子结束时是否自动删除
重力模式(modeA):
顾名思义,重力模式模拟重力,可让粒子围绕一个中心点移近或移远,它的优点是非常动态,而且移动有规则。下列各属性只在重力模式下起作用。
radiaAccel
粒子径向加速度,即平行于重力方向的加速度
radiaAccelVar
粒子径向加速度变化范围
速度变化范围
tangentialAccel
粒子切向加速度,即垂直于重力方向的加速度
tangentialAccelVar
粒子切向加速度变化范围
半径模式(modeB):
半径模式可以使粒子以圆圈方式旋转,它也可以创造螺旋效果让粒子急速前进或后退。下列各属性只在半径模式下起作用。
endRadiusVar
结束半径变化范围,即结束半径值的范围在 (endRadius - endRadiusVar) 和 (endRadius + endRadiusVar )之间,下面类似。
rotatePerSecond
粒子每秒围绕起始点的旋转角度
rotatePerSecondVar
粒子每秒围绕起始点的旋转角度变化范围
startRadius
startRadiusVar
初始半径变化范围
生命属性:
粒子生命,即粒子的生存时间
粒子生命变化范围
大小属性:
粒子结束时的大小,-1表示和初始大小一致
_endSizeVar
粒子结束大小的变化范围
_startSize
粒子的初始大小
_startSizeVar
粒子初始大小的变化范围
角度属性:
粒子角度变化范围
颜色属性:
粒子结束颜色
_endColorVar
粒子结束颜色变化范围
_startColor
粒子初始颜色
_startColorVar
粒子初始颜色变化范围
如果你不想编辑出五颜六色的粒子效果,那应该把_endColorVar,_startColorVar尽量设置为(0, 0, 0, 0)。
_positionType
粒子位置类型,有自由模式(FREE)、相对模式(RELATIVE)和打组模式(GROUPED)三种
发射器位置的变化范围(横向和纵向)
_sourcePosition
发射器原始坐标位置
Free自由模式,相对于屏幕自由,不会随粒子节点移动而移动(可产生火焰、蒸汽等效果);Relative相对模式,相对于被绑定的Node静止,粒子发射器会随Node移动而移动,可用于制作移动角色身上的特效等等;Grouped打组模式是相对于发射点的,粒子随发射器移动而移动。
自旋属性:
粒子结束自旋角度
_endSpinVar
粒子结束自旋角度变化范围
_startSpin
粒子开始自旋角度
_startSpinVar
粒子开始自旋角度变化范围
纹理渲染属性:
粒子贴图纹理
_blendFunc
纹理的混合模式方法,有DISABLE、ALPHA_PREMULTIPLIED、ALPHA_NON_PREMULTIPLIED、ADDITIVE四种类型。
DISABLE = {GL_ONE, GL_ZERO};
ALPHA_PREMULTIPLIED = {GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
ALPHA_NON_PREMULTIPLIED = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
ADDITIVE = {GL_SRC_ALPHA, GL_ONE};
表示使用0.0作为因子,实际上相当于不使用这种颜色参与混合运算。
表示使用1.0作为因子,实际上相当于完全的使用了这种颜色参与混合运算。
GL_SRC_ALPHA:表示使用源颜色的alpha值来作为因子。
GL_DST_ALPHA:表示使用目标颜色的alpha值来作为因子。
GL_ONE_MINUS_SRC_ALPHA:表示用1.0减去源颜色的alpha值来作为因子。
GL_ONE_MINUS_DST_ALPHA:表示用1.0减去目标颜色的alpha值来作为因子。
Cocos2dx使用OpenGL混合原理对图形进行渲染绘制。混合就是指把两种颜色混在一起,具体一点就是把某一像素位置原来的颜色和将要画上去的颜色,通过某种方式混在一起,从而实现特殊的效果。它是一种常用的技巧,通常可以用来实现半透明,你也可以通过不同的设置得到不同的混合结果,产生一些有趣或者奇怪的图象。
ParticleBatchNode
ParticleBatchNode 和批处理节点一样,它将通过一次调用OpenGL来绘制它的子节点。
ParticleBatchNode 实例可以引用一个且只能引用一个 texture 的对象。
只有 ParticleSystem 包含 texture 的时候它才可以被添加到SpriteBatchNode中。
所有添加到SpriteBatchNode里面的ParticleSystem都会在同一OpenGL ES调用里面被绘制.
如果 ParticleSystem 没有被添加到ParticleBatchNode中,那么OpenGL ES会调用每个粒子系统的绘图函数,这样做效率会很低。
目前只支持 ParticleSystemQuad
所有的系统都使用相同的参数被绘制:混合函数、纹理等
最为有效的用法
为所有粒子系统初始化ParticleBatchNode纹理和足够的容量
初始化所有粒子系统,并将其添加为批处理节点的子节点
程序中的应用
创建一个粒子系统,同时初始化它的粒子总数:
auto _emitter = ParticleSystemQuad::createWithTotalParticles(50);
ParticleSystemQuad是粒子系统的一种,早前的Cocos2d-x版本中,粒子系统是有两种类型的,既:ParticleSystemQuad 和 ParticleSystemPoint(现已被移除)。
auto _emitter = ParticleSystemQuad::createWithTotalParticles(50);
addChild(_emitter, 10);
_emitter-&setTexture(Director::getInstance()-&getTextureCache()-&addImage("CloseNormal.png"));
_emitter-&setAnchorPoint(Point(0, 0));
// duration
_emitter-&setDuration(ParticleSystem::DURATION_INFINITY);
// radius mode
_emitter-&setEmitterMode(ParticleSystem::Mode::RADIUS);
// radius mode: start and end radius in pixels
_emitter-&setStartRadius(4);
_emitter-&setStartRadiusVar(1);
_emitter-&setEndRadius(ParticleSystem::START_RADIUS_EQUAL_TO_END_RADIUS);
_emitter-&setEndRadiusVar(0);
// radius mode: degrees per second
_emitter-&setRotatePerSecond(100);
_emitter-&setRotatePerSecondVar(0);
_emitter-&setAngle(90);
_emitter-&setAngleVar(0);
// emitter position
auto size = Director::getInstance()-&getWinSize();
_emitter-&setPosVar(Point::ZERO);
// life of particles
_emitter-&setLife(0.5);
_emitter-&setLifeVar(0);
// spin of particles
_emitter-&setStartSpin(0);
_emitter-&setStartSpinVar(0);
_emitter-&setEndSpin(0);
_emitter-&setEndSpinVar(0);
// color of particles
Color4F startColor(0.0f, 0.8f, 0.9f, 1.0f);
_emitter-&setStartColor(startColor);
Color4F startColorVar(0, 0, 0, 1.0f);
_emitter-&setStartColorVar(startColorVar);
Color4F endColor(1.0f, 1.0f, 1.0f, 0.1f);
_emitter-&setEndColor(endColor);
Color4F endColorVar(0, 0, 0, 0.1f);
_emitter-&setEndColorVar(endColorVar);
// size, in pixels
_emitter-&setStartSize(20);
_emitter-&setStartSizeVar(1);
_emitter-&setEndSize(0);
// emits per second
_emitter-&setEmissionRate(_emitter-&getTotalParticles() / _emitter-&getLife());
// additive
_emitter-&setBlendAdditive(false);
//addChild(_emitter);
_emitter-&setPosition(Point(200,200));Cocos Studio
&2.1 第一步:新建游戏项目
2.1 第一步:新建游戏项目
项目是一个将包含场景、图层、节点、合图、资源等文件组织起来的工程。Cocos Studio中可以创建包含源代码的项目也可以创建不包含源代码的项目。
启动Cocos Studio,来到登陆页,选择新建游戏项目。我们新建一个Cocos Studio官方示例项目,项目名称为“CocosStudioDemo”
在创建项目时只要设置好项目名称和路径,点击“新建”按钮,即可新建一个Cocos Studio游戏项目。在这里您可以制作游戏中需要使用的各种资源,从启动界面到注册登录,从游戏主场景到战斗场景,从装备背包到人物动画均可在Cocos Studio中轻松实现,并最终输出游戏资源给编程人员。
如果您是一位了解编程的Cocos用户,在使用Cocos Studio时,我们为您提供了直接生成Cocos2d-x源代码项目的方式
其中cocosstudio文件夹放置您项目中使用的所有资源
XXX.ccs文件为项目的启动文件,双击该文件可以开启您的项目
XXX.cfg文件为项目配置文件,用于管理您的项目配置信息
新建包含源代码项目
在新建项目界面,勾选“生成Cococs2d-x项目”,在设置好相关的参数后,即可创建一个既包含游戏界面,也包含游戏源代码的工程,当然在创建这些工程后还是需要相应的工具来进行源代码的编写,Cocos Studio将使界面编辑到代码编辑的流程更加通顺流畅。
代码项目设置包含以下内容:
包名:应用程序在设备(IOS、Android)中的唯一ID标识。
引擎路径:您本机的Cocos2d-x路径。这个属性很重要,一定要设置正确的引擎路径。目前Cocos Studio支持2.x-3.x版本的Cocos2d-x引擎,获得最新版引擎,请到Cocos2-x官方网站获取。
项目语言:C++、Lua、JavaScript
运行环境:您可以选择是使用Cocos2d-x的运行时运行程序还是通过源代码编译程序。
附件功能:附加功能是Intel 公司提供的针对C++的特殊优化功能。您可以根据项目的要求,选择是否增加这些附加功能。
Intel@Common Connectivity Framework是由Intel公司提供的一套通用连接框架,详细内容参见:。
X86优化是由Intel公司提供的一套适用于X86平台的框架。GCC(GNU Compiler Collection,GNU编译器套装)是一套由GNU工程开发的支持多种编程语言的编译器。ICC(Intel C++ Compiler)是 Intel 公司开发的 C/C++ 编译器。关于ICC详见
其中cocosstudio文件夹放置您项目中使用的所有资源
XXX.ccs文件为项目的启动文件,双击该文件可以开启您的项目
XXX.cfg文件为项目配置文件,用于管理您的项目配置信息
其他文件为Cocos2d-x代码项目文件,如果您想使用编写代码的IDE启动您的项目,您可以访问projects文件夹下的文件找到对应IDE的启动项来启动您的项目。
按照文档无数创建项目成功, 引擎路径:您本机的Cocos2d-x路径wiki中文文档
&Windows Phone 8上集成第三方库SDK
Windows Phone 8上集成第三方库SDK
Cocos2d-x wp8平台上支持xaml后,方便了第三方Sdk的调用。本文以微软的广告这个第三方sdk为例,介绍如何在XAML工程中集成该sdk。
AdControl的官方文档
微软官方文档:,不过这种方式是在xaml页面上直接添加控件,本文介绍如何通过c++调用在c#页面里面添加控件,及对c#控件事件的回调在c++里面进行响应处理,即c++和c#的互相调用。代码均在gitbub上个人仓库adcontrol分支中,链接:,详细集成教程见下文。
下载Microsoft Advertising SDK for Windows Phone
下载链接:,下载完成后手动安装.msi文件。打开HelloCpp工程,右键选择"Add Reference",在Reference Mangager中通过"Extensions"页面添加。如下图:
配置WMAppManifest.xml
在Solution Explorer=〉Properties 里面,打开WMAppManifest.xml
选择Capabilities ,勾选 ID_CAP_IDENTITY_USER、 ID_CAP_MEDIALIB_PHOTO、ID_CAP_PHONEDIALER
准备AdControl调用接口
在HelloCppComponent中添加ICallback.h文件,并在里面添加ICallback interface。添加回掉事件处理函数和调用接口。代码如下:
namespace PhoneDirect3DXamlAppComponent
namespace AdControlHelper
// An asynchronous completed event argument
public ref class CompletedEventArgs sealed
CompletedEventArgs(bool ReturnValue, int ErrorCode, Platform::String^ ErrorMessage)
this-&ReturnValue = ReturnV
this-&ErrorCode = ErrorC
this-&ErrorMessage = ErrorM
property bool ReturnV
property int ErrorC
property Platform::String^ ErrorM
// A callback interface for C# code to implement.
public interface class ICallback
// Submit a score completed event.
event Windows::Foundation::EventHandler&CompletedEventArgs^&^ OnBannerR
event Windows::Foundation::EventHandler&CompletedEventArgs^&^ OnBannerReceivedF
// switch bottombar in mainpage to add or remove panel
void SwitchBottomBar();
//... other event and other function
CompletedEventArgs是对响应信息的封装,包含响应代码和出错信息。SwitchBottomBar()是c++调用c#的函数。OnBannerRefreshed和OnBannerReceivedFailed是回调处理函数,在C#代码中调用,响应了该函数即完成了回调。
## c#中实现ICallback接口
在HelloCpp工程中添加AdControlCallback.cs代码文件,定义类AdControlCallback继承实现ICallback接口。如下:
namespace PhoneDirect3DXamlAppComponent
/// &summary&
/// A callback class that implements the WinRT Component interface ICallback.
/// All methods provided will be called throught the ICallback interface in native code.
/// &/summary&
public class AdControlCallback : ICallback
public event EventHandler&CompletedEventArgs& OnBannerR
public event EventHandler&CompletedEventArgs& OnBannerReceivedF
public void SwitchBottomBar()
m_MainPage.SwitchBottomBar();
if (bAdCreated == false)
CreateBannerAd();
bAdCreated =
//Create the Ad at runtime and add to the container
private void CreateBannerAd()
Deployment.Current.Dispatcher.BeginInvoke(() =&
// ApplicationID = "test_client", AdUnitID = "Image480_80",
AdControl adControl = new AdControl("test_client",
// ApplicationID
"Image480_80",
// AdUnitID
// isAutoRefreshEnabled
// Make the AdControl size large enough that it can contain the image
adControl.Width = 480;
adControl.Height = 80;
adControl.AdRefreshed += new EventHandler(adControl_AdRefreshed);
adControl.ErrorOccurred += new EventHandler&Microsoft.Advertising.AdErrorEventArgs&(AdView_AdRequestFailed);
m_MainPage.AddBannerAd(adControl);
private void adControl_AdRefreshed(object sender, EventArgs e)
if (OnBannerRefreshed != null && m_d3dInterop != null)
m_d3dInterop.OnAdControlEvent(sender, new CompletedEventArgs(true, 200, "Received ad successfully"), OnBannerRefreshed);
注意以下几点:
1.AdControl为Microsoft.Advertising.Mobile.UI中的类,需要添加命名空间:using Microsoft.Advertising.Mobile.UI;。
2.AdControlCallback必须继承ICallback的成员函数和成员变量,在SwitchBottomBar()的实现中可以添加C#的代码实现,这里调用了私有方法CreateBannerAd()来处理AdControl控件。
3.处理C#控件可以使用Deployment.Current.Dispatcher.BeginInvoke,让c#控件跟C++游戏界面处于不同线程中,这样调用第三方sdk时,即使处理时间长,也不会影响游戏界面造成卡顿的现象,并且如果跟MainPage.xaml有调用关系的话也必须使处理逻辑位于主线程中。其实通过调试可以发现,MainPage.xaml.cs跟native c++是出于不同的线程中的,MainPage是在主线程即MainThread中执行,而c++的游戏界面是在worker thread中执行的。
4.adControl_AdRefreshed为AdControl的响应函数,通过响应该函数可以判断是否成功添加AdControl控件
5.adControl_AdRefreshed的响应处理,不能直接调用c++的代码,否则会产生不可预期的错误,因为当前响应函数位于主线程中(第3点已分析)。需要注意线程安全。后面我们将会介绍怎么正确处理响应函数。
修改MainPage.xaml
当c++调用SwitchBottomBar()时,我们可以调整MainPage.xaml页面,使上面显示游戏界面,下面显示一个广告窗口。调整界面如下:
直接打开MainPage.xaml页面文件,修改ContentPanel内代码,添加StackPanel,这里先将stackContainer的Visibility属性设置为Collapsed不可见,后面将在stackContainer添加AdControl控件
&!--ContentPanel - place additional content here--&
&Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&
&DrawingSurface x:Name="DrawingSurface" Loaded="DrawingSurface_Loaded" Margin="0,0,0,0"
&!-- Container where the adcontrol will be added at run time--&
&StackPanel x:Name="stackContainer" Height="0" VerticalAlignment="Bottom" Visibility="Collapsed"&
&/StackPanel&
在MainPage.xaml.cs里面添加SwitchBottomBar函数,调用时,将显示stackContainer,并且修改DrawingSurface大小。添加AddBannerAd函数,添加AdControl控件到stackContainer显示
// add to the container
public void AddBannerAd(AdControl adView)
Dispatcher.BeginInvoke(() =&
this.stackContainer.Children.Add(adView);
public void SwitchBottomBar()
Dispatcher.BeginInvoke(() =&
bool bIsVisible = Visibility.Visible == this.stackContainer.V
this.stackContainer.Visibility = (bIsVisible ? Visibility.Collapsed : Visibility.Visible);
// reMargin DrawingSurface
Thickness srcMargin = this.DrawingSurface.M
srcMargin.Bottom = bIsVisible ? 0 : 100;
this.DrawingSurface.Margin = srcM
// reHeight stackcontainer
this.stackContainer.Height = bIsVisible ? 0 : 100;
注意MainPage.xaml.cs的SwitchBottomBar()在AdControlCallback的SwitchBottomBar()调用,在AdControlCallback中保存MainPage的类实例即可,同时MainPage.xaml.cs的SwitchBottomBar()添加public属性。
在WinRT组件中实现调用代理
在HelloCppComponent中创建AdControlDelegate托管类,方便C++和C#中调用。
namespace PhoneDirect3DXamlAppComponent
namespace AdControlHelper
public ref class AdControlDelegate sealed
AdControlDelegate(void);
void SetCallback(ICallback^ callback);
property static ICallback^ GlobalC
这里以static保存实现了**ICallback的类实例**,SetCallback函数在c#中调用。SetCallback函数中GlobalCallback保存实参callback。 如果在C++中多次使用到**AdControlDelegate**类,可以使用单例模式来创建AdControlDelegate。SetCallback函数调用可以在HelloCpp工程里的MainPage的DrawingSurface_Loaded中实现,如下:
AdControlDelegate adDelegate = new AdControlDelegate();
AdControlCallback adObj = new AdControlCallback();
adDelegate.SetCallback(adObj);
游戏逻辑中调用并处理回掉函数
在menuCallbackBottom函数中测试调用sdk,并实现c#调用的响应处理事件OnBannerReceivedFailed。
AdControlDelegate^ AdControlObj = ref new AdControlDelegate();
AdControlObj-&GlobalCallback-&OnBannerReceivedFailed += ref new Windows::Foundation::EventHandler&CompletedEventArgs^&(
[this](http://cn.cocos2d-x.org/doc/cocos-docs-master/manual/framework/native/wiki/how-to-integrate-third-sdk/Platform::Object^ sender, CompletedEventArgs^ args){
Platform::String ^platform_string = args-&ErrorM
const wchar_t* wide_chars = platform_string-&Data();
char chars[512];
wcstombs(chars, wide_chars, 512);
pResponseLabel-&setString(chars);
AdControlObj-&GlobalCallback-&SwitchBottomBar();
在该函数中实现了OnBannerReceivedFailed响应函数,并且调用SwitchBottomBar来调用C# sdk。使用GlobalCallback来调用在c#中创建的实例。
## 不同线程处理
可以使用主线程(即xaml页面所处线程)来调用Sdk,也可以创建一个任务来处理。如上面介绍过的Deployment.Current.Dispatcher.BeginInvoke调用,
Deployment.Current.Dispatcher.BeginInvoke可以获取主线程,并在里面实现调用sdk。也可以使用任务来调用,如下:
//Create the Ad at runtime and add to the container
public void CreateBannerAd()
//common worker thread
System.Threading.Tasks.Task.Run(async () =&
// ApplicationID = "test_client", AdUnitID = "Image480_80",
AdControl adControl = new AdControl("test_client",
// ApplicationID
"Image480_80",
// AdUnitID
// isAutoRefreshEnabled
// Make the AdControl size large enough that it can contain the image
adControl.Width = 480;
adControl.Height = 80;
adControl.AdRefreshed += new EventHandler(adControl_AdRefreshed);
adControl.ErrorOccurred += new EventHandler&Microsoft.Advertising.AdErrorEventArgs&(AdView_AdRequestFailed);
m_MainPage.AddBannerAd(adControl);
最好回调时候能够切回游戏逻辑所处的线程,所以下一步进行回调处理。
在Direct3DInterop实现文件中,可以发现,所有的点击、按键处理事件通过mInputEvents的事件队列来处理,只要向该线程投递该事件,然后由ProcessEvents函数来处理该事件。这样,我们定义一个AdControlEvent类继承InputEvent,如下:
namespace PhoneDirect3DXamlAppComponent
class AdControlEvent : public InputEvent
AdControlEvent(Object^ sender, CompletedEventArgs^ args, Windows::Foundation::EventHandler&CompletedEventArgs^&^ handler);
virtual void execute(Cocos2dRenderer ^ renderer);
Object^ m_
CompletedEventArgs^ m_
Windows::Foundation::EventHandler&CompletedEventArgs^&^ m_
在AdControlEventt构造函数中,最后一个参数是EventHandler类型参数,避免了通过Cocos2dRenderer层层转发。因为在handler中已实现了回调函数,在execute实现中直接Invoke调用即可。如下:
void AdControlEvent::execute( Cocos2dRenderer ^ renderer )
m_handler-&Invoke(m_sender, m_args);
在Direct3DInterop类中,添加OnAdControlEvent响应函数。如下:
void Direct3DInterop::OnAdControlEvent(Object^ sender, CompletedEventArgs^ args, Windows::Foundation::EventHandler^ handler)
std::lock_guard&std::mutex& guard(mMutex);
std::shared_ptr&AdControlEvent& e(new AdControlEvent(sender, args, handler));
mInputEvents.push(e);
只要handler事件处理函数有不同的实现,就可以调用OnAdControlEvent这个函数,比如可以再实现一个OnBannerReceivedFailed来调用。相应的,修改直接回调函数AdView_AdRequestFailed的代码:
private void AdView_AdRequestFailed(object sender, Microsoft.Advertising.AdErrorEventArgs e)
//if (OnBannerReceived != null)
OnBannerReceived(sender, new CompletedEventArgs(true, 200, "Received ad successfully"));
if (OnBannerReceivedFailed != null && m_d3dInterop != null)
m_d3dInterop.OnAdControlEvent(sender, new CompletedEventArgs(true, (int)e.ErrorCode, "Received ad failed" + e.Error.Message), OnBannerReceivedFailed);
在AdControlCallback中设置并保存Direct3DInterop实例的函数:
public void SetDirect3DInterop(Direct3DInterop d3dInterop)
m_d3dInterop = d3dI
以上代码整个调用过程就完成了,假如需要在通过MainPage调用其它xaml页面,可以用SetMainPage在AdControlCallback中保存该MainPage类实例,如:
public void SetMainPage(MainPage mainPage)
m_MainPage = mainP
private void CallToShowPage()
// get the UI thread (ie. the Main Thread) to let the worker thread (ie. opengl thread) work normally.
Deployment.Current.Dispatcher.BeginInvoke(() =&
m_MainPage.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
如果有点击按钮,弹出整个全屏页面的情况,便可以使用上面的CallToShowPage方法。wiki中文文档
&如何使用 CCLOG
如何使用 CCLOG
CCLOG可以选择性地包含由在随后的附加参数指定的值替换,格式为请嵌入格式说明符。
注意:在OS X
CCLOG没有NSLOG强大。如果你不知道如何格式化输出,你不能使用CCLog。
#define COCOS2D_DEBUG 1
Visual Studio
适合那些想在Visual Studio中检查预处理宏,右键单击project manager-& click properties-& select configuration properties-& C/C++ -& Preprocessor。你会在右边的预处理器定义。
Parameters(参数)
凡在最后的说明符字符是最重要的组成部分,因为它定义的类型及其相应参数的解释:
CCLOG("Characters: %c %c", 'a', 65);
CCLOG("Decimals: %d %ld", L);
CCLOG("Preceding with blanks: %10d", 1977);
CCLOG("Preceding with zeros: %010d", 1977);
CCLOG("Some different radixes: %d %x %o %#x %#o", 100, 100, 100, 100, 100);
CCLOG("Floats: %4.2f %.0e %E", 3.6, 3.1416);
CCLOG("%s","A string");
CCLOG未绑定到Lua中,你可以使用print来代替或定义一个新的函数来实现相同的效用。
cclog = function(...)
print(string.format(...))
cclog("float and int and string: %4.2f, %#x, %s", 3.14, "Cocos2d-x");
print("you can use either print or cclog as you want");
print("another float is " .. 3.14 .. " another int is " .. 1024);
cocos2d: Characters: a A
cocos2d: Decimals:
cocos2d: Preceding with blanks:
cocos2d: Preceding with zeros:
cocos2d: Some different radixes: 100 64 144 0x64 0144
cocos2d: Floats: 3.14 3e+00 3.
cocos2d: A string
cocos2d: [LUA-print] float and int and string: 3.14, 0x7de, Cocos2d-x
cocos2d: [LUA-print] you can use either print or cclog as you want
cocos2d: [LUA-print] another float is 3.14 another int is 1024

我要回帖

更多关于 cocos2d x入门 的文章

 

随机推荐