【问题标题】:Corona simple eventListener电晕简单事件监听器
【发布时间】:2013-02-21 11:34:05
【问题描述】:

我不知道为什么我的按钮在按下时不会改变外观,谁能告诉我下面的 sn-p 是否有问题。我正在使用 Corona SDK

这是我完整的 main.lua,如果我删除事件侦听器,buttonswap 可以工作。

display.setStatusBar(display.HiddenStatusBar);

local textObj = display.newRetinaText("Click and see what happens!", 40, 40, nil, 0)
textObj:setTextColor(255,0,0);

local widget = require "widget";

local button = widget.newButton{
    default = "Button.png",
    over = "ButtonClicked.png",
    onPress = button.touch;
      }

button.x = display.contentCenterX;
button.y = display.contentCenterY+200;

local function touch(e)
if(e.phase == "began")then
        textObj.text = "Clicked!";
        textObj:setTextColor(255,255,255);
elseif(e.phase == "ended") then
        textObj.text = "Released!";
        textObj:setTextColor(255,0,0);
    end
end

现在我只能看到文字,而不是按钮!

【问题讨论】:

    标签: mobile lua coronasdk


    【解决方案1】:

    我已经测试了您的代码,并且按钮在单击时会发生变化。我能想到的唯一原因是缺少 ButtonClicked.png。检查控制台(名为 Corona Simulator Output 的窗口)是否有任何错误。

    另外,覆盖基础: 确保你运行的是你正在编辑的同一个 main.lua。

    修改后:

    我相信您正在使用

    覆盖默认按钮行为
    button:addEventListener("touch", button);
    

    尝试使用构造函数参数onPress

    local button = widget.newButton{
        default = "Button.png",
        over = "ButtonClicked.png",
        onPress = touch
    }
    

    【讨论】:

    • 它是正确的main.lua,而ButtonClicked与那个主文件在同一个文件夹中......奇怪
    • 事件监听器是否覆盖了我的按钮定义?在那种情况下我将如何解决它?
    • 感谢您的宝贵时间,鉴于我是 corona 的新手,我将再次编辑我的代码。还是不行
    • 再次编辑。我应该在控制台中至少有一些错误。
    【解决方案2】:

    好的,我解决了!我是这样做的。

    local button = widget.newButton{
    default = "Button.png",
    over = "ButtonClicked.png",
    
    }
    button.x = display.contentCenterX;
    button.y = display.contentCenterY+200;
    
    
    function button:tap( e )
    -- Do what you want to do when event occurs
    end
    
    button:addEventListener( "tap", button );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2023-03-14
      • 2014-02-21
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多