How can i make a function which chooses specific category of questions ?

Hi guys, so i’ve tried creating a module which chooses a category based on what button is pressed.

For example “Category1” was pressed, in that category1 it displays/asked questions about “internet”, and if a user presses the other button like “Category2” it displays questions about “Food”, and another button which is “Category3” is pressed questions about “Life” is displayed/asked. Of course only 1 button can be pressed and the user must/have to finish the category he went to.

Here is the current code i tried:

local function gameCatHandler(event)
local phase = event.phase

if myData.gameCat == “category1” then
– print(event.target.id)
print(“Category”,myData.categories[event.target.id].ques_no)
myData.gameCat = event.target.id
myData.setBg(1,2)
storyboard.gotoScene(“category1”, {
effect = “fade”,
time = 250,
})

else if myData.gameCat == “category2” then
print(“Category”,myData.categories[event.target.id].ques_no)
myData.gameCat = event.target.id
myData.setBg(1,2)
storyboard.gotoScene(“category2”, {
effect = “fade”,
time = 250,
})

else

print(“Category”,myData.categories[event.target.id].ques_no)
myData.gameCat = event.target.id
myData.setBg(1,2)
storyboard.gotoScene(“category3”, {
effect = “fade”,
time = 250,
})
end
end

return true
end
end

Buttons Code

local screenGroup = self.view
local bgGroup = display.newGroup()
local buttonGroup = display.newGroup()
screenGroup:insert(bgGroup)
screenGroup:insert(buttonGroup)

background = display.newImageRect ( “Images/Bg.png”,565 , _H )
background.x = _W*.5 ; background.y = _H*.5
bgGroup:insert(background)

local btnData = {
{id=“category1”,x=180,y= 25,},
{id=“category2”,x=348,y=160},
{id=“category3”,x=265,y=225},
}

for i = 1, 3 do

local btn = widget.newButton( {
id = btnData[i].id,
x = btnData[i].x,
y = btnData[i].y,
width = 50,
height = 50,
– font = native.systemFont,
– fontSize = 18,
– labelColor = {
– default = {0,0,0},
– over = {255,255,255}
– },
defaultFile = “buttons/”… string.lower(btnData[i].id)… “Press.png”,
overFile = “buttons/”… string.lower(btnData[i].id)… “Pressed.png”,
onRelease = gameCatHandler,
} )

btn.anchorX = 0
btn.anchorY = 0

buttonGroup:insert( btn)

When i use the code for the event handler it doesn’t call properly  like for example i pressed the category1 button, the game displays category3 rather than 1. What is the proper code for it?

All questions these categories have is only on 1 Database(SQL), or is it easier to have each categories have an individual database?

Firstly, you seem to be making a separate scene for all of the categories. Unless these scenes will look and behave very differently from each other, you only need one scene, which displays the right questions depending on the category selected, using the category field to select the relevant questions from the SQL database each time the scene loads and put them in a lua table.

The category can either be passed as a scene parameter, or held in a global variable. It looks like you are already storing the category selected in myData.gameCat.

Your code doesn’t work because you are checking myData.gameCat before it has been set to the value of event.target.id. So it will always be blank and default to category3.

[lua]

local function gameCatHandler(event)

      local phase = event.phase

      local obj = event.target

      print(“Category”,myData.categories[obj.id] .q ues_no)

           

      myData.gameCat = obj.id

      myData.setBg( 1 , 2 )

      storyboard.gotoScene(“questions”, {

            effect = “fade”,

            time = 250 ,

      })

return true

end

[/lua]

Thanks for the code and reply. But what if each of my scenes behave differently like for example, Category 3 has its own module, and has its own specific background, designs, buttons, etc. and the same goes for category 1 & 2.

To make it more clearer, i want each of their button in a menu module where those buttons are visible and if of them is pressed they go to their specific module and inside those are their own set of questions, buttons, functions,etc.

In that case you just do: 

[lua]

  1. storyboard.gotoScene(obj.id, {
  2.             effect = “fade”,
  3.             time = 250,
  4.       })

[/lua]

 

As long as the id is the same as the scene name, it will load the appropriate scene.

 

If not set up a table:

 

[lua]

 

local sceneNames = {category1 = “myFirstSceneName”, category2 = “mySecondSceneName”, category3 = “anotherSceneName”}

  1. storyboard.gotoScene(sceneNames[obj.id], {
  2.             effect = “fade”,
  3.             time = 250,
  4.       })

 

 

[/lua]

 

This saves you having to do loads of if…then…else statements for each category.

The general issue, which has come up several times this week is you cannot set table attributes in the widget creation code:

local myButton = widget.newButton({

       myAttribute = “Fred”,

       … – rest of the button code

})

myAttribute is not something the newButton’s create code knows about so it gets thrown away.  The only thing you can put inside the curly braces are things the widget expects.  If you want the button to carry additional attributes you have to do them outside of the initializer:

local myButton = widget.newButton({

       … – rest of the button code

})

myButton.myAttribute = “Fred”,

Rob

Thanks a alot for the replies. Finally solved it. THANK YOU

Firstly, you seem to be making a separate scene for all of the categories. Unless these scenes will look and behave very differently from each other, you only need one scene, which displays the right questions depending on the category selected, using the category field to select the relevant questions from the SQL database each time the scene loads and put them in a lua table.

The category can either be passed as a scene parameter, or held in a global variable. It looks like you are already storing the category selected in myData.gameCat.

Your code doesn’t work because you are checking myData.gameCat before it has been set to the value of event.target.id. So it will always be blank and default to category3.

[lua]

local function gameCatHandler(event)

      local phase = event.phase

      local obj = event.target

      print(“Category”,myData.categories[obj.id] .q ues_no)

           

      myData.gameCat = obj.id

      myData.setBg( 1 , 2 )

      storyboard.gotoScene(“questions”, {

            effect = “fade”,

            time = 250 ,

      })

return true

end

[/lua]

Thanks for the code and reply. But what if each of my scenes behave differently like for example, Category 3 has its own module, and has its own specific background, designs, buttons, etc. and the same goes for category 1 & 2.

To make it more clearer, i want each of their button in a menu module where those buttons are visible and if of them is pressed they go to their specific module and inside those are their own set of questions, buttons, functions,etc.

In that case you just do: 

[lua]

  1. storyboard.gotoScene(obj.id, {
  2.             effect = “fade”,
  3.             time = 250,
  4.       })

[/lua]

 

As long as the id is the same as the scene name, it will load the appropriate scene.

 

If not set up a table:

 

[lua]

 

local sceneNames = {category1 = “myFirstSceneName”, category2 = “mySecondSceneName”, category3 = “anotherSceneName”}

  1. storyboard.gotoScene(sceneNames[obj.id], {
  2.             effect = “fade”,
  3.             time = 250,
  4.       })

 

 

[/lua]

 

This saves you having to do loads of if…then…else statements for each category.

The general issue, which has come up several times this week is you cannot set table attributes in the widget creation code:

local myButton = widget.newButton({

       myAttribute = “Fred”,

       … – rest of the button code

})

myAttribute is not something the newButton’s create code knows about so it gets thrown away.  The only thing you can put inside the curly braces are things the widget expects.  If you want the button to carry additional attributes you have to do them outside of the initializer:

local myButton = widget.newButton({

       … – rest of the button code

})

myButton.myAttribute = “Fred”,

Rob

Thanks a alot for the replies. Finally solved it. THANK YOU