Please help me !! help me create random my choice for math game

This is my code !

module(…, package.seeall)
–display.setStatusBar(display.HiddenStatusBar)
function new()
local addgroup = display.newGroup()
local ui = require(“ui”)
local movieclip = require(“movieclip”)
–local background= display.newImage(“main/mainBg.png”,0,0)
local num1
local num2
local num3
local num4
local picture
local numbertouch
local label1 = display.newText("?", 40, 160, native.systemFont, 50)
local label2 = display.newText("?", 140, 160, native.systemFont, 50)
local sum = display.newText("+", 90, 160, native.systemFont, 50)
local result = display.newText("=", 190, 160, native.systemFont, 50)
local qustion = display.newText("?", 240, 160, native.systemFont, 50)

local choice ={}
choice[1] = display.newText("?", 230, 340, native.systemFont, 50)
choice[2] = display.newText("?", 135, 340, native.systemFont, 50)
choice[3] = display.newText("?", 40, 340, native.systemFont, 50)
local c1
local c2
local c3
function randomNumber()
num1=math.random(1,9)
num2=math.random(1,9)
label1.text=num1
label2.text=num2
local sum1 =num1 + num2
num3=math.random(16,20)
local sum2 = num3+8

num4=math.random(11,15)
local sum3 =num4+8

c1 =sum1
c2 =sum2
c3 =sum3
choice[1].text =c1
choice[2].text =c2
choice[3].text =c3

randomNumber()
return addgroup
end

… thank you so much [import]uid: 67093 topic_id: 12645 reply_id: 312645[/import]

two quick things… what are you trying to achieve and please can you put your code in lua tags [import]uid: 24641 topic_id: 12645 reply_id: 46310[/import]

@tuktoonz, your code should work assuming you’re using a director type wrapper. did you have a specific question? [import]uid: 6787 topic_id: 12645 reply_id: 46336[/import]

It looks like you are trying to recursively call yourself.

In side of:

[lua]function randomNumber()

– you call randomNumber() again:
randomNumber()
return addgroup
end[/lua]

If Lua supports recursive calls, you will never get to the “return addgroup” line since you call randomNumber from within itself. It processes again, and before you can return from it, you call it again. I would think you would eventually get a stack overflow and the app would crash.

And if that is the whole lua file, there is at least one “end” missing. [import]uid: 19626 topic_id: 12645 reply_id: 46342[/import]

@robmiracle, you’re right, looks like there is an end missing, forgot to mention that.

Throwing away the other stuff, this works:

  
local num1  
local num2  
local num3  
local num4  
local picture  
local numbertouch  
  
local label1 = display.newText("?", 40, 160, native.systemFont, 50)  
local label2 = display.newText("?", 140, 160, native.systemFont, 50)  
local sum = display.newText("+", 90, 160, native.systemFont, 50)  
local result = display.newText("=", 190, 160, native.systemFont, 50)  
local qustion = display.newText("?", 240, 160, native.systemFont, 50)  
  
local choice ={}  
choice[1] = display.newText("?", 230, 340, native.systemFont, 50)  
choice[2] = display.newText("?", 135, 340, native.systemFont, 50)  
choice[3] = display.newText("?", 40, 340, native.systemFont, 50)  
local c1  
local c2  
local c3  
  
function randomNumber()  
 num1=math.random(1,9)  
 num2=math.random(1,9)  
 label1.text=num1  
 label2.text=num2  
 local sum1 =num1 + num2  
  
 num3=math.random(16,20)  
 local sum2 = num3+8  
  
 num4=math.random(11,15)  
 local sum3 =num4+8  
  
 c1 =sum1  
 c2 =sum2  
 c3 =sum3  
 choice[1].text =c1  
 choice[2].text =c2  
 choice[3].text =c3  
end  
  
randomNumber()  

So @tuktoonz, you just need to figure out where to go next. [import]uid: 6787 topic_id: 12645 reply_id: 46347[/import]

Also, just in glancing if this function ultimately is intended to return “addgroup” I’m not seeing anywhere that anything gets inserted into that group.

You’ll get there!

–Croisened [import]uid: 48203 topic_id: 12645 reply_id: 46352[/import]

Thanks for all the comments, But now I do it then. Thanks very much :slight_smile: [import]uid: 67093 topic_id: 12645 reply_id: 46375[/import]

Hi tuktoonzz, can you help me to fix the code, because I tried to random the “choice” option (sum1, sum2, sum3) but i can’t figure it out how to make it happen…
My purpose is to create a simple game for kids(sum, subtraction,division and multiply) [import]uid: 64917 topic_id: 12645 reply_id: 47138[/import]

someone can help me please… [import]uid: 64917 topic_id: 12645 reply_id: 47242[/import]

@seaweedbread I think we (the community) doesn’t understand what you need help with. @snarla above provided working code, so what problems are you running into now?

[import]uid: 19626 topic_id: 12645 reply_id: 47296[/import]