This article outlines the use of XML parser
with the help of a simple Quiz game. The first is to design the UI for the
game for the Quiz. It can be divided mainly in the four types of screens :
the main screen, the main menu screen, the quiz screen and the result screen.
The main screen consists of the Quiz name
and the start button. The main menu screen consists of various categories for
the Quiz questions and a next button. The various categories are provided in
form of radio buttons. The quiz screen consists of the text box containing the quiz,
four answer options (as radio buttons) and a next button to move onto next
page. The final screen is the result screen which consists of a text box
displaying your score. This is just the basic skeleton; other ornamentation of
UI can be done as per your choice. And then we can kick start the coding part.
The startmovie method is used declaring the
global variable, functions used for initialization and other cleanup operations
(which will be clearer as we proceed).
global objxml,objfileio,errorcode,parsedlist,errorString,file_data,points,i,iter
global flag, answer
on startmovie me
points=0
i=1
flag=false
answer=""
end
-- checking the correct ans and performing the cleanup operations
on checkanswer(i) me
if(flag=true) then
correct_ans=objxml.child[1].child[i-1].child[5].child[1].text
if(answer=correct_ans) then
points=points+1
alert("correct answer!!!!!!!")
flag=false
end if
end if
channel(10).removeScriptedSprite()
end
After clicking on the next button in the main
screen, the main menu screen appears. The main tasks to be performed are :
loading of xml files , parsing the selected xml file which is based on the
radio button selected by the user from the main menu for the Quiz category. Here
we have four categories geography, history, science and general corresponding
to which we have four xml files . Based on the option selected by the user, the objfileio object (fileio type) is used to read from the xml file. Then, the
parsing of xml file is done using the objxml object (xmlparser type).
on mouseUp me
-- Accessing the xml data file
objxml=new xtra("xmlparser")
objfileio=new xtra("fileio")
--Retrieving the xml files from the appropriate path
if (sprite("menu1").selected=true) then
objfileio.openfile("c:\\geography.xml",1)
else if (sprite("menu2").selected=true) then
objfileio.openfile("c:\\history.xml",1)
else if (sprite("menu3").selected=true) then
objfileio.openfile("c:\\science.xml",1)
else if (sprite("menu4").selected=true) then
objfileio.openfile("c:\\general.xml",1)
end if
--Reading the file and parsing it using xml parser
file_data=objfileio.readfile()
objxml.ignorewhitespace(true)
errorcode=objxml.parsestring(file_data)
-- Retrieve the error string in case of error
errorString=objxml.getError()
parsedlist=objxml.makelist()
if (voidP(errorString)) then
-- Initializing the iter to no. of questions
iter=(objxml.child[1].child.count)+1
else
alert "Sorry,could not retrieve data" & errorString
-- Exit from the handler
exit
end if
_movie.go("second")
_movie.sendSprite(7,#mouseUp, me)
end
When we click on the next button in the main
menu screen, control goes to the quiz screen. The loading of the questions is done. The text and
captions for the radio buttons for each of the quiz questions are picked up
from the parsedlist. Whether the question contains any image or
not is identified by tag type i.e. “image_quest” in the “general.xml” file. The checkanswer function will be iterated based on the number of
questions in the file which is taken care of by the variable i.
on mouseUp me
checkanswer(i)
if i<iter then
-- Handling the question with images
if (objxml.child[1].child[i].name="image_quest") then
img=objxml.child[1].child[i].child[6].attributevalue[1]
_movie.newMember("bitmap").importFileInto(img)
channel(10).makeScriptedSprite(member("abc"), point(230,100))
sprite("txtdisplay").visible=false
sprite("image_text").visible=true
sprite("image_text").text=objxml.child[1].child[i].attributevalue[1]
sprite("radio_b1").label=objxml.child[1].child[i].child[1].child[1].text
sprite("radio_b2").label=objxml.child[1].child[i].child[2].child[1].text
sprite("radio_b3").label=objxml.child[1].child[i].child[3].child[1].text
sprite("radio_b4").label=objxml.child[1].child[i].child[4].child[1].text
updateStage()
else
-- Handling the questions without images
sprite("image_text").visible=false
sprite("txtdisplay").visible=true
sprite("txtdisplay").text=objxml.child[1].child[i].attributevalue[1]
sprite("radio_b1").label=objxml.child[1].child[i].child[1].child[1].text
sprite("radio_b2").label=objxml.child[1].child[i].child[2].child[1].text
sprite("radio_b3").label=objxml.child[1].child[i].child[3].child[1].text
sprite("radio_b4").label=objxml.child[1].child[i].child[4].child[1].text
end if
updateStage()
-- Unselecting the radio buttons
sprite("radio_b1").selected=false
sprite("radio_b2").selected=false
sprite("radio_b3").selected=false
sprite("radio_b4").selected=false
else if i>=iter then
-- Going to the end marker and displaying the results
_movie.go("end")
member("result").text="Your score is: " & points
i=0
end if
i=i+1
end
The click method is
called when user selects any of the radio buttons in the quiz screen. All the
other radio buttons are de-selected to avoid the multiple button selection. The
caption of the radio button selected by the user is stored in global variable answer for verification of the answer later on.
on click me
sprite("radio_b1").selected=false
sprite("radio_b2").selected=false
sprite("radio_b3").selected=false
sprite("radio_b4").selected=false
sprite(me.spritenum).selected=true
flag=true
answer=sprite(me.spriteNum).label
end
The checkanswer function in the startmovie
method compares the actual answer with the value in the answer variable and if the answer matches then dialog
containing the “correct answer” text pops up. After all the Quiz questions in the parsedlist are displayed, control goes to result screen. The score
is displayed in the text field of the result screen.
Feedback:
If you have
any questions or comments concerning this article, please send a message to
poonam308y@yahoo.com
|