Attempt to Index GlobalCredits 'event' (a nil value)

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have just started to learn LUA in school, but i cannot find many helpful tutorials o the internet to aid in my learning. I have made a simple game (which doesn't work yet, i realize that) and a main menu. However, when i try to start the app, it gives me this error:

/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: attempt to index global 'showCredits' (a nil value)
stack traceback:
/Users/jordanmcbride/Desktop/Lua Projects/Tapinator/main.lua:47: in main chunk
[Finished in 9.4s]

I have looked the error, and I cannot seem to understand how to fix it. The other questions have said something about returning the function returning a nil value, and that I should add a return statement to the end, but that doesn't work either.

Here is the code @ line 47.

function showCredits.touch(e)
    playButton.isVisible = false
    creditsButton.isVisible = false
    creditsView = display.newImage('credits.png', 0, display.contentHeight)

    lastY = name.y
    transition.to(name, {time = 300, y = display.contentHeight * 0.5 - title.height - 25})
    transition.to(creditsView, {time = 300, y = display.contentHeight * 0.5 + creditsView.height, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end

Here is my full code, in case the problem lies elsewhere:

display.setStatusBar(display.HiddenStatusBar)

radius = 40

smallTime = 200
bigTime = 800

score = 0
scoreInc = 2000

--HomePage
local name
local playButton
local creditsButton
local homePage

--Credits
local creditsPage


--Sounds
local circleSpawn = audio.loadSound( "circle_spawn.wav" )
local circleTap = audio.loadSound( "circle_tap.wav" )

function Main()
    name = display.newImage('title.png', display.contentWidth / 2, 53
profilepic.png
manpreet 2 years ago

 

The answer by greatwolf will work. But just a tip from my experience. One way I like to create functions is to try to define the name of the function first near the top of the lua file. Then I will define the function later on in the file. Something like this:

--function preallocation
local onPlayTap
local onSoundOnTap
local onSoundOffTap
local onCreditsTap
local onHelpTap

---------------------------------------------------------------------------------
-- Custom Function Definitions
---------------------------------------------------------------------------------
--Called when Sound On Button is tapped, turn off sound
onSoundOnTap = function(event)

end

--Called when Sound Off Button is tapped, turn on sound
onSoundOffTap = function(event)

end

--Called when Credits button is tapped, shows credits
onCreditsTap = function(event)

end

--Called when Help button is tapped, shows help
onHelpTap = function(event)

end

--Callback to Play button. Moves scene to Level Picker Scene
onPlayTap = function(event)

end

What this does is allow each function to be called by any other function in the file. If you do it the way you are doing it by adding the function name before the function like so:

local showCredits = {}
function showCredits.touch(e)

end

local hideCredits = {}
function hideCredits.touch(e)

end

your showCredit function will not be able to call the hideCredits function below it because the hideCredits variable has not been defined yet when the showCredit function was defined. Although this may not effect your current game, in future apps or games, you may need to call functions inside of other functions. To make this work properly, predefine all your function variables first, then define all your function afterwards. Hope this helps.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.