我曾经编程,尽管已经有很多年了,所以我不得不重新学习其中的大部分,我试图做到这一点,因此当单击三个按钮之一时,它会读取附加到该按钮的文件,但是我得到了所有当我运行它时,现在在终端中是nul
我尝试遍历代码段,但找不到任何有效的方法
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local background = display.newImage("background.png")
background.x = centerX
background.y = centerY
local widget = require( "widget" )
-- Path for the file to read
local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
local ranfDirect = system.pathForFile( "ranf_Run.txt" )
local centDirect = system.pathForFile( "cent_Run.txt" )
local function cluthaButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print(cluthaFile)
end
end
local function centButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("C E N T R A L")
end
end
local function ranfButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("R A N F U R L Y")
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}
-- Path for the file to read
--local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
--local ranfDirect = system.pathForFile( "ranf_Run.txt" )
--local centDirect = system.pathForFile( "cent_Run.txt" )
-- Open the file handle
local cluthaFile, errorString = io.open( cluthaDirect, "r" )
local centFile, errorString = io.open( centDirect, "r" )
local ranfFile, errorString = io.open( ranfDirect, "r" )
if not cluthaFile then
-- Error occurred; output the cause
print( "cluthaFile error: " .. errorString )
else
-- Output lines
for line in cluthaFile:lines() do
print( line )
end
-- Close the file handle
--io.close( cluthaFile )
end
cluthaFile = nil
答案 0 :(得分:0)
尝试(未测试)
local widget = require( "widget" )
local function getContentOfFile( fileName, dir )
local contents = ''
local dir = dir or system.ResourceDirectory
local path = system.pathForFile( fileName, dir )
local file, errorString = io.open( path, "r" )
-- Open the file handle
local file, errorString = io.open( path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
contents = file:read( "*a" )
-- Close the file handle
io.close( file )
end
file = nil
return contents
end
local function cluthaButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("clutha_Run.txt") )
end
end
local function centButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("cent_Run.txt") )
end
end
local function ranfButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("ranf_Run.txt") )
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}