此函数逐行读取文件,并将全局变量设置为相应读取的值。然而,问题是这个代码在模拟器上工作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
答案 0 :(得分:0)
在物理设备上尝试一些事情:
sum = display.contentWidth
它是否有效? sum = 100
怎么样? mapObject[i].x = sum
替换为mapObject[i].x = 100
,它是否仍会崩溃? mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")
中的路径是无效还是tempstring
为空? (虽然你认为display.newImage('graphics/.png')
不应该造成任何麻烦)答案 1 :(得分:0)
我猜这个:
mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")
正在生成一个在设备上无效的文件名。设备区分大小写,而模拟器则不区分大小写。