Quiz framework based on score. Help, por favor.

Hi!

I am creating a type of questionare to help decide the outcome of a situation. My original idea was to make a flowchart between lua files using Director but I have discovered that this process leads to highly inaccurate answers. My solution to this is to use a score system to determine the outcome. My problem, here, is where to start.

I am using Director first off, and have a series of 8 lua files, each one contains a separate question and about 5 usable buttons. Each button takes you to the next lua file with the next question, but each button is also worth a different amount. I want the user to answer the 8 questions and come out on the results page with a score based on which buttons they hit.

I thought it would be as simple as creating a global variable of Score = 0 at the menu and adding Score = Score + 1 to the button touch event, this sadly does not work.

I get this error when I touch the button.

A6.lua:26: attempt to perform arithmetic on global 'Score' (a nil value) stack traceback:
Since this is obviously not working my way, how would any of you go about doing this? sorry if my problem isnt as coherent as it should be. And, also if I get this to work Ill make a demo quiz framework and put it into the Code Exchange. [import]uid: 10355 topic_id: 12113 reply_id: 312113[/import]

Try accessing the global variable on your other screens like this:

_G.Score = _G.Score + 1

See if that works.

Also, just a note about structuring your app – if you have 8 questions do you really need 8 different files? How about 1 file/screen that loops through a list of 8 questions to ask?

This came up recently in a conversation, it was about a game with 50 levels having a different Lua file for each level. To some people that seems reasonable, but unless there’s something weird going on you should be able to use 1 screen file and 50 blocks of data.

But that’s a different topic for a different thread. :slight_smile:

Jay

[import]uid: 9440 topic_id: 12113 reply_id: 44111[/import]

As said above, _G. should clear things up when using director.

Coincidentally I sell a template on Techority that does this exact thing; it can make for some pretty fun little apps :slight_smile:

Peach [import]uid: 52491 topic_id: 12113 reply_id: 44202[/import]

Oh wow that worked perfectly! Thank you guys. And, Peach, I took a look at your template and it looks extremely well put together. If only I had known about that earlier D:
I have one more question though, I am trying to make it where the background changes based on the score but the route i am taking is not working…

This is the function I am working with:

  
 local function bgChange ( event )  
 if \_G.Score \> 15 and \_G.Sore \< 10 then   
 bG6.isVisible = true  
  
 end  
 end  
  

I know it has something to do with this line if \_G.Score \> 15 and \_G.Sore \< 10 then and what ever combination of operators I use ( ==, >=, <=, ) nothing works.

Any suggestions?

[import]uid: 10355 topic_id: 12113 reply_id: 44606[/import]

I’m not sure if you copied and pasted that code, but you have a typo:

_G.Sore should be _G.Score

That could be the problem. [import]uid: 8444 topic_id: 12113 reply_id: 44615[/import]

Heh, as firemaplegames said, it seems you have a typo if that is a copy and paste :wink: [import]uid: 52491 topic_id: 12113 reply_id: 44651[/import]

Opps! I fixed it, but its still not working! Grr. [import]uid: 10355 topic_id: 12113 reply_id: 44691[/import]

What problem are you getting now? What’s happening on screen? Any errors?

I’m sure we can help you fix it :slight_smile: [import]uid: 52491 topic_id: 12113 reply_id: 44776[/import]

Ahh thank you for your willingness to help :slight_smile: I really appreciate it.

And thats the problem, im not receiving any errors. I have the Global score set up working perfectly. Although my problem is creating a function that changes the background image based on the global score.

  
local function bgChange ( event )  
 if \_G.Score \> 15 and \_G.Score \< 10 then   
 bG6.isVisible = true  
  
 end  
 end  
  
Runtime:addEventListener("enterFrame", bgChange)  
  
  

Thats what I have written here… except that it does nothing… it doesn’t even create an error in terminal. :confused: [import]uid: 10355 topic_id: 12113 reply_id: 45022[/import]

OK, I think we’re looking at this wrong. Try something more like this;

[lua]function setBG (event)
if _G.score > 15 then
local background = display.newImage (“bg1.png”)
background.y = 203
localGroup:insert(background)
else
local background = display.newImage (“bg2.png”)
background.y = 203
localGroup:insert(background)
end
end
setBG()[/lua]

That is supposing you are using director.

Your above code also has an issue, you say;
[lua]if _G.Score > 15 and _G.Score < 10 then[/lua]

Greater than 15 AND less than 10.

No such number exists :wink:

Take a look at it, and the above code; see if you can get it with that, if not - my willingness to help will still be here and I’m sure we can solve it.

Peach [import]uid: 52491 topic_id: 12113 reply_id: 45192[/import]

Sorry, ignore the background.y - that was copied and changed slightly from an app I used it in where I needed to set the Y :wink: [import]uid: 52491 topic_id: 12113 reply_id: 45193[/import]

Ahh thank you! This works perfect! The thing is that I have 7 different backgrounds to change between, so would this formula still hold true? I’ll be really busy the next few days so ill be strapped to test it out, but the second I get back ill fiddle around with adding 5 more operators :stuck_out_tongue:
Thank you again!! [import]uid: 10355 topic_id: 12113 reply_id: 45739[/import]