[Resolved] attempt to index upvalue

Hi guys, Can you please help me?
I do all pages with Kwik and would like to modify one page to add a game on that page.
The game is working good separetly but not working integrated. I think this is a problem with director class.

Here is a link of a game http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-whack-a-mole-game-final-steps/
and my code

  
module(..., package.seeall)   
local movieclip = require('movieclip')  
local widget = require("widget")   
  
  
function new()   
  
 local menuGroup = display.newGroup()   
  
 gamesbg = display.newImageRect( imgDir.. "p5\_snakebg.png", 2048, 1536 );   
 gamesbg.x = 1024; gamesbg.y = 768; gamesbg.alpha = 1; gamesbg.oldAlpha = 1   
 menuGroup:insert(gamesbg); menuGroup.gamesbg = gamesbg   
  
local hit = audio.loadSound('hit.wav')  
local titleBg  
local playBtn  
local titleView  
local score  
local w1  
local w2  
local w3  
local w4  
local w5  
local w6  
local w7  
local w8  
local worms  
local lastWorm = {}  
local timerSource  
local currentWorms = 0  
local wormsHit = 0  
local totalWorms = 1  
local loadMain = {}  
local startButtonListeners = {}  
local showGameView = {}  
local prepareWorms = {}  
local startTimer = {}  
local showWorm = {}  
local popOut = {}  
local wormHit = {}  
local alert = {}  
  
function loadMain()  
 score = display.newText('0' .. '/' .. totalWorms, 58, 6, native.systemFontBold, 56)  
 score:setTextColor(238, 238, 238)  
 prepareWorms()  
end  
  
function prepareWorms()  
 w1 = display.newImage('worm.png', 450, 404)  
 w2 = display.newImage('worm.png', 970, 404)  
 w3 = display.newImage('worm.png', 1500, 404)  
 w4 = display.newImage('worm.png', 670, 630)  
 w5 = display.newImage('worm.png', 1285, 630)  
 w6 = display.newImage('worm.png', 270, 875)  
 w7 = display.newImage('worm.png', 955, 875)  
 w8 = display.newImage('worm.png', 1650, 875)  
  
 worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)  
  
  
 for i = 1, worms.numChildren do  
 worms[i]:addEventListener('tap', wormHit)  
 worms[i].isVisible = false  
 end  
  
 startTimer()  
end  
  
function startTimer()  
 timerSource = timer.performWithDelay(1500, showWorm, 0)  
end  
  
function showWorm(e)  
 if(currentWorms == totalWorms) then  
 alert()  
 else  
 lastWorm.isVisible = false  
 local randomHole = math.floor(math.random() \* 8) + 1  
  
 print (lastWorm)  
  
 lastWorm = worms[randomHole]  
  
 lastWorm:setReferencePoint(display.BottomCenterReferencePoint)  
 lastWorm.yScale = 0.1  
 lastWorm.isVisible = true  
  
 Runtime:addEventListener('enterFrame', popOut)  
  
 currentWorms = currentWorms + 1  
 end  
end  
  
function popOut(e)  
 lastWorm.yScale = lastWorm.yScale + 0.07  
  
 if(lastWorm.yScale \>= 1) then  
 Runtime:removeEventListener('enterFrame', popOut)  
 end  
end  
  
function wormHit:tap(e)  
 audio.play(hit)  
 wormsHit = wormsHit + 1  
 score.text = wormsHit .. '/' .. totalWorms  
 lastWorm.isVisible = false  
end  
  
  
function alert()  
 timer.cancel(timerSource)  
 lastWorm.isVisible = false  
  
  
 titleBg = display.newImage('titleBg.png')  
 playBtn = display.newImage('playBtn.png', 1200, 1250)  
 titleView = display.newGroup(titleBg, playBtn)  
  
 local alert = display.newImage('alertBg.png')  
 alert:setReferencePoint(display.CenterReferencePoint)  
 alert.x = display.contentCenterX  
 alert.y = display.contentCenterY  
 transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3})  
  
 local score = display.newText(wormsHit .. '/' .. totalWorms, 220, 690, native.systemFontBold, 50)  
 score:setTextColor(0, 0, 0)  
  
 startButtonListeners('add')  
