Corona SDK - LUA数组的设置属性在模拟器上运行,但不在设备上运行

时间:2014-08-22 22:31:55

标签: android lua corona

此函数逐行读取文件,并将全局变量设置为相应读取的值。然而,问题是这个代码在模拟器上工作100%,但是一旦编译并在Android设备上运行它就会停止工作。通过使用显示文本,我将错误定位在代码行上:“mapObject [i] .x = sum”。属性.x属于real类型,我已经确认sum,numerator和denominator都是数值数据类型。应用程序在第一次运行包含“mapObject [i] .x = sum”的循环时崩溃,因此i = 1. 1在数组的范围内。任何帮助表示赞赏,我无法理解这一点。这是功能代码:

-- Read map from file
local function loadmap(mapnum)
    local tempstring
    local numerator
    local denominator
    local sum
    local i

    local path = system.pathForFile( "maps/map" .. mapnum .. ".txt", system.ResourceDirectory)
    --Open the file
    local fh = io.open( path, "r" )

    if fh then
        -- read the lines of data systematically.
        mapName = fh:read( "*l" )
        mapNPCS = fh:read( "*l" )
        mapSpawnTimer = tonumber(fh:read( "*l" ))
        mapSpawnMax = tonumber(fh:read( "*l" ))
        mapSpawnMaxLocations = tonumber(fh:read( "*l" ))
        mapObjectMax = tonumber(fh:read( "*l" ))

        for i = 1, mapObjectMax do
            tempstring = fh:read( "*l" )
            mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")

            -- Fractional mapping for all screen sizes
            denominator = tonumber(fh:read( "*l" ))
            numerator = tonumber(fh:read( "*l" ))
            sum = (display.contentWidth / denominator) * numerator
            mapObject[i].x = sum

            denominator = tonumber(fh:read( "*l" ))
            numerator = tonumber(fh:read( "*l" ))
            sum = (display.contentHeight / denominator) * numerator
            mapObject[i].y = sum

            mapObject[i].myName = "object"      

            physics.addBody(mapObject[i], "kinematic", {density = 10.0, friction = 0.0})
        end

        for i = 1, mapSpawnMaxLocations do
            mapSpawnX[i] = tonumber(fh:read( "*l" ))
            mapSpawnY[i] = tonumber(fh:read( "*l" ))
        end

        io.close( fh )
    end
end

这是代码附带的全局变量:

-- Map Data Variables
local mapName
local mapNPCS
local mapSpawnTimer
local mapSpawnMax
local mapSpawnMaxLocations
local mapObjectMax
local mapObject = {}
local mapSpawnX = {}
local mapSpawnY = {}

-- Map Npc Spawning
local mapNPC = {}
local maxMapNPCS = 30
local npcsSpawned
local spawnTimer = 0

2 个答案:

答案 0 :(得分:0)

在物理设备上尝试一些事情:

  • 如果您使用sum = display.contentWidth它是否有效?
  • sum = 100怎么样?
  • 这可能是分母是0吗? (虽然这不应该导致崩溃,只是一个错误)
  • 尝试将mapObject[i].x = sum替换为mapObject[i].x = 100,它是否仍会崩溃?
  • 可能是mapObject [i]不是有效的显示对象,比如mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")中的路径是无效还是tempstring为空? (虽然你认为display.newImage('graphics/.png')不应该造成任何麻烦)

答案 1 :(得分:0)

我猜这个:

mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")

正在生成一个在设备上无效的文件名。设备区分大小写,而模拟器则不区分大小写。