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__)
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 isContainsPoint(touch, node)
local locationInNode = node:convertToNodeSpace(touch:getLocation())
local rect = cc.rect(0, 0, node:getContentSize().width, node:getContentSize().height)
if cc.rectContainsPoint(rect, locationInNode) then
return true
end
return false
end
local function touchesBegan(touches, event)
cclog("touchesBegan")
local node = event:getCurrentTarget()
if isContainsPoint(touches[1], node) then
node:runAction(cc.ScaleTo:create(0.06, 1.06))
end
end
local function touchesMoved(touches, event)
cclog("touchesMoved")
local node = event:getCurrentTarget()
if isContainsPoint(touches[1], node) then
local currentPosX, currentPosY = node:getPosition()
local diff = touches[1]:getDelta()
node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y))
end
end
local function touchesEnded(touches, event)
cclog("touchesEnded")
local node = event:getCurrentTarget()
if isContainsPoint(touches[1], node) then
node:runAction(cc.ScaleTo:create(0.06, 1.0))
end
end
local function touchesCancelled(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 listener = cc.EventListenerTouchAllAtOnce:create()
listener:registerScriptHandler(touchesBegan, cc.Handler.EVENT_TOUCHES_BEGAN)
listener:registerScriptHandler(touchesMoved, cc.Handler.EVENT_TOUCHES_MOVED)
listener:registerScriptHandler(touchesEnded, cc.Handler.EVENT_TOUCHES_ENDED)
listener:registerScriptHandler(touchesCancelled, cc.Handler.EVENT_TOUCHES_CANCELLED)
local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, boxA)
end
return SettingScene