require "Cocos2d"
require "Cocos2dConstants"
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
return msg
end
local function main()
collectgarbage("collect")
--避免内存泄漏
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
--初始化导演
local director = cc.Director:getInstance()
local glview = director:getOpenGLView()
if glview == nil then
glview = cc.GLView:createWithRect("HelloLua", cc.rect(0, 0, 960, 640))
director:setOpenGLView(glview)
end
glview:setDesignResolutionSize(960, 640, cc.ResolutionPolicy.SHOW_ALL)
--显示帧率
director:setDisplayStats(true)
--设置帧率
director:setAnimationInterval(1.0 / 60)
--增加搜索路径
cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")
--创建场景
local scene = require("GameScene")
local settingScene = scene.create()
--运行场景
if cc.Director:getInstance():getRunningScene() then
cc.Director:getInstance():replaceScene(settingScene)
else
cc.Director:getInstance():runWithScene(settingScene)
end
end
xpcall(main, __G__TRACKBACK__)
require "Cocos2d"
require "Cocos2dConstants"
-- 定义常量
func = 1
funcN = 2
funcND = 3
local size = cc.Director:getInstance():getWinSize()
local debug = true
cclog = function(...)
if debug then
print(string.format(...))
end
end
local GameScene = class("GameScene",function()
local scene = cc.Scene:createWithPhysics()
scene:getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)
return scene
end)
function GameScene:ctor()
self.visibleSize = cc.Director:getInstance():getVisibleSize()
self.origin = cc.Director:getInstance():getVisibleOrigin()
end
function GameScene.create()
local scene = GameScene.new()
scene:addChild(scene:createAction())
return scene
end
function GameScene:createAction()
local layer = cc.Layer:create()
local bg = cc.Sprite:create("Background.png")
bg:setPosition(cc.p(size.width/2,
size.height/2))
layer:addChild(bg)
local function OnClickMenu(tag, menuItemSender)
cclog("tag = %d", tag)
actionFlag = menuItemSender:getTag()
local scene = require("ActionScene")
local nextScene = scene.create()
local ts = cc.TransitionJumpZoom:create(1, nextScene)
cc.Director:getInstance():pushScene(ts)
end
local pItmLabel1 = cc.Label:createWithBMFont("fonts/fnt2.fnt","CallFunc")
local pItmMenu1 = cc.MenuItemLabel:create(pItmLabel1)
pItmMenu1:setTag(func)
pItmMenu1:registerScriptTapHandler(OnClickMenu)
local pItmLabel2 = cc.Label:createWithBMFont("fonts/fnt2.fnt", "CallFuncN")
local pItmMenu2 = cc.MenuItemLabel:create(pItmLabel2)
pItmMenu2:setTag(funcN)
pItmMenu2:registerScriptTapHandler(OnClickMenu)
local pItmLabel3 = cc.Label:createWithBMFont("fonts/fnt2.fnt", "CallFuncND")
local pItmMenu3 = cc.MenuItemLabel:create(pItmLabel3)
pItmMenu3:setTag(funcND)
pItmMenu3:registerScriptTapHandler(OnClickMenu)
local menu = cc.Menu:create(pItmMenu1, pItmMenu2, pItmMenu3)
menu:alignItemsVertically()
layer:addChild(menu)
return layer
end
return GameScene
</pre><pre name="code" class="cpp">require "Cocos2d"
require "Cocos2dConstants"
local size = cc.Director:getInstance():getWinSize()
local HIDE_TAG = true
local isPlaying = false
local plane
local ActionScene = class("ActionScene",function()
return cc.Scene:create()
end)
function ActionScene:ctor()
cclog("ActionScene init")
end
function ActionScene.create()
local scene = ActionScene.new()
scene:addChild(scene:createLayer())
return scene
end
function ActionScene:createLayer()
--函数动作
cclog("ActionScene actionFlag = %d", actionFlag)
local layer = cc.Layer:create()
local bg = cc.Sprite:create("Background.png")
bg:setPosition(cc.p(size.width/2, size.height/2))
layer:addChild(bg)
plane = cc.Sprite:create("Plane.png")
plane:setPosition(cc.p(size.width/2, size.height/2))
layer:addChild(plane)
local backMenuItem = cc.MenuItemImage:create("Back-up.png","Back-down.png")
backMenuItem:setPosition(cc.Director:getInstance():convertToGL(cc.p(120, 100)))
local goMenuItem = cc.MenuItemImage:create("Go-up.png","Go-down.png")
goMenuItem:setPosition(size.width/2, 100)
local menu = cc.Menu:create(backMenuItem, goMenuItem)
menu:setPosition(cc.p(0, 0))
layer:addChild(menu)
local function backMenu(pSender)
cclog("ActionScene backMenu")
cc.Director:getInstance():popScene()
end
backMenuItem:registerScriptTapHandler(backMenu)
local function goMenu(pSender)
cclog("ActionScene goMenu")
if actionFlag == func then
self:OnCallFunc()
elseif actionFlag == funcN then
self:OnCallFuncN()
else
self:OnCallFuncND()
end
end
goMenuItem:registerScriptTapHandler(goMenu)
return layer
end
local function CallBack1()
plane:runAction(cc.TintBy:create(0.5, 255, 0, 255))
end
function ActionScene:OnCallFunc()
cclog("ActionScene OnCallFunc")
local ac1 = cc.MoveBy:create(2, cc.p(100, 100))
local ac2 = ac1:reverse()
local acf = cc.CallFunc:create(CallBack1)
local seq = cc.Sequence:create(ac1, ac2, acf)
plane:runAction(cc.Sequence:create(seq))
end
local function CallBack2(pSender)
local sp = pSender
sp:runAction(cc.TintBy:create(1, 255, 0, 255))
end
function ActionScene:OnCallFuncN()
cclog("ActionScene OnCallFuncN")
local ac1 = cc.MoveBy:create(2, cc.p(100, 100))
local ac2 = ac1:reverse()
local acf = cc.CallFunc:create(CallBack2)
local seq = cc.Sequence:create(ac1, ac2, acf)
plane:runAction(cc.Sequence:create(seq))
end
local function CallBack3(pSender, table)
local sp = pSender
for k,v in pairs(table) do
print(k,v)
end
cclog("CallBack3 %d", table[1])
sp:runAction(cc.TintBy:create(table[1], table[2], table[3], table[4]))
end
function ActionScene:OnCallFuncND()
cclog("ActionScene OnCallFuncND")
local ac1 = cc.MoveBy:create(2, cc.p(100, 100))
local ac2 = ac1:reverse()
local acf = cc.CallFunc:create(CallBack3, {1, 255, 0, 255})
local seq = cc.Sequence:create(ac1, ac2, acf)
plane:runAction(cc.Sequence:create(seq))
end
return ActionScene