從今天開始關注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) 移除那些申明為私有或者保護的成員方法
