Cocos2dx 学习笔记(1) Hello Lua!

从今天开始关注Torque2D的同时学习一下Cocos2dx,在博客做个记录,大家共同提高 :)

前期准备

  1: VS2010环境并有cocos2dx的项目创建向导

  2: 最新版本的引擎

  3: 创建使用Box2D和Lua的新项目

代码分析

  为了简洁明了,后面我的学习方式是直接阅读,跟踪代码,查资料只在大方向上有用,细节还是要跟踪调试,这才是开源的魅力!  

// main.cpp

#include "main.h"
#include "AppDelegate.h"
#include "CCEGLView.h"

// 为C++的名称空间宏,using namespace cocos2d的缩写
USING_NS_CC;

// 这里是控制台开关宏,是否打开控制台
#define USE_WIN32_CONSOLE

int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

#ifdef USE_WIN32_CONSOLE
	
	// 创建一个控制台
    AllocConsole();

	// 将标准输入输出流定位到这个控制台
	// "CONOUT$""CONIN$"是对当前控制台的输入输出示意字符串
    freopen("CONIN$", "r", stdin);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);

#endif

	// 创建一个Cocos2dx应用程序的代理实例
    AppDelegate app;

	// 获取主窗体,使用OpenGL渲染
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();

	// 设置窗体尺寸
    eglView->setFrameSize(480, 320);

	// 进入循环,游戏结束时返回结果
    int ret = CCApplication::sharedApplication()->run();

#ifdef USE_WIN32_CONSOLE

	// 删除控制台
    FreeConsole();

#endif

    return ret;
}
// AppDelegate

#ifndef __APP_DELEGATE_H__
#define __APP_DELEGATE_H__

#include "cocos2d.h"

// cocos2d应用程序代理类
// 私有继承,在没必要重载基类方法的时候,关闭权限
// 代理类负责两个任务:
//	1: 创建CCApplication实例,使得sharedApplication()有效
//	2: 重载CCApplicationProtocol提供的几个回调访问,来控制对象创建/销毁的进程
class  AppDelegate : private cocos2d::CCApplication
{
public:

    AppDelegate();
    virtual ~AppDelegate();

public:

	// CCApplication执行运行,进入循环之前调用来初始化游戏环境
	// CCDirector和CCScene的初始化在这里进行
	// 返回真则进入游戏循环,假则直接退出游戏
    virtual bool applicationDidFinishLaunching();

	// 在收到窗体最小化消息后接到通告
	// 在智能机上表现为切换到后台
    virtual void applicationDidEnterBackground();

	// 窗体还原回复后接到通告
	// 在智能机上表现为切换到前台
    virtual void applicationWillEnterForeground();
};

#endif  // __APP_DELEGATE_H__

#include "cocos2d.h"
#include "CCEGLView.h"
#include "AppDelegate.h"
#include "CCLuaEngine.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

USING_NS_CC;

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
    SimpleAudioEngine::end();
}

bool AppDelegate::applicationDidFinishLaunching()
{
	// "导演"的初始化
	// CCDirector::sharedDirector()内部会自动创建实例
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

	// 调试信息显示
    pDirector->setDisplayStats(true);

	// 设置FPS,也就是设定了FPS的上限,比如60帧那么最高60,够用就可以了,200,300的可以用来测试
    pDirector->setAnimationInterval(1.0 / 60);

	// 脚本引擎注册
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

	// 启动脚本的加载和执行
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
    pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
    pEngine->executeScriptFile(path.c_str());
#endif

    return true;
}

