尝试索引全局“自我”(零值)

时间:2014-03-18 23:34:02

标签: lua corona

我运行了这段代码,它让我错误地尝试索引全局' 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)

-- Shoot cannon ball
 cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )

--Collision listener
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 )

--Collision listener
cannonBall:addEventListener ('collision', ballCollision)

 scene.view:insert( ballCollision )

end

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

你的功能应该是这样的。

function scene:createScene(event)

    local group = self.view

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

    --      CREATE display objects and add them to 'group' here.
    --      Example use-case: Restore 'group' from previously saved state.

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

以下是参考: http://docs.coronalabs.com/api/library/storyboard/