尝试对本地变量'star1'进行索引(值为nil)-我已经在所有地方进行了研究,但仍不理解。

我已经卡了一段时间了,真的需要帮助。基本上我想要做的是让这些物体在屏幕上生成,然后允许玩家根据他们的意愿将它们拖动。

目前,随机出现的物体流动到屏幕上,但是当我尝试点击“star1”时,我得到了错误:

Attempt to index local 'star1' (a nil value)

我应该怎么正确地做呢?

这是我的代码:

(display.setStatusBar(display.HiddenStatusBar)

局部物理=require('physics')
physics.start()
--physics.setGravity(0, 0)

_W=display.contentWidth;--返回屏幕宽度
_H=display.contentHeight;--返回屏幕高度

local bg=display.newImage('bg1.png'local starTable={}

Ship=display.newImage(“head.png”)
    Ship.name="ship"
    Ship.x=-80
    Ship.y=100
    physics.addBody(Ship,{isSensor = true})
    Ship.bodyType='dynamic'
    shipIntro=transition.to(Ship,{time = 4000,x = 200)
    Ship.width=40
    Ship.height=40
    Ship.gravityScale = 0

local star1=display.newImage(“acorn.png”)
    Star1.name = "star1"
    物理.addBody(star1,{isSensor = true})
    star1.bodyType = 'static'

function initStar()
    local star1 = {}
    star1.imgpath = "acorn.png"
    star1.movementSpeed = 10000
    table.insert(starTable, star1)

    local star2 = {}
    star2.imgpath = "enemyA.png"
    star2.movementSpeed = 12000
    table.insert(starTable, star2);

    local star3 = {}
    star3.imgpath = "star3.png"
    star3.movementSpeed = 14000
    table.insert(starTable, star3)
end

function getRandomStar()
    local temp = starTable[math.random(1, #starTable)]
    local randomStar = display.newImage(temp.imgpath)
    randomStar.myName = "star"
    randomStar.movementSpeed = temp.movementSpeed
    randomStar.x = math.random(0,_W)
    randomStar.y = _H + 50 -- Start the star off screen
    randomStar.rotation = math.random(0,360)
    starMove = transition.to(randomStar, {
        time=randomStar.movementSpeed,
        y=-45,
        onComplete = function(self) self.parent:remove(self); self = nil; end
        }) -- Move the star
end

function star1:touch( event )
    if event.phase == "began" then

        self.markX = self.x
        self.markY = self.y

    elseif event.phase == "moved" then

        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY

        self.x, self.y = x, y    -- move object based on calculations above
    end

    return true
end

star1:addEventListener( "touch", star1 )

starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)

initStar()
点赞
用户1979583
用户1979583
将你的代码:

local star1 = display.newImage("acorn") -- 我想是第27行


尝试下面的代码:

local star1 = display.newImage("acorn.png") --[[ 添加带后缀/扩展名的图片名称 --]]


继续编码............. :)
2013-08-07 19:53:35