当前位置:   article > 正文

cocos2d 3.2的一个简单lua demo_lua显示一张图片

lua显示一张图片

开发工具:Cocos Code IDE

开发环境:MAC

项目效果:显示一张图片

项目代码:

  1. require "Cocos2d"
  2. -- cclog
  3. local cclog = function(...)
  4. print(string.format(...))
  5. end
  6. -- for CCLuaEngine traceback
  7. function __G__TRACKBACK__(msg)
  8. cclog("----------------------------------------")
  9. cclog("LUA ERROR: " .. tostring(msg) .. "\n")
  10. cclog(debug.traceback())
  11. cclog("----------------------------------------")
  12. return msg
  13. end
  14. local function setLayer(gameScene)
  15. local layer = cc.Layer:create()
  16. layer:setAnchorPoint(0,0)
  17. gameScene:addChild(layer)
  18. return layer
  19. end
  20. ---------------------------
  21. --@return #Sprite image sprite
  22. local function setImageSprite(layer)
  23. local sprite = cc.Sprite:create("dog.png")
  24. sprite:setPosition(200, 200)
  25. layer:addChild(sprite)
  26. end
  27. local function main()
  28. collectgarbage("collect")
  29. -- avoid memory leak
  30. collectgarbage("setpause", 100)
  31. collectgarbage("setstepmul", 5000)
  32. cc.FileUtils:getInstance():addSearchPath("src")
  33. cc.FileUtils:getInstance():addSearchPath("res")
  34. local gameScene = cc.Scene:create()
  35. local layer = setLayer(gameScene)
  36. setImageSprite(layer)
  37. cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(480, 320, 0)
  38. if cc.Director:getInstance():getRunningScene() then
  39. cc.Director:getInstance():replaceScene(gameScene)
  40. else
  41. cc.Director:getInstance():runWithScene(gameScene)
  42. end
  43. end
  44. local status, msg = xpcall(main, __G__TRACKBACK__)
  45. if not status then
  46. error(msg)
  47. end



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/652424
推荐阅读
相关标签
  

闽ICP备14008679号