1
votes

I ran this code and it gave me an error attempt to index global 'self' (a nil value), in this scene i'm creating the Question1 of the game, inside which includes creating cannon, balloons and other game elements. I checked but i'm not sure whats wrong here.

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

2 Answers

3
votes

You probably need function scene:createScene(). Note the colon instead of the dot in your original code.

0
votes

Your function should be like this.

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.

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

end

Here is the reference: http://docs.coronalabs.com/api/library/storyboard/