end  
  
  
function startButtonListeners(action)  
 playBtn:addEventListener('tap', showGameView)  
  
  
end  
  
  
  
  
  
function showGameView:tap(e)  
 transition.to(titleView, {time = 300, y = -titleView.height})  
 score = display.newText('0' .. '/' .. totalWorms, 58, 6, native.systemFontBold, 56)  
 score:setTextColor(238, 238, 238)  
  
end  
  
loadMain()  
 return menuGroup   
  
end   

Problem comes from here

lastWorm = worms[randomHole]  
lastWorm:setReferencePoint(display.BottomCenterReferencePoint)  
lastWorm.yScale = 0.1  
lastWorm.isVisible = true  

Simulator log

Runtime error  
 ...rs\build\page\_5.lua:100: attempt to  
index upvalue 'lastWorm' (a nil value)  
stack traceback:  
 [C]: ?  
 ...rs\build\page\_5.lua:100: in function  
 '\_listener'  
 ?: in function   

Thanks guys

[import]uid: 128187 topic_id: 31826 reply_id: 331826[/import]

@Berlogi,

  1. Are you sure this is right?[code]
    w1 = display.newImage(‘worm.png’, 450, 404)
    w2 = display.newImage(‘worm.png’, 970, 404)
    w3 = display.newImage(‘worm.png’, 1500, 404)
    w4 = display.newImage(‘worm.png’, 670, 630)
    w5 = display.newImage(‘worm.png’, 1285, 630)
    w6 = display.newImage(‘worm.png’, 270, 875)
    w7 = display.newImage(‘worm.png’, 955, 875)
    w8 = display.newImage(‘worm.png’, 1650, 875)

worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)
[/code]

You might try this too:[code]
worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)

w1 = display.newImage(worms, ‘worm.png’, 450, 404)
w2 = display.newImage(worms, ‘worm.png’, 970, 404)
w3 = display.newImage(worms, ‘worm.png’, 1500, 404)
w4 = display.newImage(worms, ‘worm.png’, 670, 630)
w5 = display.newImage(worms, ‘worm.png’, 1285, 630)
w6 = display.newImage(worms, ‘worm.png’, 270, 875)
w7 = display.newImage(worms, ‘worm.png’, 955, 875)
w8 = display.newImage(worms, ‘worm.png’, 1650, 875)
[/code]

  1. I suggest you change your randomHole selector from this: local randomHole = math.floor(math.random() \* 8) + 1

to this: local randomHole = math.random( 1 , 8 ) -- inclusive random between 1 and 8

or to this: local randomHole = math.random( 1 , worms.numChildren ) -- inclusive random between 1 and numChildren

  1. If you don’t want to try these changes or if they make no difference, you can track down the bad index thus:
lastWorm = worms[randomHole]  
if( lastWorm ) then -- OK! non-nil worm  
 lastWorm:setReferencePoint(display.BottomCenterReferencePoint)  
 lastWorm.yScale = 0.1  
 lastWorm.isVisible = true  
  
else -- UH-OH! got a problem here  
 if( randomHole ) then  
 print( tostring(randomHole), " : randomHole is not nil")  
 else  
 print( "randomHole is nil!" )  
 end  

(This will tell you what index fails; You could go further and print them all too. That would tell you what happened just prior to the failure)

I might just be missing it here, but it seems that for some reason you are indexing an invalid slot in the worms group.

Cheers,
Ed

[import]uid: 110228 topic_id: 31826 reply_id: 127016[/import]

Darn. On a second read through I see you are in fact printing the index in your code. Just out of curiosity, is it always the same index or a random one that gives you problems? [import]uid: 110228 topic_id: 31826 reply_id: 127018[/import]

Thank you so much!!!

This solved my problem!

[code]
worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)

w1 = display.newImage(worms, ‘worm.png’, 450, 404)
w2 = display.newImage(worms, ‘worm.png’, 970, 404)
w3 = display.newImage(worms, ‘worm.png’, 1500, 404)
w4 = display.newImage(worms, ‘worm.png’, 670, 630)
w5 = display.newImage(worms, ‘worm.png’, 1285, 630)
w6 = display.newImage(worms, ‘worm.png’, 270, 875)
w7 = display.newImage(worms, ‘worm.png’, 955, 875)
w8 = display.newImage(worms, ‘worm.png’, 1650, 875)
[/code] [import]uid: 128187 topic_id: 31826 reply_id: 127025[/import]

