Lua_多点触摸

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值