編輯:Android 遊戲開發
圖片資源的打包首選毫無疑問是TexturePacker,官方網站:https://www.codeandweb.com/texturepacker
這大概是2D領域裡最好用的資源打包工具,沒有之一。它把多張小圖拼合成一張大圖,並同步輸出一份plist索引檔——只要指定一個資料夾,裡面的小圖就會被自動合成大圖,合圖流程變得輕鬆無比。更關鍵的是,TexturePacker本身就內建資源加密功能。
使用方法到處都有教程,這裡不再重複,只針對網路上較少提及的兩個重點做補充:命令列與加密。
TexturePacker圖形介面上的所有設定,都能用命令列執行。這帶來一個很大的好處:圖片資源可以完全批次處理。開發前期使用的資源通常尚未壓縮加密,等到要發布時,只要替換成壓縮圖後跑一次命令列批次處理即可,非常省事。
1. 命令列的安裝
預設安裝TexturePacker後,命令列並不會自動加入系統環境變數。設定方式:點擊標題欄的 TexturePacker,選擇安裝命令列即可。
2. 常用參數
以下是TexturePacker的常用參數,完整列表可直接在終端機輸入 TexturePacker 查詢。
You can specify one or more .png or .tps files or directories for processing.
<folder> Recursively adds all known files in the sprite sheet
<*.tps> *.tps file created with TexturePackerPro (requires pro license)
Additional set options override settings in the *.tps file
<*.swf> Flash requires pro license
<images> Image formats, supported:
Output:
--sheet <filename> Name of the sheet to write, see texture-format for formats available
--texture-format <id> Sets the format for the textures.
The format type is automatically derived from the sheet's file name
if possible.
--data <filename> Name of the data file to write
--format <format> Format to write, default is cocos2d
--force-publish Ignore smart update hash and force re-publishing of the files
--texturepath <path> Adds the path to the texture file name stored in the data file.
Use this if your sprite sheets are not stored in another folder
than your data files.
--reduce-border-artifacts Removes dark shadows around sprites
--extrude <int> Extrudes the sprites by given value of pixels to fix flickering
problems in tile maps
--scale <float> Scales all images before creating the sheet. E.g. use 0.5 for half size
--scale-mode <mode> Use mode for scaling:
TexturePacker官方對圖片加密有完整說明:https://www.codeandweb.com/texturepacker/contentprotection
不過cocosBuilder作者已不再維護該工具,官方教程對應的cocos2d 2.x 已有些過時,因此這裡重新整理一次。
查閱cocos2d-x原始碼可以發現加密的關鍵邏輯:用密鑰循環對data位元組做互斥或(XOR)。
// encrypt first part completely
for (; i < len && i < securelen; i++)
{
data[i] ^= s_uEncryptionKey[b++];
if (b >= enclen)
{
b = 0;
}
}
// encrypt second section partially
for (; i < len; i += distance)
{
data[i] ^= s_uEncryptionKey[b++];
if (b >= enclen)
{
b = 0;
}
}
1. 效果預覽
先對比一下加密與未加密的資源包:

VS

經過TexturePacker加密後,資源就無法被直接查看了。實務上建議開發階段使用未加密資源,發布前再用命令列批次處理一次即可。
2. 加密步驟
(1)點擊 ContentProtection 旁的 Lock 圖示,彈出視窗後填入32位元密鑰(0-9、a-f):

Create new key:由系統隨機產生密鑰。
Clear/Disable:啟用/停用圖片加密。
Save as global key:把目前密鑰存為全域密鑰,之後加密其他圖片可直接沿用。
Use global key:使用已儲存的全域密鑰。
(2)Save。(3)Publish。
接下來的步驟與官方說明不同,因為3.0已內建圖片加密的處理。
先說明一個規則:如果你的密鑰是 aaaaaaaabbbbbbbbccccccccdddddddd,必須把它拆成4段、每段8位元:
0xaaaaaaaa
0xbbbbbbbb
0xcccccccc
0xdddddddd
TexturePacker官網的做法是下載它提供的ZipUtils檔案來替換cocos2d-x。但在最新版的cocos2d-x中,zipUtils已經內建了對TexturePacker解密方程的處理,因此不需要照著官網步驟操作(而且直接替換ZipUtils反而會導致cocos2d-x lib編譯出錯)。直接使用cocos2d-x 3.0自帶的即可。
解密相關的原始碼位於 cocos2dx/support/zip_support/ZipUtils.h:
/** Sets the pvr.ccz encryption key.
*
* Example: If the key used to encrypt the pvr.ccz file is
* 0xaaaaaaaabbbbbbbbccccccccdddddddd you will call this function with
* the key split into 4 parts as follows
*
* ZipUtils::ccSetPvrEncryptionKey(0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd);
*
* Note that using this function makes it easier to reverse engineer and
* discover the complete key because the key parts are present in one
* function call.
*
* IMPORTANT: Be sure to call ccSetPvrEncryptionKey or
* ccSetPvrEncryptionKeyPart with all of the key parts *before* loading
* the spritesheet or decryption will fail and the spritesheet
* will fail to load.
*/
static void ccSetPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2,
unsigned int keyPart3, unsigned int keyPart4);
ZipUtils中還有其他類似函數,擇一使用即可。
3. 呼叫方式
在 AppDelegate.cpp 的 applicationDidFinishLaunching 中加入:
ZipUtils::ccSetPvrEncryptionKey(0x12345678, 0x87654321, 0x12345678, 0x87654321);
之後就能正常載入加密資源了。
下面是Lua端使用資源的測試程式碼:
require "Cocos2d"
cclog = function(...)
print(string.format(...))
end
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
end
local function createShowSpriteScene()
cc.SpriteFrameCache:getInstance():addSpriteFrames("files/sprite.plist")
local showSprite = cc.Sprite:createWithSpriteFrameName("success_1.png")
showSprite:setPosition(cc.p(200, 200))
return showSprite
end
local function main()
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
local scene = cc.Scene:create()
local layer = createShowSpriteScene()
scene:addChild(layer)
cc.Director:getInstance():runWithScene(scene)
end
xpcall(main, __G__TRACKBACK__)
如果把 AppDelegate.cpp 中設定的密鑰那行程式碼註解掉,執行時就會直接報錯:

這也反過來驗證了:密鑰必須在載入精靈圖表之前設定完成,解密才會成功。
Android 遊戲開發 7:讓 SurfaceView 真正填滿整個螢幕
上一節我們成功在 SurfaceView 上放進了 Button 與 TextView,功能上已經通了。但如果你要做的是遊戲,畫面被擠在中間一小塊、像個播放視窗一樣,那顯
Android遊戲開發實戰(十一):打造太鼓達人音樂遊戲
玩遊戲時你應該注意過,遊戲裡的音樂通常分為兩類,設置菜單裡往往也會分別標明「游戲音樂」與「游戲音效」。客觀地說,這兩者的差別很明顯:背景音樂同一時間只會播放一首,除非一曲
Android遊戲開發實戰(十五):重力感應控制小球
重力感應的核心,其實就是手機裡的加速度傳感器(accelerometer)。Android平台一共提供了八類傳感器,
23 天從零做一款 Android 遊戲(第 6~8 天):第一批外星人、螢幕方向與道具構想
第 6 天:第一批外星人和螢幕方向好了,現在有一批復古風格的外星人可以在畫面上到處飛了。抱歉,暫時沒有 demo——這款 Android 遊戲目前只有 .apk 安裝包,