zoukankan      html  css  js  c++  java
  • 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范例
    笔记本的硬盘坏了
    “References to generic type List should be parameterized”
    配置管理软件(configuration management software)介绍
    WinCE文件目录定制及内存调整
    使用Silverlight for Embedded开发绚丽的界面(3)
    wince国际化语言支持
    Eclipse IDE for Java EE Developers 与Eclipse Classic 区别
    WinCE Heartbeat Message的实现
    使用Silverlight for Embedded开发绚丽的界面(2)
  • 原文地址:https://www.cnblogs.com/xiajianwei/p/5120974.html
Copyright © 2011-2022 走看看