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]