如何统计我点击了多少次我的图像按钮 * LUA *

我已经创建了“Hello World”文本和可点击按钮。当我点击按钮时,“Hello World”会随机跳动。

问题:我不知道如何让我的程序计算我点击按钮的次数。

我的进度:

local myHeight = display.contentHeight
local myWidth = display.contentWidth

local topRightHeight = display.newText ("Height "..myHeight, 300 , 40 ,nil,15)
local topRightWidth = display.newText ("Width "..myWidth, 300,60,nil,15)

local redButton = display.newImage ("button.png",0,0)
redButton.x = display.contentWidth -60
redButton.y = display.contentHeight -62.5

local textObj = display.newText ("Hello World",0,0,native.systemFont,18)
textObj: setFillColor(0,250,0)
textObj.x = 40
textObj.y = 30

local number = 0

number = display.newText (number, 30, 30 , native.systemFont, 25)

function moveButtonRandom (event)
textObj.x = math.random(50, display.contentWidth -50)
textObj.y = math.random(50, display.contentHeight -50)

end

redButton: addEventListener ("tap", moveButtonRandom)
点赞
用户2987421
用户2987421

你可以使用 int 类型的标志来计算按钮点击的次数。

最初将 flag 设为 0,如果按钮被点击,则在 onclick() 方法内使用 flag=flag+1

2014-01-30 07:16:56
用户88888888
用户88888888

你可以通过在“clickListener”中监控一些标志来跟踪点击。在通过“clickListener”单击按钮时递增旗标。类似这样的东西

在您的活动中初始化一个变量,例如 int flagForButton = 0

OnClickListener clickListener1 = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
                    flagForButton++;
        Toast.makeText(MarketSnap.this, String.valueOf(flagForButton), Toast.LENGTH_SHORT).show();
    }
};

将 clickListener 设置在按钮上。像这样

yourbutton.setOnClickListener(clickListener1);

EDIT

在回答这个问题时,问题被标记为“Android”。 所以我把代码放在了上面。 但是现在问题已被编辑并标记为“LUA”。 因此,逻辑仍相同,您可以在按钮的“clickListener”中递增“flagcount”。

2014-01-30 07:20:59
用户1925928
用户1925928

现在它工作得像魅力一样

display.setStatusBar(display.HiddenStatusBar)

local redButton = display.newImage ("button.png",0,0)
redButton.x = display.contentWidth - 60
redButton.y = display.contentHeight - 62.5

local textObj = display.newText("Hello World", 0, 0, native.systemFont, 18)
textObj:setFillColor(0, 250, 0)
textObj.x = 40
textObj.y = 30

local number = 0
local textField = display.newText(number, 30, 30, native.systemFont, 25)

local function moveButtonRandom(event)
    textObj.x = math.random(50, display.contentWidth - 50)
    textObj.y = math.random(50, display.contentHeight - 50)
    number = number + 1
    textField:removeSelf()
    textField = display.newText(number, 30, 30, native.systemFont, 25)
end

redButton:addEventListener("tap", moveButtonRandom)
2014-01-30 19:21:01
用户2409015
用户2409015

每次点击时更新文本数字。无需删除并重新创建。

local number = 0
local textField = display.newText(number, 30, 30, native.systemFont, 25)

local function updateNumber(n)
    textField.text = n
end

local function moveButtonRandom(event)
    textObj.x = math.random(50, display.contentWidth - 50)
    textObj.y = math.random(50, display.contentHeight - 50)
    number = number + 1
    updateNumber(number)
end

redButton:addEventListener("tap", moveButtonRandom)
2014-03-20 09:09:31