Hi, I’m having a problem with coroutines while I do the initial load of my game, I have an animation running while loading my database for initial setup and I do this initial setup in a coroutine but the animation stop for a seconds can someone tell me what I’m doing wrong?
This is my code
main.lua
local animation--global variable --after click a button this executes animation= display.newSprite( sheet, myApp.new\_animations.base\_loading ) animation.x,animation.y=display.contentCenterX,display.contentCenterY animation:play( ) if(isNew) and (myApp.userData.username==nil or myApp.userData.username=="") then local co=coroutine.create( function () composer.loadScene("src.registerView", false,{}) userOperations.resetDatabase(); userOperations.shuffleQuestions(); userOperations.shuffleChallenges(); userOperations.setUserInfo() display.remove( animation ) composer.gotoScene("src.registerView", {effect="crossFade", time = 100}) end) coroutine.resume(co) else composer.loadScene("src.welcomeView", false,{}) questionOperations.getNextCategory2() display.remove( animation ) composer.gotoScene("src.welcomeView", {effect="crossFade", time = 100}) end
userOperations.lua
function userOperations.resetDatabase() DBHandler.restartDatabase() end function userOperations.shuffleQuestions( ) local questions=DBHandler.getQuestions(1)--difficulty 1 local pos=0 questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) pos=#questions; questions=DBHandler.getQuestions(2) questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) pos=pos + #questions; questions=DBHandler.getQuestions(3) questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) end function userOperations.shuffleChallenges( ) local challenges=DBHandler.getChallenges(1)--difficulty 1 local pos=0 challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) local pos=#challenges; challenges=DBHandler.getChallenges(2) challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) local pos=pos + #challenges; challenges=DBHandler.getChallenges(3) challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) end function userOperations.setUserInfo() local users={} users[1]=globals.userData DBHandler.addUsers(users) DBHandler.addUserCredit(globals.userData.id\_user,globals.livesData.register,0,0) DBHandler.addUserLives(globals.userData.id\_user,globals.coinsData.register,0,0) globals.userData.credit=DBHandler.getUserCredit(globals.userData.id\_user) globals.userData.lives=DBHandler.getUserLives(globals.userData.id\_user) globals.userData.progress=DBHandler.getUserProgres(globals.userData.id\_user) globals.userPreferences.created=true return true end local function shuffle(theTable) local theTable = theTable local n = #theTable local j local random = math.random for i=n-1, 1, -1 do j = random(i) theTable[j],theTable[i] = theTable[i],theTable[j] end return theTable end
DBHandler.lua
function dbHandler.restartDatabase() local res; local sql=[=[UPDATE category SET enabled=1; UPDATE question SET enabled=1; UPDATE challenge SET enabled=1; DELETE FROM question\_order; DELETE FROM answer; DELETE FROM user\_credit; DELETE FROM user\_live; DELETE FROM power\_log; DELETE FROM question\_log; DELETE FROM user;]=]; res=db:exec(sql); return res==sqlite3.OK; end function dbHandler.addUsers(users) local res; for i=1,#users do local q = [[INSERT INTO user VALUES (NULL, ']] .. users[i].username .. [[',']] .. users[i].avatar .. [[',]] .. users[i].score.. [[,]] .. users[i].level.. [[,]] ..os.time()..[[,]] .. users[i].current\_page.. [[);]] res=db:exec( q ) end return res==sqlite3.OK; end function dbHandler.getChallenges(dificulty) local data = {} for row in db:nrows('SELECT \* FROM challenge where dificulty='..dificulty) do data[#data+1] = { id\_challenge=row.id\_challenge, name = row.name, options= row.options, answer= row.answer, enabled= row.enabled, position=row.position, type=row.type } end return data; end function dbHandler.getQuestions(dificulty) local data = {} for row in db:nrows('SELECT \* FROM question WHERE dificulty='..dificulty) do data[#data+1] = { id\_question = row.id\_question, id\_category= row.id\_category, dificulty = row.dificulty, question=row.question, correct=row.correct, score=row.score, comment=row.comment, movie=row.movie, enabled=row.enabled, answer=row.answer, option\_a=row.option\_a, option\_b=row.option\_b, option\_c=row.option\_c } end return data; end function dbHandler.saveQuestionsOrder(questions,startPos) for i=1,#questions do local q = [[INSERT INTO question\_order VALUES (NULL,]] .. questions[i].id\_question .. [[,]] .. (startPos+i) ..[[,1);]] db:exec( q ) end end function dbHandler.updateChallengeOrder(questions) for i=1,#questions do local q = [[UPDATE challenge SET position=]] .. questions[i].position .. [[where id\_challenge=]] .. questions[i].id\_challenge ..[[;]] db:exec( q ) end end
this is all what I’m doing while animation run on start, how can I solve the problem