我正在尝试使用以下内容减少star1.movementSpeed = 10000;
值
local function star1incr()
star1.movementSpeed = star1.movementSpeed - 1
print( "- 1" )
end
timer.performWithDelay(10000, star1incr, 0)
但我收到此错误:\main.lua:18: attempt to index global 'star1' (a nil value)
完整代码
-- requires
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local starTable = {} -- Set up star table
local physics = require "physics"
physics.start()
function initStar()
local star1 = {}
star1.imgpath = "Star1.png"; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable
end --END initStar()
local function star1incr()
star1.movementSpeed = star1.movementSpeed - 1
print( "- 1" )
end
timer.performWithDelay(10000, star1incr, 0)
function getRandomStar()
local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) -- Set image path for object
if ( temp.imgpath == "Cloud4.png" ) then
physics.addBody( randomStar, "static", { density=.1, bounce=.1, friction=.2, radius=45 } )
end
randomStar.myName = "star" -- Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
randomStar.x = math.random(10, _W);
randomStar.y = -35;
randomStar.rotation = math.random(0,20) -- Rotate the object
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=500,
onComplete = function(self) self.parent:remove(self); self = nil; end
}) -- Move the star
end--END getRandomStar()
function startGame()
starTimer1 = timer.performWithDelay(1000,getRandomStar, 0)
end--END startGame()
initStar()
startGame()
我该如何解决这个问题?
答案 0 :(得分:1)
您将star1声明为local,然后在函数结束后将其放入可启动表中star1将不再存在。
所以你需要使用starTable [1]而不是star1作为第一颗星,或者将星号作为参数传递。
答案 1 :(得分:0)
是的,这有效:D
local function star1incr()
starTable[1].movementSpeed = starTable[1].movementSpeed - 1
print( "- 1" )
end