0

我正在尝试通过以下方式降低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()

我怎样才能解决这个问题 ?

4

2 回答 2

1

您将star1 声明为本地,然后在函数结束后将其放入可启动表中,star1 不再存在。

所以你需要使用starTable[1] 而不是star1 作为第一颗星或者传入星号作为参数。

于 2013-02-21T13:18:59.197 回答
0

是的,这有效:D

   local function star1incr()
        starTable[1].movementSpeed = starTable[1].movementSpeed - 1
        print( "- 1" )
    end
于 2013-02-21T13:44:26.753 回答