尝试对全局变量"self"进行索引(该值为nil)。

我运行了这段代码,但遇到了一个错误,提示尝试索引全局变量 'self'(一个空值)。在这个场景中,我正在创建游戏的第一个问题(Question1),其中包括创建炮弹、气球和其他游戏元素。我检查了代码,但不确定哪里出了问题。

function scene.createScene()
local group = self.view    ---line 27 where i got the error

scoreText = display.newText( "0", 0, 0, globals.font.bold, 32 )
scoreText.x = display.contentCenterX
scoreText.y = 32
group:insert( scoreText )

background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 240
background.y = 195

questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)

infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
background.x = 200
background.y = 100

restartBtn = display.newImage ("restartBtn.png")
group:insert(restartBtn)
background.x = 470
background.y = 300

cannon = display.newImage ("cannon.png")
group:insert(cannon)
background.x = 10
background.y = 270

cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true

--创建气球
function createBalloons(a, b)

  for i = 1, a do
     for j = 1, b do

         local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
         balloon.balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
         balloon.balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
         balloon.balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
         balloon.balloonText1:setFillColor( 1,1, 0 )
         balloon.balloonText2:setFillColor( 1,1, 0 )
         balloon.balloonText3:setFillColor( 1,1, 0 )
         balloon.name = 'balloon'
         physics.addBody(balloon)
         balloon.bodyType = 'static'
         table.insert(balloons, balloon)
         end
    end
    target.text = #balloons
end

--炮台充能
function cannonCharge:touch(e)
  if(e.phase == 'began') then
        impulse = 0
        cannon.isVisible = true
        Runtime:addEventListener('enterFrame', charge)
    end
end

--充能特效
function charge()
local degreesPerFrame = 1
cannon.rotation = cannon.rotation - degreesPerFrame
     impulse=impulse-0.2

     if(cannon.rotation < -46) then
          cannon.rotation = -46
          impulse = -3.2
        end
end

--发射炮弹
function shot:touch(e)
    if(e.phase == 'ended') then

        Runtime:removeEventListener('enterFrame', charge)
        cannon.isVisible = false
        cannon.rotation = 0

        local cannonBall = display.newImage('cannon ball.png', 84, 220)
        physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
        group:insert(cannonBall)

-- 发射炮弹
 cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )

--碰撞监听
cannonBall:addEventListener ('collision', ballCollision)

    end
end

--炮弹与气球碰撞
function ballCollision(e)
   if (e.other.name == 'balloon') then
            scene.updateScore()
        e.target:removeSelf()
        print ('remove balloon text')
            e.other:removeSelf()
            audio.play(pop)
        end
    end

cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )

--碰撞监听
cannonBall:addEventListener ('collision', ballCollision)

 scene.view:insert( ballCollision )

end
点赞
用户107090
用户107090

你可能需要 function scene:createScene()。请注意你原始代码里的 冒号 而不是 点号

2014-03-18 23:48:52
用户2409015
用户2409015

你的功能应该像这样。

function scene:createScene( event )

    local group = self.view

    -----------------------------------------------------------------------------

    --      在此处创建显示对象并将它们添加到“group”中。
    --      例如使用情况:从之前保存的状态恢复“group”。

    -----------------------------------------------------------------------------

end

参考文档如下: http://docs.coronalabs.com/api/library/storyboard/

2014-03-20 05:03:55