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("SettingScene")
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__)
</pre><pre name="code" class="cpp">require "Cocos2d"
require "Cocos2dConstants"
local size = cc.Director:getInstance():getWinSize()
local SettingScene = class("SettingScene",function()
return cc.Scene:create()
end)
function SettingScene.create()
local scene = SettingScene.new()
scene:addChild(scene:createLayer())
return scene
end
function SettingScene:ctor()
cclog("SettingScene init")
end
-- create layer
function SettingScene:createLayer()
local layer = cc.Layer:create()
--单点触摸事件(一个物体)
local function touchBegan(touch, event)
return true
end
local function touchMoved(touch, event)
cclog("touchMoved")
end
local function touchEnded(touch, event)
cclog("touchEnded")
end
local function touchCancelled(touch, event)
cclog("touchCancelled")
end
local listener = cc.EventListenerTouchOneByOne:create()
listener:registerScriptHandler(touchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
listener:registerScriptHandler(touchMoved, cc.Handler.EVENT_TOUCH_MOVED)
listener:registerScriptHandler(touchEnded, cc.Handler.EVENT_TOUCH_ENDED)
listener:registerScriptHandler(touchCancelled, cc.Handler.EVENT_TOUCH_CANCELLED)
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, layer)
--单点触摸事件(多个物体进行判断触摸点)
local function touchBegan(touch, event)
cclog("touchBegan")
-- 获取事件所绑定的Node
local node = event:getCurrentTarget()
local locationInNode = node:convertToNodeSpace(touch:getLocation())
local rect = cc.rect(0, 0, node:getContentSize().width, node:getContentSize().height)
if cc.rectContainsPoint(rect, locationInNode) then
node:runAction(cc.ScaleBy:create(0.06, 1.06))
return true
end
end
local function touchMoved(touch, event)
cclog("touchMoved")
-- 获取事件所绑定的Node
local node = event:getCurrentTarget()
local currentPosX, currentPosY = node:getPosition()
local diff = touch:getDelta()
node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y))
end
local function touchEnded(touch, event)
cclog("touchEnded")
-- 获取事件所绑定的Node
local node = event:getCurrentTarget()
local locationInNode = node:convertToNodeSpace(touch:getLocation())
local rect = cc.rect(0, 0, node:getContentSize().width, node:getContentSize().height)
if cc.rectContainsPoint(rect, locationInNode) then
node:runAction(cc.ScaleTo:create(0.06, 1.0))
return true
end
end
local function touchCancelled(touch, event)
cclog("touchCancelled")
end
local bg = cc.Sprite:create("BackgroundTile.png", cc.rect(0, 0, size.width, size.height))
bg:getTexture():setTexParameters(gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT)
bg:setPosition(cc.p(size.width/2, size.height/2))
layer:addChild(bg, 0)
local boxA = cc.Sprite:create("BoxA2.png")
boxA:setPosition(cc.p(size.width/2.0 - 120, size.height/2.0 + 120))
layer:addChild(boxA, 10)
local boxB = cc.Sprite:create("BoxB2.png")
boxB:setPosition(cc.p(size.width/2.0, size.height/2.0))
layer:addChild(boxB, 20)
local boxC = cc.Sprite:create("BoxC2.png")
boxC:setPosition(cc.p(size.width/2.0 + 120, size.height/2.0 + 160))
layer:addChild(boxC, 30)
local listener1 = cc.EventListenerTouchOneByOne:create()
listener1:setSwallowTouches(true)
listener1:registerScriptHandler(touchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
listener1:registerScriptHandler(touchMoved, cc.Handler.EVENT_TOUCH_MOVED)
listener1:registerScriptHandler(touchEnded, cc.Handler.EVENT_TOUCH_ENDED)
listener1:registerScriptHandler(touchCancelled, cc.Handler.EVENT_TOUCH_CANCELLED)
local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, boxA)
local listener2 = listener1:clone()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, boxB)
local listener3 = listener1:clone()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener3, boxC)
end
return SettingScene