dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [F8] My attempt at an XML score table failed horribly. Tips?

Hybrid View

  1. #1
    Senior Member
    Join Date
    Aug 2006
    Posts
    110

    [F8] My attempt at an XML score table failed horribly. Tips?

    After discussing how to make a high score table with other methods, you guys kept on referring me to use XML to do it. So, I took some time and looked at using XML in Flash.

    • I am able to create an XML object with all the elements that I want in it somewhat properly, using things like appendChild() and createElement().
      So, I had the necessary tools to make simple tags such as <name> and <score> and whatever I needed.
    • I am able to find values in my Nodes with relative ease, and display the information that I want in Flash.
    • I traced my XML Object and see that it was in fact exactly how I wanted it.
    • Through the help pages in Flash, I got the idea that I was supposed to use send() to send my data to somewhere to process the information (in my case, a .php file). My .php file in turn processes the data.


    But, here's where my problems started:

    For some reason, send() was not working when I tested my .php file in a browser, so I reverted to using getURL(), which seemed to work in some test runs. However, the test runs only worked when I used the HTTP GET protocol. It never worked when I tried using POST. As you know, making a high score table using the GET protocol isn't very smart.

    I somehow got the POST protocol to work, even though I was sure the solution was exactly what I tried in the first place. (this may be because of my browser's cache, I'm not sure).



    In the end, this is what I did:
    • Output the tags that I wanted to an XML file with the hopes of organizing it the way I want later on (which would be easy for me)
    • Could not use send() for some reason, and decided to use getURL()


    ...and as you can see, I was probably doing all the wrong things trying to do this. Basically, I feel that the way I did it was probably extremely sloppy. Can anybody help point me in the proper direction as to what to do? I feel like doing this all from scratch again.

    I am willing to post parts of my code if anybody's willing to help tell me what I did wrong here and there.
    Last edited by The Chow; 01-25-2007 at 12:40 AM.
    "I dreamt that I was a 'Genie-Firefighter'! I fought fires ...and granted wishes!"

  2. #2
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    I wanted to upload vars from a text file years ago with flash. It worked well. Then I learned xml and the whole way I thought of flash changed.

    you want to use sendAndload() nothing else will do.

    This is what I recommend you do:

    Turn the name and score into a element
    (i like attributes so I make the element look like this)

    <node id="name" points="100"/>

    add that element to an xml var that has uploaded the xml file that has all the scores.

    then send the edited xml file to php to rewrite the file with

    fwrite($handel,$message);

    The fun part is that you take the xml file that was loaded in flash and push it into an array than use the sort function to put the array in order than use a for statement to output it all.

    I assume that you understand php and the action script to amend a xml file.
    Last edited by zervell; 01-25-2007 at 10:21 AM.
    CEO OF

  3. #3
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,796
    You don't even need XML in order to use SendAndLoad(). (So why complicate it??)

  4. #4
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    complicated? not at all. Keeping in an xml format will save you time and keep the file in an organized fashion. Also xml is super versatile. He could use css to display the xml with style out of flash if he wanted.

    why would you want to use send? wouldn't that open a new window or us the one your in? SendAndLoad posts the information to the sever side file and then the results can be sent to a var if you want. Yes you are right about not needing SendAndLoad but this is were it best serves its purpose.

    Is it complicated to you because you don't understand SendAndLoad or the benefits of xml?
    CEO OF

  5. #5
    Senior Member
    Join Date
    Aug 2006
    Posts
    110
    Thanks, zervell.

    I'll show what I did in my .php, and what I did in my Flash animation:

    The php:
    code:
    $file = "writer.xml";
    $written = "";
    $open = fopen($file, "a");

    // note: I have both GET and POST protocols here to so I
    // can use GET to see what information is being sent.
    // I only intend to keep the POST version
    if ($_GET) {
    $written = $_GET["myXML"];
    }
    if ($_POST) {
    $written = $_POST["myXML"];
    }

    fwrite($open, $written);
    fclose($open);



    I have the following problem with my php:
    • I cannot seem to concatenate stuff into my $written variable (namely a "\n" after each input), unless I'm simply doing it wrong. It comes out as "0" whenever I try. What am I supposed to use?




    The ActionScript (note I didn't make the elements part of the attributes yet like in your suggestion):
    code:
    var myXML:XML = new XML();

    myXML.appendChild(myXML.createElement("name"));
    myXML.childNodes[0].appendChild(myXML.createTextNode("SomeName"));
    myXML.appendChild(myXML.createElement("score"));
    myXML.childNodes[1].appendChild(myXML.createTextNode("1000"));

    myXML.contentType = "text/xml";
    // myXML.send("xml_writer.php", "_self", "POST"); // the Help files directed me to this function for some reason.
    // getURL("xml_writer.php", "_self", "GET"); // this was just to test with to see what information was getting sent
    getURL("xml_writer.php", "_self", "POST"); // this one worked after a while.



    I have the following problems with my ActionScript:
    • createTextNode() always requires a Text input. How will I be able to put my "score" variable in that spot, if its type is Number? (this might be solved by making the tag the way you described, I don't know)
    • each one of the methods I used near the end always seems to refresh the page after it's run, probably due to the second parameter in it. I want the animation to be smooth and continuous after I run through this code. How would I solve this?
    • Also, would you recommend me to use anything other than send() or getURL()?
    "I dreamt that I was a 'Genie-Firefighter'! I fought fires ...and granted wishes!"

  6. #6
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    lucky for you I have just finished the Beta 2.1 of my xmlloader lite. It works, but you'll need to finish it.

    [+] it needs to check if the input text is empty
    [+] it needs a load bar for the xml file

    those are the 2 things i need to finish and I can do it in 30 sec. but i'm being layze.

    I explain the code. so have fun.
    action script

    don't get scared of the code, 1/3 is comments and another 1/3 is for the scrolling code.
    Code:
    /*
    suger cookie game 
    Xmlloader Lite beta 2.1
    by waarith abdul-majeed
    2007
    *****************************************
    please keep suger cookie game logo or give
    credit. Thank you and enjoy.
    
    */
    
    stop();
    //gets todays date
    var today_date:Date = new Date();
    var date_str:String = (today_date.getFullYear()+"/"+(today_date.getMonth()+1)+"/"+today_date.getDate());
    //'i' is used to force flash to load xml
    var i:Number = int(Math.random()*10000);
    //'stringer' is what will be used to hold the array
    var stringer:String = "";
    //'output' is the array that will hold the info from the xml file 
    var output:Array = new Array();
    //'xml' is the var that will hold the xml
    var xml = new XML();
    //'wanttoscroll' is the boolean that will allow/disalow action_scroller() to run
    wanttoscroll = false;
    /*'reciver' takes the return values from the php if you have any. If you don't have return values you still need this var*/
    var reciver:LoadVars = new LoadVars();
    /*'reciver.read' is how the php file can send the var; in the php file the statment print "read=".$message.""; will give reciver.read a value*/
    reciver.read = "";
    //shortcuts to "\n" and "\t"
    edl = "\n";
    tab = "\t";
    //'submitapi' will only come up if the player 'played' the game
    played = false;
    if (played) {
    	//creates 'submitapi'
    	attachMovie("submitapi", "submitapi", getNextHighestDepth(), {_x:(Stage.width/2), _y:(Stage.height/2)});
    }
    
    //this will make the xml var look past returns and spaces in the XML file; very important
    xml.ignoreWhite = true;
    //loads the XML file to the xml var
    xml.load("http://zervell.phpnet.us/xmlloader.xml?"+i);
    //test to see if loaded
    xml.onLoad = function(success:Boolean) {
    	if (success) {
    		//this function dose the xml to array
    		dueprocess();
    	} else {
    		//the text that holds the socores will display "error" if XML file is a no show
    		stringer = "error";
    	}
    };
    onEnterFrame = function () {
    	//explaned already (EA)
    	if (wanttoscroll) {
    		//'action_scroller' function takes 
    		action_scroller();
    	}
    };
    submitapi.b1.onPress = function() {
    	//**NOTE these submitapi.cm.text are the input text from submitapi 
    
    	//just something I learned form someelse I don't know why I use it I just do
    	xml.contentType = "text/xml";
    	//this will add <node/> to the xml var you have 
    	xml.firstChild.appendChild(xml.createElement("node"));
    	//xfacts is a shortcut to xml.firstChild.lastChild.attributes
    	xfacts = xml.firstChild.lastChild.attributes;
    	//now my element looks like this <node date"00/00/00"/>
    	xfacts.date = date_str;
    	//now my element looks like this <node date="00/00/00" comments="blah blah"/>
    	xfacts.comments = submitapi.cm.text;
    	//now my element looks like this <node date="00/00/00" comments="blah blah" score="1000"/>
    	//**NOTE the "int(Math.random()*10000)" should be the var that holds the score
    	xfacts.score = sc.text=int(Math.random()*10000);
    	//now my element looks like this <node date="00/00/00" comments="blah blah" score="1000" id="name" / >
    	xfacts.id = submitapi.id.text;
    	//now you altered xml file is "POSTED" to score.php
    	xml.sendAndLoad("http://zervell.phpnet.us/score.php", reciver);
    	//submitapi is now invisible
    	this._parent._visible = false;
    };
    //**NOTE you don't need to give the php file a value
    reciver.onLoad = function() {
    	//read value comes
    	trace("read="+this.read);
    	//loads the edited XML file
    	xml.load("http://zervell.phpnet.us/xmlloader.xml?"+i);
    	//EA
    	dueprocess();
    	//'postit' pulls the output array into 'stringer' 
    	postit();
    };
    
    
    function dueprocess() {
    // = []; clears the array of any value 
    	output = [];
    	//xml.firstChild.childNodes.length is the number of high scores
    	for (var i:Number = 0; i<xml.firstChild.childNodes.length; i++) {
    		//atr shortcut to xml.firstChild.childNodes[i].attributes
    		var atr = xml.firstChild.childNodes[i].attributes;
    		//adds the values score,date,id,and comments to output array
    		//**NOTE the score must be set to a Number because, it is a string be default
    		output.push({score:Number(atr.score), date:atr.date, id:atr.id, comments:atr.comments});
    	}
    	output.sortOn("score", 18);
    	//EA
    	postit();
    	return 0;
    }
    function postit() {
    	//clears stringer string
    	stringer = "";
    	//xml.firstChild.childNodes.length is the number of high scores
    	for (i=0; i<xml.firstChild.childNodes.length; i++) {
    		//shortcuts to output array
    		var sco = output[i].score;
    		var idn = output[i].id;
    		var dte = output[i].date;
    		var cmt = output[i].comments;
    		
    		//stringer gets concatenated to it's self every time the loop is called
    		//'bolcol_it' is a function that colors the text and makes it bold
    		
    		stringer = stringer+bolcol_it(i+1+".", "0000000")+tab+bolcol_it(idn, "FF00000")+edl+bolcol_it("Score: ", "0000000")+bolcol_it(sco, "FF00000")+edl+cmt+edl+bolcol_it(dte, "FF00000")+edl+edl;
    	}
    	return 0;
    }
    //EA
    function bolcol_it(string, color) {
    	return "<b><font color=\'#"+color+"\'>"+string+"</font></b>";
    }
    //The rest of the code deals with scrolling and the pop up tool tip
    onMouseDown = function () {
    	wanttoscroll = true;
    	removeMovieClip(messagerx);
    };
    onMouseUp = function () {
    	wanttoscroll = false;
    	scroller._visible = false;
    };
    messager("Press and hold mouse button down to scroll");
    function messager(tex) {
    	attachMovie("messager", "messagerx", getNextHighestDepth(), {_x:(Stage.width/2), _y:(Stage.height/2)});
    	messagerx.messa = tex;
    }
    action_scroller = function () {
    	if ((_ymouse<outer._y+(outer._height)) && (_ymouse>outer._y)) {
    		var scrollp_n;
    		scroller._visible = true;
    		outer.scroll += scrollp_n=int((_ymouse-(outer._y+(outer._height/2)))/20);
    		scroller._x = _xmouse+20;
    		scroller._y = _ymouse;
    		if (scrollp_n>0) {
    			scroller._yscale = -100;
    		} else if (scrollp_n<0) {
    			scroller._yscale = 100;
    		}
    	}
    };
    php file

    [+]NOTE: the php can be improved and I going to do that in the final versoin of xmlloader.


    PHP Code:
    <?php

    $file 
    fopen("xmlloader.xml""w+") or die("Can't open XML file");

    $xmlString $HTTP_RAW_POST_DATA

    if(!
    fwrite($file$xmlString)){

        print 
    "read=Error writing to XML-file";

    }

    print 
    $xmlString."\n";

    fclose($file);

    print 
    "read=done";

    ?>
    [+]NOTE:the XML file must start off like this.

    Code:
    <?xml version="1.0"?><scores></scores>

    open up your text editor and copy and paste php code in it and save as '.php' and the same for xml file but, save as '.xml'. (take the ' 's off)
    you already know this but others may want to try.


    right now the xmlloader lite is not ready for the public use so I will only give this file to you.

    I PMed the password to you

    xmlloader lite.zip
    Last edited by zervell; 01-25-2007 at 03:40 PM.
    CEO OF

  7. #7
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    Quote Originally Posted by zervell

    php file

    [+]NOTE: the php can be improved and I going to do that in the final versoin of xmlloader.
    "Ray Beez file contents will get out of sync". What are you talking about? the worse that could happen someone's score will be over writen(highly unlikely). Ray Beez if you understood my code you would know that the php code dosen't put the content in order, flash dose. the xml file just holds the content.

    re-write? again what are you talking about fopen("xmlloader.xml", "w+") truncates the file to 0 aka deletes the content. This is the second time you just started yapping about nothing. I use facts when I help people not BS. I don't post if i don't grasp the idea of whats going on. I think you should use the same principal.

    All the step that you have discribed as being the process of how my code writes to the xml file is out of wack. Maybe you code in AS4 or something because your not making any cents.
    Last edited by zervell; 01-25-2007 at 06:01 PM.
    CEO OF

  8. #8
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,796
    You're proposing opening an XML file, changing it, sending it back to the server to re-write the file? Dangerous! When lots of people are playing the file contents will get out of sync.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center