Copy link to clipboard
Copied
I have a file called temp.xml saved out on localhost (I'm running wamp for testing), and it has this in it:
<?xml version="1.0" encoding="utf-8"?>
<1>
<SAVEDATA pos="1"/>
</1>
The idea is, that as you get to checkpoints pos="" would be updated. I can't seem to figure out how to actually edit this. The reason SAVEDATA is within <1> is because I also wanted it to somehow assign a unique ID to each player (There's a login system), and when you login, it checks your ID, and continues the game from where you left off.
if you use the xml like i showed, you can use the following to load and edit the pos attribute:
// load it
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,f);
urlLoader.load(new URLRequest("test2.xml"));
var xml:XML;
function f(e:Event):void{
xml = XML(e.target.data);
}
// edit xml: (for example to change player with id="3" to have pos="7", use editXMLF(3,7);
// you don't need to change any code above or below except the parameters you pass when you call editXMLF
funct
...Copy link to clipboard
Copied
do you want to add the SAVEDATA node or edit its pos attribute?
p.s. that xml should have a wrapper node and you should not use a number for a node name. use something like:
<?xml version="1.0" encoding="utf-8"?>
<players>
<player id="1">
<SAVEDATA pos = "1"/>
</player>
</players>
Copy link to clipboard
Copied
I want to edit the pos attribute, and thanks for the heads up.
Copy link to clipboard
Copied
if you use the xml like i showed, you can use the following to load and edit the pos attribute:
// load it
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,f);
urlLoader.load(new URLRequest("test2.xml"));
var xml:XML;
function f(e:Event):void{
xml = XML(e.target.data);
}
// edit xml: (for example to change player with id="3" to have pos="7", use editXMLF(3,7);
// you don't need to change any code above or below except the parameters you pass when you call editXMLF
function editXMLF(playerID:int, newPOS:int):void{
xml.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;
}
Copy link to clipboard
Copied
Wouldn't it be:
xml.players.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;
because player is within players.
Regardless, it isn't working for me.
player.addEventListener(MouseEvent.CLICK, addNumber);
function addNumber(Mouse:Event):void
{
editXMLF(2, 4);
trace("gfrvsg");
}
player is an object on stage, and the trace was simply to make sure I was clicking it.
Copy link to clipboard
Copied
no, it should be exactly as i showed. the wrapper node is assigned to the variable xml.
and what makes you think it's not working? ie, what did you do to test? (hint: use a trace(xml) statement to confirm it's working.)
Copy link to clipboard
Copied
I used a mouse click function on my player and the xml never updated.
I also tried trace(xml), and it returned "null"
Copy link to clipboard
Copied
copy and paste the code you're using.
Copy link to clipboard
Copied
//XML Loading
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,f);
urlLoader.load(new URLRequest("temp.xml"));
var xml:XML;
function addNumber(Mouse:Event):void
{
editXMLF(2, 4);
trace("gfrvsg");
}
function f(e:Event):void{
xml = XML(e.target.data);
}
// edit xml: (for example to change player with id="3" to have pos="7", use editXMLF(3,7);
// you don't need to change any code above or below except the parameters you pass when you call editXMLF
function editXMLF(playerID:int, newPOS:int):void
{
xml.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;
}
trace(xml);
Copy link to clipboard
Copied
1. nothing's calling addNumber. therefore, nothing's calling editXMLF.
2. your trace(xml) should return null because it's executing before, temp.xml completes loading and xml is defined.
p.s. i'm not trying to give you a hard time. but, the code i suggested works. you're not using it correctly. almost certainly, you're trying to use editXMLF before your xml file is defined or, you're trying to edit a player id that doesn't exist in your xml.
Copy link to clipboard
Copied
I had this on the top of my file (after imports and stop();)
player.addEventListener(MouseEvent.CLICK, addNumber);
I just didn't include it.
Now it changes the value in flash, but it's not saving to file.
Copy link to clipboard
Copied
so, as long as you click that after loading is complete (highly likely), the code i suggested should work.
if you also want to save that changed xml to temp.xml, use:
var fRef:FileReference=new FileReference();
function editXMLF(playerID:int, newPOS:int):void{
xml.player.(@id==playerID.toString()).SAVEDATA.@pos = newPOS;
fRef.save(xml,"temp.xml");
}
Copy link to clipboard
Copied
It prompts me to save the file from localhost. I don't want a game that consistantly prompts you everytime the game saves.
Copy link to clipboard
Copied
are you saving the xml file onto a server?
if yes, use the urlloader class and call a server-side script that saves your file.
Copy link to clipboard
Copied
I have it on localhost via WAMP, so it'd be saving to 127.0.0.1/xmlDoc.xml
But I don't have a server sided script for it. I'd assume it should be done in php
Copy link to clipboard
Copied
yes, most servers support php.
if you post your xml variable as xmlVar, you can use:
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
$file = "temp.xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xmlVar);
fclose($fh);
?>
Copy link to clipboard
Copied
I found a way to load via php to MySQL, which seems to work perfectly. I just need to figure out how to save it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now