Как прочитать конкретный файл с помощью Corona SDK - PullRequest
0 голосов
/ 26 января 2019

Я использую для программирования, хотя прошло уже несколько лет, поэтому мне приходится переучивать большую часть этого, я пытаюсь сделать так, чтобы при нажатии на одну из 3 кнопок он считывал файл, прикрепленный к кнопке, но все, что я получал в данный момент 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

1 Ответ

0 голосов
/ 29 января 2019

Попробуйте (не проверено)

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,
}
...