• quick-cocos2d中自定义按钮(BUTTON)


    1,在cpp下创建一个view分组,然后在里面创建一个MyButton.lua文件:

    local MyButton={}
    print("nihao")
    function MyButton.new(params)
    print("nihao-----")
    local listener=params.listener
    local button
    params.listener=function(sender)
    if params.preapare then
    params.prepare()
    end
    local function zoom1(offset,time,onComplete)
    local x,y=button:getPosition()
    local size=cc.size(193,81)
    local scaleX=button:getScaleX()*(size.width+offset)/size.width
    local scaleY=button:getScaleY()*(size.height-offset)/size.height

    transition.moveTo(button,{y=y-offset,time=time})
    transition.scaleTo(button,{
    scaleX=scaleX,
    scaleY=scaleY,
    time=time,
    onComplete=onComplete,
    })
    end

    local function zoom2(offset,time,onComplete)
    local x,y=button:getPosition()
    local size=cc.size(193,81)

    transition.moveTo(button,{y=y+offset,time=time/2})
    transition.scaleTo(button,{
    scaleX=1.0,
    scaleY=1.0,
    time=time,
    onComplete=onComplete,
    })
    end

    -- button:getParent():setEnabled(false)--先关闭父类的button功能
    print("button")
    zoom1(40,0.08,function()
    zoom2(40,0.09,function()
    zoom1(20,0.10,function()
    zoom2(20,0.1,function()
    print("button")
    -- button:getParent():setEnabled(true)
    local tag=button:getTag()
    listener(tag)
    end)
    end)
    end)
    end)
    end
    button=cc.ui.UIPushButton:new();
    button:setButtonImage(cc.ui.UIPushButton.NORMAL, params.image, false)
    button:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
    if event.name=="ended" then
    params.listener()
    end
    return true
    end)
    return button
    end
    return MyButton

    2:引用自定义的Button:

    local Button=require("app.view.MyButton")
    local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
    end)

    function MainScene:ctor()
    cc.ui.UILabel.new({
    UILabelType = 2, text = "Hello, World", size = 64})
    :align(display.CENTER, display.cx, display.cy)
    :addTo(self)
    local button=Button.new({
    image="anniu-xuanzhong.png",
    listener=function()
    print("click")
    end,
    x=display.cx,
    y=display.cy
    })
    button:setPosition(cc.p(320, 480))
    self:addChild(button)
    end

    function MainScene:onEnter()
    end

    function MainScene:onExit()
    end

    return MainScene

  • 相关阅读:
    JAVA 面向对象的扩展 内部类
    对于win10 更换JDK后 查询JDK路径还是原路径的解决办法
    懂得的懂
    稀疏数组转化二维数组
    Flume的安装配置
    CentOS7配置ip和ssh免密登录和hadoop环境
    AOP的使用和事务
    spring的个人理解
    单车月结算2-修改和删除功能
    单车月结算1
  • 原文地址:https://www.cnblogs.com/xiajianwei/p/5120974.html
Copyright © 2020-2023  润新知