0
我試圖減少star1.movementSpeed = 10000;
值與:的upvalue一個零值誤差
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()
我該怎麼解決這個 ?