cocos2d-x-lua:API-瞬时动作

Task3.lua

--region *.lua
--Date
--此文件由[BabeLua]插件自动生成



--endregion

PLACE_TAG=102
FLIPX_TAG=103
FLIPY_TAG=104
HIDE_SHOW_TAG=105
TOGGLE_TAG=106

actionFlag = -1 


local size = cc.Director:getInstance():getWinSize()

local Task3Scene = class("Task3Scene", function()
    return cc.Scene:create()
end)

function Task3Scene:create()
    local scene = Task3Scene:new()
    scene:addChild(scene:createLayer())

    return scene
end


function Task3Scene:ctor()
    self.visibleSize = cc.Director:getInstance():getVisibleSize()
    self.origin = cc.Director:getInstance():getVisibleOrigin()
    self.schedulerID = nil
end


function Task3Scene:createLayer()

    local layer = cc.Layer:create()

    --create bg
    local bg = cc.Sprite:create("background.png")
    bg:setPosition(size.width/2,size.height/2)
    layer:addChild(bg)

    
    local function OnClickMenu(tag,menuItemSender)
    
    	actionFlag = menuItemSender:getTag()
    	local scene = require("MyAction")
    	local nextScene = scene.create()
    	local ts = cc.TransitionJumpZoom:create(1,nextScene)
    	cc.Director:getInstance():pushScene(ts)
    end
    
    
    --1  
    local placeLabel = cc.Label:createWithBMFont("fonts/fnt2.fnt","Place")
    local placeMenu = cc.MenuItemLabel:create(placeLabel)
    placeMenu:setTag(PLACE_TAG)
    placeMenu:registerScriptTapHandler(OnClickMenu)
    
    --2
    local flipXLabel = cc.Label:createWithBMFont("fonts/fnt2.fnt","flipX")
    local flipXMenu = cc.MenuItemLabel:create(flipXLabel)
    flipXMenu:setTag(FLIPX_TAG)
    flipXMenu:registerScriptTapHandler(OnClickMenu)
    
    --3
    local flipYLabel = cc.Label:createWithBMFont("fonts/fnt2.fnt","flipY")
    local flipYMenu = cc.MenuItemLabel:create(flipYLabel)
    flipYMenu:setTag(FLIPY_TAG)
    flipYMenu:registerScriptTapHandler(OnClickMenu)
    
    --4
    local hideLabel = cc.Label:createWithBMFont("fonts/fnt2.fnt","Hide or Show")
    local hideMenu = cc.MenuItemLabel:create(hideLabel)
    hideMenu:setTag(HIDE_SHOW_TAG)
    hideMenu:registerScriptTapHandler(OnClickMenu)
    
    --5
    local toggleLabel = cc.Label:createWithBMFont("fonts/fnt2.fnt","Toggle")
    local toggleMenu = cc.MenuItemLabel:create(toggleLabel)
    toggleMenu:setTag(TOGGLE_TAG)
    toggleMenu:registerScriptTapHandler(OnClickMenu)    
    
    local mn = cc.Menu:create(placeMenu,flipXMenu,flipYMenu,hideMenu,toggleMenu)
    mn:alignItemsVertically()
    layer:addChild(mn)

    return layer
end

return Task3Scene


MyAction.lua

--region *.lua
--Date
--此文件由[BabeLua]插件自动生成



--endregion


local hiddenFlag=true

local size = cc.Director:getInstance():getWinSize()

local MyAction = class("MyAction", function()
    return cc.Scene:create()
end)

function MyAction:create()
    local scene = MyAction:new()
    scene:addChild(scene:createLayer())

    return scene
end

--scene 构造函数
function MyAction:ctor()
    self.visibleSize = cc.Director:getInstance():getVisibleSize()
    self.origin = cc.Director:getInstance():getVisibleOrigin()
    self.schedulerID = nil
end


function MyAction:createLayer()

    local layer = cc.Layer:create()

    --create bg
    local bg = cc.Sprite:create("Background.png")
    bg:setPosition(size.width/2,size.height/2)
    layer:addChild(bg)

    --plane
    local sprite = cc.Sprite:create("Plane.png")
    sprite:setPosition(size.width/2,size.height/2)
    layer:addChild(sprite)

    --backbutton
    local backMenuItem = cc.MenuItemImage:create("Back-up.png","Back-down.png")
    backMenuItem:setPosition(size.width/3,size.height - 100)
    
    --gobutton
    local goMenuItem = cc.MenuItemImage:create("Go-up.png","Go-down.png")
    goMenuItem:setPosition(size.width/2,100)
    
    --menu
    local mn = cc.Menu:create(backMenuItem,goMenuItem)
    mn:setPosition(0,0)
    layer:addChild(mn)
    
    
    
    --back Function
    local function backMenu(pSender)
    	cc.Director:getInstance():popScene()
    end
    
    --backItem registerFunction
    backMenuItem:registerScriptTapHandler(backMenu)
    
   
    --go Function
    local function goMenu(pSender)
    
    	local p = cc.p(math.random()*size.width,math.random()*size.height)
    	
    	if actionFlag == PLACE_TAG then                 --注意:这里actionFlag传入方式
    	   sprite:runAction(cc.Place:create(p))
    	elseif actionFlag == FLIPX_TAG then             --runAction 执行的是瞬时的动作,瞬时的动作时瞬间执行完
    	   sprite:runAction(cc.FlipX:create(true))
        elseif actionFlag == FLIPY_TAG then
           sprite:runAction(cc.FlipY:create(true))
    	elseif actionFlag == HIDE_SHOW_TAG then
    	   if hiddenFlag then
    	       sprite:runAction(cc.Hide:create())
    	       hiddenFlag = false
    	   else
    	       sprite:runAction(cc.Show:create())
    	       hiddenFlag = true
    	   end
    	else
    	   sprite:runAction(cc.ToggleVisibility:create())
    	end
    end
    
    --go register Funciton
    goMenuItem:registerScriptTapHandler(goMenu)

    return layer
end

return MyAction

cocos2d-x-lua:API-瞬时动作_第1张图片

你可能感兴趣的:(cocos2d-x-lua)