// 当应用程序后台化后,比如来电话也会是这个效果,那么这里的操作是:
void AppDelegate::applicationDidEnterBackground()
{
	// 终止所有动画的播放,cocos2dx的操作为不进行场景渲染
    CCDirector::sharedDirector()->stopAnimation();
	// 背景音乐暂停
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// 当应用程序重新回到前台,一切恢复
void AppDelegate::applicationWillEnterForeground()
{
	// 渲染场景
    CCDirector::sharedDirector()->startAnimation();
	// 背景音乐恢复播放
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

 LUA引擎和接口导出这里就不说了,和cocos关系不大,用过Lua的都有所了解,知道怎么用就行.下面说一个Lua里不常用的写法(反正我不常用):

local function()的嵌套,打开hello.lua就可以看到,做了一个相关的实验:

local function printA( content )

	local addin = " From NameA";

	local function printB( content )

		local addin = " From NameB";
		return content .. addin;

	end

	print( content .. addin );
	print( printB( content ) .. addin );

end

print( printA( "Example" ) );

// 输出结果:
Example From NameA
Example From NameB From NameA
>Exit code: 0

//如果这样:
print( printB( "ExampleB" ) );
// 输出结果:
lua: test.lua:19: attempt to call global 'printB' (a nil value)
stack traceback:
	test.lua:19: in main chunk
	[C]: ?
>Exit code: 1

 脚本全解析

-- 跟踪绑定执行函数发生错误的信息并输出
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end

local function main()

	-- 避免脚本泄露,设置脚本内存回收参数
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)

	-- local function cclog(...)的变种
    local cclog = function(...)
        print(string.format(...))
    end

	-- 外在脚本包含,作用类似C++的include方法
    require "hello2"
    cclog("result is " .. myadd(3, 5))

	-- 获取可视区域
    local visibleSize = CCDirector:sharedDirector():getVisibleSize()
	cclog( "visibleSize" .. visibleSize.width .. "#" .. visibleSize.height );

	-- 可视原点坐标(OpenGL坐标系,左下角原点)
    local origin = CCDirector:sharedDirector():getVisibleOrigin()
	cclog( "origin" .. origin.x .. "#" .. origin.y );

	-- 创建精灵(松鼠)
    local function creatDog()

		-- 单帧尺寸设定
        local frameWidth = 105
        local frameHeight = 95

		-- 加载动画资源并创建精灵帧
        local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")	-- 加载精灵动画所在纹理
        local rect = CCRectMake(0, 0, frameWidth, frameHeight)						-- 第一帧帧区域设定
        local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)			-- 创建第一精灵帧
        rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)					-- 第二帧帧区域设定(一共两帧)
        local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)			-- 创建第一精灵帧( PS: 精灵帧(CCSpriteFrame)并不进行像素拷贝,保存指针和必要信息而已)

		-- 基于精灵帧创建一个精灵对象
        local spriteDog = CCSprite:createWithSpriteFrame(frame0)
        spriteDog.isPaused = false
        spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)

		-- 创建精灵帧序列(有序数组)
        local animFrames = CCArray:create()
        animFrames:addObject(frame0)
        animFrames:addObject(frame1)

		-- 根据精灵帧序列创建一个动画实例,0.5是参数delay,帧间隔时间(秒)
        local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)

		-- 根据动画创建动作实例
        local animate = CCAnimate:create(animation);

		-- 设定精灵动作
		-- CCRepeatForever为无限循环播放指定动作的行为控制器
        spriteDog:runAction(CCRepeatForever:create(animate))

		-- 计时回调,用于精灵的移动
        local function tick()

            if spriteDog.isPaused then return end
            local x, y = spriteDog:getPosition()
            if x > origin.x + visibleSize.width then
                x = origin.x
            else
                x = x + 1
            end

            spriteDog:setPositionX(x)

        end

		-- 加入日程表,间隔为0代表每帧
        CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)

        return spriteDog

    end

	-- 创建农场
    local function createLayerFarm()

		-- 为农场单独创建一层
		-- CCLayer是引擎中很重要的一个类,它继承了CCNode的所有特性并负责接收输入事件
        local layerFarm = CCLayer:create()

		-- 添加农场背景图
		-- setPosition是设置场景节点的中心位置
        local bg = CCSprite:create("farm.jpg")
        bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)
        layerFarm:addChild(bg)

		-- 添加地块精灵
        for i = 0, 3 do
            for j = 0, 1 do
                local spriteLand = CCSprite:create("land.png")
                spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
                layerFarm:addChild(spriteLand)
            end
        end

		-- 添加庄稼(这里只取了子图)
        local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95))
        for i = 0, 3 do
            for j = 0, 1 do
                local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop);
                spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
                layerFarm:addChild(spriteCrop)
            end
        end

		-- 把移动的松鼠加进来
        local spriteDog = creatDog()
        layerFarm:addChild(spriteDog)

		-- 触摸相关消息
        local touchBeginPoint = nil

		-- 触摸开始(鼠标按下)
        local function onTouchBegan(x, y)
            cclog("onTouchBegan: %0.2f, %0.2f", x, y)
            touchBeginPoint = {x = x, y = y}	-- 位置记录
            spriteDog.isPaused = true			-- 松鼠定格
			-- 触摸开始的响应必须返回true才会有后续的事件接收
            return true
        end

		-- 触摸拖拽(MouseMoving)
        local function onTouchMoved(x, y)
            -- cclog("onTouchMoved: %0.2f, %0.2f", x, y)
            if touchBeginPoint then
				-- 整个农场层拖动
                local cx, cy = layerFarm:getPosition()
                layerFarm:setPosition(cx + x - touchBeginPoint.x,
                                      cy + y - touchBeginPoint.y)
                touchBeginPoint = {x = x, y = y}
            end
        end

		-- 触摸结束(鼠标弹起)
        local function onTouchEnded(x, y)
            cclog("onTouchEnded: %0.2f, %0.2f", x, y)
            touchBeginPoint = nil			-- 位置记录清空
            spriteDog.isPaused = false		-- 松鼠回复播放
        end

		-- 触摸事件接收函数
        local function onTouch(eventType, x, y)
            if eventType == CCTOUCHBEGAN then
                return onTouchBegan(x, y)
            elseif eventType == CCTOUCHMOVED then
                return onTouchMoved(x, y)
            else
                return onTouchEnded(x, y)
            end
        end

		-- 注册农场层触摸事件脚本通告的相关事项
        layerFarm:registerScriptTouchHandler(onTouch)
        layerFarm:setTouchEnabled(true)

        return layerFarm

    end

	-- 创建界面层,菜单
    local function createLayerMenu()

		-- 作为独立一层(界面)
        local layerMenu = CCLayer:create()

        local menuPopup, menuTools, effectID

		-- 菜单点击回调
        local function menuCallbackClosePopup()
			-- 关闭音效
            SimpleAudioEngine:sharedEngine():stopEffect(effectID)
			-- 隐藏菜单
            menuPopup:setVisible(false)
        end

		-- 菜单点击回调
        local function menuCallbackOpenPopup()
			-- 加载并播放音效
            local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
            effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
			-- 菜单显示
            menuPopup:setVisible(true)
        end

		-- 创建一个弹出菜单(背包面板?)
        local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
        menuPopupItem:setPosition(0, 0)
        menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
        menuPopup = CCMenu:createWithItem(menuPopupItem)
        menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
        menuPopup:setVisible(false)
        layerMenu:addChild(menuPopup)

		-- 添加左下角的工具按钮,用来触发弹出菜单
        local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
        menuToolsItem:setPosition(0, 0)
        menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
        menuTools = CCMenu:createWithItem(menuToolsItem)
        local itemWidth = menuToolsItem:getContentSize().width
        local itemHeight = menuToolsItem:getContentSize().height
        menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
        layerMenu:addChild(menuTools)

        return layerMenu

    end

	-- 播放背景音乐
    local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3")
    SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)

	-- 预加载音效
    local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
    SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)

	-- 创建场景将农场层和界面层依次加入场景
    local sceneGame = CCScene:create()
    sceneGame:addChild(createLayerFarm())
    sceneGame:addChild(createLayerMenu())

	-- 设定为当前场景并执行
    CCDirector:sharedDirector():runWithScene(sceneGame)

end

-- 执行脚本函数并捕获错误信息
-- 函数原型: xpcall( 调用函数, 错误捕获函数 );
xpcall(main, __G__TRACKBACK__)

 结束~ :)

补充一下README的内容:


1. 使用tolua++自动生成C++导出文件

    命令: tolua++.exe -tCocos2d -o LuaCocos2d.cpp Cocos2d.pkg

    在编写.pkg并且将他们包含在主编译文件后,调用上面的命令行进行CPP文件生成

2. 如何编写.pkg文件

    1) 枚举写法保持不变
    2) 去掉引擎对类的CC_DLL库导入导出定义,注意多继承的情况
    3) 移除内联函数的inline关键字和实现细节
    4) 移除权限设定关键字,public,protected,private
    5) 移除类成员变量的申明
    6) 保留静态static关键字
    7) 移除那些申明为私有或者保护的成员方法

原文地址:https://www.cnblogs.com/KevinYuen/p/2952915.html