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

Lua 尝试索引“全局”;“自我”;(零值),lua,coronasdk,Lua,Coronasdk,我运行了这段代码,它给了我一个错误尝试索引全局“自我”(一个零值),在这个场景中,我正在创建游戏的问题1,其中包括创建大炮、气球和其他游戏元素。我查过了,但我不确定这里出了什么问题 function scene.createScene() local group = self.view ---line 27 where i got the error scoreText = display.newText( "0", 0, 0, globals.font.bold, 32 ) score

我运行了这段代码,它给了我一个错误尝试索引全局“自我”(一个零值),在这个场景中,我正在创建游戏的问题1,其中包括创建大炮、气球和其他游戏元素。我查过了,但我不确定这里出了什么问题

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
函数scene.createScene()
本地组=self.view---第27行,我在那里得到了错误
scoreText=display.newText(“0”,0,0,globals.font.bold,32)
scoreText.x=display.contentCenterX
scoreText.y=32
组:插入(文本)
背景=display.newImage(“bkg_clouds.png”)
分组:插入(背景)
背景x=240
背景y=195
questionText=display.newText('a',display.contentCenterX,display.contentWidth/4,native.systemFont,40)
分组:插入(问题文本)
infoBar=display.newImage(“infoBar.png”)
组:插入(信息栏)
背景x=200
背景y=100
restartBtn=display.newImage(“restartBtn.png”)
组:插入(重新启动BTN)
背景x=470
背景y=300
cannon=display.newImage(“cannon.png”)
分组:插入(加农炮)
背景x=10
背景y=270
cannon.anchorX=0.5
cannon.anchorY=0.5
restartbn.isVisible=true
函数createBallodes(a,b)
对于i=1,a do
对于j=1,b do
本地气球=display.newImage('balloon\u fat\u red.png',465+(i*30),80+(j*50))
balloon.balloottext1=display.newText(平假名数组[x+1],495125)
balloon.balloottext2=display.newText(平假名数组[x+2],495175)
balloon.balloottext3=display.newText(平假名数组[x+3],495225)
balloon.BallookText1:setFillColor(1,1,0)
balloon.BallookText2:setFillColor(1,1,0)
balloon.BallooText3:setFillColor(1,1,0)
balloon.name='balloon'
物理。addBody(气球)
balloon.bodyType='static'
表.插入(引出序号、引出序号)
结束
结束
target.text=#引出序号
结束
功能不能充电:触摸(e)
如果(e.phase==“开始”),则
脉冲=0
cannon.isVisible=true
运行时:addEventListener('enterFrame',charge)
结束
结束
功能电荷()
局部degreesPerFrame=1
cannon.rotation=cannon.rotation-degrees帧
脉冲=脉冲-0.2
如果(加农炮旋转<-46),则
旋转角度=-46
脉冲=-3.2
结束
结束
功能快照:触摸(e)
如果(e.phase=='end'),则
运行时:removeEventListener('enterFrame',charge)
cannon.isVisible=false
旋转角度=0
本地炮弹=display.newImage('cannonBall.png',84220)
addBody(炮弹,{密度=1,摩擦=0,反弹=0})
分组:插入(炮弹)
--发射炮弹
炮弹:applylinearpulse(dir,pulse,cannonBall.x,cannonBall.y)
--冲突侦听器
炮弹:添加了监视器(“碰撞”,炮弹碰撞)
结束
结束
函数(e)
如果(e.other.name=='balloon'),则
scene.updateScore()
e、 目标:removeSelf()
打印('删除引出序号文本')
e、 其他:removeSelf()
音频播放(流行音乐)
结束
结束
炮弹:applylinearpulse(dir,pulse,cannonBall.x,cannonBall.y)
--冲突侦听器
炮弹:添加了监视器(“碰撞”,炮弹碰撞)
场景.视图:插入(碰撞)
结束

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

您的函数应该是这样的

功能场景:CreateSecene(事件)

结束

以下是参考资料:

    local group = self.view

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

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

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