Cocos2d-x 3.2 Lua樣本 ClickAndMoveTest(點擊Mobile Testing)

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   strong   

Cocos2d-x 3.2 Lua樣本 ClickAndMoveTest(點擊Mobile Testing)

 本篇部落格介紹Cocos2d-x 3.2Lua樣本中點擊移動的例子,在這個例子你可以得到如何建立單點觸摸的事件和註冊事件監聽回調方法。



範例程式碼:
--[[ClickAndMoveTest.lua點擊與移動]]---- 擷取螢幕尺寸local size = cc.Director:getInstance():getWinSize()local layer = nil -- 層local kTagSprite = 1 --精靈標記local function initWithLayer()    local sprite = cc.Sprite:create(s_pPathGrossini)    -- 添加顏色層,黃色    local bgLayer = cc.LayerColor:create(cc.c4b(255,255,0,255))    layer:addChild(bgLayer, -1)        -- 添加    layer:addChild(sprite, 0, kTagSprite)    -- 設定精靈位置到(20,150)    sprite:setPosition(cc.p(20,150))    -- 執行跳的動作,第一個參數為期間,第二個參數為位置,第三個參數為跳的高度,第四個參數跳的次數      sprite:runAction(cc.JumpTo:create(4, cc.p(300,48), 100, 4))    -- 背景層執行無限重複的動作序列,先淡進,再淡出    bgLayer:runAction(cc.RepeatForever:create(cc.Sequence:create(                                                 cc.FadeIn:create(1),                                                 cc.FadeOut:create(1))))    -- 觸摸開始    local function onTouchBegan(touch, event)        return true    end    -- 觸摸結束    local function onTouchEnded(touch, event)        -- 擷取點擊位置        local location = touch:getLocation()                -- 根據標記擷取子節點        local s = layer:getChildByTag(kTagSprite)        s:stopAllActions()-- 停止所有動作        -- 執行移動動作,移動到點擊的位置        s:runAction(cc.MoveTo:create(1, cc.p(location.x, location.y)))        local posX, posY = s:getPosition() -- 擷取精靈的位置        local o = location.x - posX -- X軸的距離        local a = location.y - posY -- Y軸的距離        local at = math.atan(o / a) / math.pi * 180.0 --求角度 ,反正切函數求弧度/π*180.0        --1弧度= 180/π, 1度=π/180        -- 點擊位置在下邊        if a < 0 then            -- 點擊位置在左邊            if o < 0 then                at = 180 + math.abs(at)            else            -- 點擊位置在右邊                at = 180 - math.abs(at)            end        end        -- 執行旋轉的動作        s:runAction(cc.RotateTo:create(1, at))    end    -- 單點觸摸的監聽器    local listener = cc.EventListenerTouchOneByOne:create()    -- 註冊兩個回調監聽方法    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )    local eventDispatcher = layer:getEventDispatcher()-- 時間派發器    -- 綁定觸摸事件到層當中    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)    return layerend---------------------------------- Click And Move Test--------------------------------function ClickAndMoveTest()    cclog("ClickAndMoveTest")    local scene = cc.Scene:create()    layer = cc.Layer:create()    initWithLayer()    scene:addChild(layer)    scene:addChild(CreateBackMenuItem())        return sceneend


Cocos2d-x有兩種觸摸事件,一種是單點觸摸,一種是多點觸摸,本篇部落格例子使用的單點觸摸事件。 EventListenerTouchOneByOne類
-- 單點觸摸的監聽器    local listener = cc.EventListenerTouchOneByOne:create()    -- 註冊兩個回調監聽方法    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )    local eventDispatcher = layer:getEventDispatcher()-- 時間派發器    -- 綁定觸摸事件到層當中    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)

最後的一個方法綁定節點到監聽器中去,觸摸優先順序越低的越先被觸摸。相同優先順序的,後添加的先被觸。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.