您的位置:首页 > 移动开发 > Cocos引擎

小羊驼和你一起学习cocos2d-x与lua之五(lua编写cocos2d-x游戏)

2013-04-11 13:19 429 查看
欢迎转载:请保留原文出处。
http://blog.csdn.net/linyongliang?viewmode=list
这里我用编辑工具是Sublime Text 2 支持lua语言的高亮与智能提示填写。百度可以找到win 64 32 破解 汉化版

这里我用的调试工具是decode 或者luaeditor 百度也有破解汉化版,调试可以,编辑比较麻烦。

一下是第一个lua写的cocos2d-x游戏demo

local function main()
--内存回收
collectgarbage("setpause",100)
collectgarbage("setstepmul",5000)
local cclog=function ( ... )
print(string.format(...))
end
--设计的分辨率
local frameWidth = 1280
local frameHeight = 720
local ratioWidht=1
local ratioHeight = 1
local ratioElement=1
local function getRatio()
local size=CCDirector:sharedDirector():getWinSize()
ratioWidht=size.width/frameWidth
ratioHeight=size.height/frameHeight
if ratioWidht>=ratioHeight then
ratioElement=ratioHeight
else
ratioElement=ratioWidht
end
end
local function createLayer()
local layer = CCLayer:create()
local cache = CCSpriteFrameCache:sharedSpriteFrameCache()
cache:addSpriteFramesWithFile("logo.plist","logo.png")
local spriteLoadBackground=CCSprite:createWithSpriteFrameName("LoadingBG.png")
spriteLoadBackground:setPosition(frameWidth*0.5*ratioWidht,frameHeight*0.5*ratioHeight)
spriteLoadBackground:setScaleX(ratioWidht)
spriteLoadBackground:setScaleY(ratioHeight)
layer:addChild(spriteLoadBackground)
return layer
end
--流程
getRatio()
local scene=CCScene:create()
scene:addChild(createLayer())
CCDirector:sharedDirector():runWithScene(scene)
end
function __G__TRACKBACK__(msg)
print("----------------------------------------")
print("LUA ERROR: " .. tostring(msg) .. "\n")
print(debug.traceback())
print("----------------------------------------")
end
xpcall(main, __G__TRACKBACK__)
小羊驼给你讲解一下 这个是最后一行是xpcall(main, __G__TRACKBACK__) lua中传入入口函数main 和打印错误的函数。入口函数中我先调用获取分辨率函数,来设定分辨率。然后新建一个场景和层,在层中新建一个精灵,设定精灵的各种属性,加进去。嘻嘻效果图如下


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: