【问题标题】:Simple Corona SDK App - Touch Event简单的 Corona SDK 应用程序 - 触摸事件
【发布时间】:2014-01-17 05:28:04
【问题描述】:

我是 Lua 新手,目前正在开发一个名为 Red VS Blue 的项目。它基本上适用于红队和蓝队两支球队,并起到记分牌的作用。如果我单击左侧,红色得到 1 分。如果我点击蓝色的一面,蓝色会得到一分。屏幕分辨率为 320 x 480,但会根据设备而变宽。 这是一个屏幕截图: http://i40.tinypic.com/5lqosk.jpg

这是我的代码:

display.setStatusBar(display.HiddenStatusBar);
local redscore = 0
local bluescore = 0
local background = display.newImage("images/background.png");
local redc = display.newImage("images/redc.png", 0, 0);
local bluec = display.newImage("images/bluec.png", 240, 0);
local redtext = display.newText(redscore, 55, 100, native.systemFont, 32*4);
local bluetext = display.newText(bluescore , 290, 100, native.systemFont, 32*4);

local function redt( event )
    redscore = redscore + 1;
    return true
end
redc:addEventListener( "tap", redt )

local function bluet( event )
    bluescore = bluescore + 1;
    return true
end
bluec:addEventListener( "tap", bluet )

redc 和 bluec 是充当传感器的空白图片

【问题讨论】:

  • 你的问题是什么?
  • 好吧,触摸功能不起作用!

标签: lua coronasdk


【解决方案1】:

如果您想缩放图片以始终完美地适合屏幕的一半,请执行以下操作:

--[[
    Take half the width of the device and adjust for larger screens, dividing by
    the current width of the picture to produce the value it needs to be scaled
    by to fit
]]
local wScale = ((display.layoutWidth/2) - display.screenOriginX) / redc.contentWidth

--Do the same for the height
local hScale = ((display.layoutHeight/2) - display.screenOriginY) / redc.contentHeight

--Scale the image
redc:scale(wScale,hScale)

--[[
    Position the image. Since the default is a central reference point, you take half of              
    the image's width/height and add the adjustment for larger screens
]]
redc.x = (redc.contentWidth/2) + display.screenOriginX
redc.y = (redc.contentHeight/2) + display.screenOriginY

要在触摸框时更新记分牌,请按如下方式构建事件侦听器:

local function redt( event )
    redscore = redscore + 1;
    redtext.text = tostring(redscore)
    return true
end
redc:addEventListener( "tap", redt )

重复蓝色框。你还想用它做什么?

【讨论】:

  • 非常感谢您的回答!现在可以了。只是事后的想法,通常当您编写 lua 或任何其他语言时,您会发表评论,还是只是为了教我?像这样:--这是一条评论
  • 没问题,Corona 有时会有点麻烦——我不骗你,我已经解决了一些“这个对象为零”的错误,只需使用打印出对象的字符串表示print(tostring(object))。直到今天,这让我感到困惑。最好始终对您的代码进行注释,首先是因为如果您稍后再看它,您有时可能会忘记您已经完成的变通方法或快速修复。其次,因为每个人都以不同的方式编码,所以其他人在没有 cmets 的情况下查看/帮助编辑您的代码将非常困难
  • 注意:在 lua 中 --This is a single line comment --[[ This is a multi-line comment ]] 从我上面的回答中可以看出
猜你喜欢
  • 2013-04-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多