Attempt to index field 'view' (a nil value)

Hello,

I was wondering if anyone can help me figure out why I am getting “Attempt to index field ‘view’ (a nil value)” when I use “scene.view:insert(straw )” in my forward referenced function of a composer scene (at line 31)? Please see code below. Any help would be GREATLY appreciated… pulling my hair out! :slight_smile:

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local function strawTap(e)

transition.to(e.target, { xScale = .01, yScale = .01})

end

local maxStraws = 10

local straws = {“strawberry.png”, “strawberry2.png”, “strawberry3.png”}

local allStraws = {}

local function makeAStraw(id)

local strawidx = rnd(1, 3)

local randX = rnd(400, 700)

local randY = rnd(670, 720)

local randRot = rnd(-10, 10)

local strawsize = rnd(1, 4) / 100

local straw = display.newImage( “images/” … straws[strawidx])

straw.x = randX

straw.y = randY

straw.rotation = randRot

straw:scale ( strawsize, strawsize )

straw.name = “straw” … tostring(id)

straw:addEventListener ( “tap”, strawTap )

allStraws[#allStraws+1] = straw

scene.view:insert( straw )

end

for v = 1, maxStraws do

makeAStraw(v)

end

local function turnOnStraws()

for i = 1, #allStraws do

transition.to(allStraws[i], {time = 3000, xScale = .15, alpha = 1, yScale = .15,})

end

end

[/lua]

Hi Corona Staff and Community. I am still chewing on the issuer presented above. I have attached the strawberry assets here, if anyone would like to run the code and help me figure out what is wrong with how I am inserting them into the scene, you would be my hero!

Any advice/solutions would be so great. 

That code shown above isn’t a complete Composer scene file, for example, it’s missing 

return scene

…at the end.

I plugged your code into a complete Composer scene file I named play.lua and then called that from main like this:

local composer = require(“composer”)

composer.gotoScene(“play”)

The error went away. Oh, I also switched the loop that calls makeAStraw down into the scene:show() function (in the did phase).

You know my email address, if you want to send me your entire project I can try to get you straightened up, but I have 4 classes in the next 24 hours so I’m a tad swamped. :slight_smile: Hopefully what I wrote above will get you on the right path.

 Jay

Thank you so much for providing a solution!!!

Moving the makeAStraw loop down to scene:show( ) did the trick. Makes sense. 

Glad I could help. Basically. don’t run code in the main body of a Composer scene file – almost everything should get triggered initially from inside one of the scene listeners.

 Jay

Hi Corona Staff and Community. I am still chewing on the issuer presented above. I have attached the strawberry assets here, if anyone would like to run the code and help me figure out what is wrong with how I am inserting them into the scene, you would be my hero!

Any advice/solutions would be so great. 

That code shown above isn’t a complete Composer scene file, for example, it’s missing 

return scene

…at the end.

I plugged your code into a complete Composer scene file I named play.lua and then called that from main like this:

local composer = require(“composer”)

composer.gotoScene(“play”)

The error went away. Oh, I also switched the loop that calls makeAStraw down into the scene:show() function (in the did phase).

You know my email address, if you want to send me your entire project I can try to get you straightened up, but I have 4 classes in the next 24 hours so I’m a tad swamped. :slight_smile: Hopefully what I wrote above will get you on the right path.

 Jay

Thank you so much for providing a solution!!!

Moving the makeAStraw loop down to scene:show( ) did the trick. Makes sense. 

Glad I could help. Basically. don’t run code in the main body of a Composer scene file – almost everything should get triggered initially from inside one of the scene listeners.

 Jay