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 が存在しなくなったら、それを起動可能なテーブルに配置します。

そのため、最初のスターには star1 ではなく starTable[1] を使用するか、パラメーターとしてスター番号を渡す必要があります。

于 2013-02-21T13:18:59.197 に答える