You’re welcome but I see I got caught by the old cut-copy-paste error. I meant this worms = display.newGroup()

Again, glad the problem got straightened out!

-Ed [import]uid: 110228 topic_id: 31826 reply_id: 127029[/import]

@Berlogi,

  1. Are you sure this is right?[code]
    w1 = display.newImage(‘worm.png’, 450, 404)
    w2 = display.newImage(‘worm.png’, 970, 404)
    w3 = display.newImage(‘worm.png’, 1500, 404)
    w4 = display.newImage(‘worm.png’, 670, 630)
    w5 = display.newImage(‘worm.png’, 1285, 630)
    w6 = display.newImage(‘worm.png’, 270, 875)
    w7 = display.newImage(‘worm.png’, 955, 875)
    w8 = display.newImage(‘worm.png’, 1650, 875)

worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)
[/code]

You might try this too:[code]
worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)

w1 = display.newImage(worms, ‘worm.png’, 450, 404)
w2 = display.newImage(worms, ‘worm.png’, 970, 404)
w3 = display.newImage(worms, ‘worm.png’, 1500, 404)
w4 = display.newImage(worms, ‘worm.png’, 670, 630)
w5 = display.newImage(worms, ‘worm.png’, 1285, 630)
w6 = display.newImage(worms, ‘worm.png’, 270, 875)
w7 = display.newImage(worms, ‘worm.png’, 955, 875)
w8 = display.newImage(worms, ‘worm.png’, 1650, 875)
[/code]

  1. I suggest you change your randomHole selector from this: local randomHole = math.floor(math.random() \* 8) + 1

to this: local randomHole = math.random( 1 , 8 ) -- inclusive random between 1 and 8

or to this: local randomHole = math.random( 1 , worms.numChildren ) -- inclusive random between 1 and numChildren

  1. If you don’t want to try these changes or if they make no difference, you can track down the bad index thus:
lastWorm = worms[randomHole]  
if( lastWorm ) then -- OK! non-nil worm  
 lastWorm:setReferencePoint(display.BottomCenterReferencePoint)  
 lastWorm.yScale = 0.1  
 lastWorm.isVisible = true  
  
else -- UH-OH! got a problem here  
 if( randomHole ) then  
 print( tostring(randomHole), " : randomHole is not nil")  
 else  
 print( "randomHole is nil!" )  
 end  

(This will tell you what index fails; You could go further and print them all too. That would tell you what happened just prior to the failure)

I might just be missing it here, but it seems that for some reason you are indexing an invalid slot in the worms group.

Cheers,
Ed

[import]uid: 110228 topic_id: 31826 reply_id: 127016[/import]

Darn. On a second read through I see you are in fact printing the index in your code. Just out of curiosity, is it always the same index or a random one that gives you problems? [import]uid: 110228 topic_id: 31826 reply_id: 127018[/import]

Thank you so much!!!

This solved my problem!

[code]
worms = display.newGroup(w1, w2, w3, w4, w5, w6, w7, w8)

w1 = display.newImage(worms, ‘worm.png’, 450, 404)
w2 = display.newImage(worms, ‘worm.png’, 970, 404)
w3 = display.newImage(worms, ‘worm.png’, 1500, 404)
w4 = display.newImage(worms, ‘worm.png’, 670, 630)
w5 = display.newImage(worms, ‘worm.png’, 1285, 630)
w6 = display.newImage(worms, ‘worm.png’, 270, 875)
w7 = display.newImage(worms, ‘worm.png’, 955, 875)
w8 = display.newImage(worms, ‘worm.png’, 1650, 875)
[/code] [import]uid: 128187 topic_id: 31826 reply_id: 127025[/import]

You’re welcome but I see I got caught by the old cut-copy-paste error. I meant this worms = display.newGroup()

Again, glad the problem got straightened out!

-Ed [import]uid: 110228 topic_id: 31826 reply_id: 127029[/import]