I can import the data from my xml file (ImportXML.hx) but I do not put the imported content in my leMotDuJeu variable that is in the Main class. When I call the public variable monMot(ImportXML.hx) in my Main class, it is null. How to fix that?
my ImportXML.hx class
package;
import openfl.display.*;
import openfl.events.*;
import openfl.net.*;
import xpath.*;
import xpath.xml.*;
class ImportXML
{
var xml:Xml;
var xmlLoader:URLLoader = new URLLoader();
var xpathXml:XPathHxXml;
public var mot:XPath;
public var monMot:String;
public var finChargement:Bool = false;
public function new()
{
xmlLoader.load(new URLRequest("http://localhost/test.xml"));
xmlLoader.addEventListener(Event.COMPLETE, onComplete);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
}
public function onComplete(e:Event)
{
xml = Xml.parse(xmlLoader.data);
xmlLoader.removeEventListener(Event.COMPLETE, onComplete);
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
xpathXml = XPathHxXml.wrapNode(xml);
mot = new XPath("/Anagrammes/mot");
var result = mot.selectNodes(xpathXml);
for (i in result)
{
monMot = i.getStringValue();
}
}
function onError(err:IOErrorEvent)
{
trace("Erreur!! Veuillez vous connecter a internet svp");
}
}
and my xml file is test.xml which is in localhost/test.xml (wampserver)
<?xml version="1.0" encoding="UTF-8"?>
<Anagrammes version= "1.0">
<mot>MONSIEUR</mot>
<motAnagramme1>VISA</motAnagramme1>
<motAnagramme2>VAIS</motAnagramme2>
</Anagrammes>
my main classe is Main.hx
package;
import openfl.display.*;
import openfl.text.*;
import openfl.events.*;
import openfl.Lib;
import ImportXML;
/**
* ...
* @author ILAMBO
*/
class Main extends Sprite
{
public static var laLettreSeletionnee:String = "";
public static var laPositionDeLaLettreSelectionneeEnX:Float;
public static var leContenneurDeLaLettreSelectionnee:openfl.display.DisplayObject = null;
private var alphabetDesLettres:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private var lesLettresProposees:Array<String>;
private var lesLettresDansLaMainDuJoueur:Array<String>;
private var lesLettresJouees:Array<String>;
private var laMainDuJoueur:Sprite;
private var laPartieJeu:Sprite;
private var lesMotsValidesAnagrammesAuMotDuJeu:Array<String>;
public static var leMotDuJeu:String;
var importations:ImportXML;
var importationsXpath:XmlFast;
//public static var motPropose:String = "";
public function new()
{
super();
//Preparation de la main du joueur
lesLettresDansLaMainDuJoueur = new Array<String>();
laMainDuJoueur = new Sprite();
laMainDuJoueur.x = 165; laMainDuJoueur.y = 280;
laMainDuJoueur.width = 310; laMainDuJoueur.height = 30;
laMainDuJoueur.graphics.beginFill(0x0ff);
laMainDuJoueur.graphics.drawRect(0, 0, 310, 30);
laMainDuJoueur.name = "main";
this.addChild(laMainDuJoueur);
//Preparation de la partie jeu
lesLettresJouees = new Array<String>();
laPartieJeu = new Sprite();
laPartieJeu.x = 260; laPartieJeu.y = 150;
laPartieJeu.width = 120;
laPartieJeu.height = 30;
laPartieJeu.graphics.beginFill(0xFFF66F);
laPartieJeu.graphics.drawRect(0, 0, 120, 30);
laPartieJeu.name = "jeu";
this.addChild(laPartieJeu);
//Preparation du jeu
lesMotsValidesAnagrammesAuMotDuJeu = new Array<String>();
leMotDuJeu = ExtraireUnMotDuFichier();
if (leMotDuJeu == "") leMotDuJeu = "VISA";
//Proposition des lettres
GenererDesLettres(leMotDuJeu);
//Ajout des evenements du jeu
this.stage.addEventListener(MouseEvent.CLICK, JouerAvecUneLettre);
//stage.addEventListener(Event.ENTER_FRAME, enterFrame);
}
/*private function enterFrame(e:Event)
{
if (Main.leMotDuJeu != null) {
trace(Main.leMotDuJeu);
stage.removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
*/
private function JouerAvecUneLettre(e:MouseEvent)
{
if (leContenneurDeLaLettreSelectionnee != null)
{
if (leContenneurDeLaLettreSelectionnee.name == "main")
{
var laLettre:ImageLettre = new ImageLettre(Main.laLettreSeletionnee);
this.DonnerUneLettreAuJeu(laLettre);
lesLettresDansLaMainDuJoueur.remove(Main.laLettreSeletionnee);
lesLettresJouees.push(Main.laLettreSeletionnee);
LibererUneCarte();
RestructurerLesElementsDeLaMain();
}
else if (leContenneurDeLaLettreSelectionnee.name == "jeu")
{
var laLettre:ImageLettre = new ImageLettre(Main.laLettreSeletionnee);
this.DonnerUneLettreAuJoueur(laLettre);
lesLettresJouees.remove(Main.laLettreSeletionnee);
lesLettresDansLaMainDuJoueur.push(Main.laLettreSeletionnee);
ReprendreUneLettre();
RestructurerLesElementsDuJeu();
}
}
leContenneurDeLaLettreSelectionnee = null;
}
private function RestructurerLesElementsDeLaMain()
{
this.laMainDuJoueur.removeChildren(0, laMainDuJoueur.numChildren - 1);
trace("Le joueur a " + lesLettresDansLaMainDuJoueur.length);
for (lettre in lesLettresDansLaMainDuJoueur)
{
var uneLettre:ImageLettre = new ImageLettre(lettre);
DonnerUneLettreAuJoueur(uneLettre);
}
}
private function RestructurerLesElementsDuJeu()
{
this.laPartieJeu.removeChildren(0, laPartieJeu.numChildren - 1);
trace("Le jeu a " + lesLettresJouees.length);
for (lettre in lesLettresJouees)
{
var uneLettre:ImageLettre = new ImageLettre(lettre);
DonnerUneLettreAuJeu(uneLettre);
}
}
private function DonnerUneLettreAuJeu(uneLettre:ImageLettre)
{
uneLettre.y = 0;
uneLettre.x = 30 * this.laPartieJeu.numChildren;
this.laPartieJeu.addChild(uneLettre);
}
private function DonnerUneLettreAuJoueur(uneLettre:ImageLettre)
{
uneLettre.y = 0;
uneLettre.x = 40 * this.laMainDuJoueur.numChildren;
this.laMainDuJoueur.addChild(uneLettre);
}
public function ExtraireUnMotDuFichier() : String
{
return "";
}
private function GenererDesLettres(unMotPropose:String) : Void
{
lesLettresProposees = new Array<String>();
var nosLettres:Array<String> = new Array<String>();
for (i in 0...unMotPropose.length)
{
nosLettres.push(unMotPropose.charAt(i));
}
trace("Nos lettres sont au nombre de " + nosLettres.length);
while (nosLettres.length < 12)
{
var unePosition:Int = Main.GenereUnEntierEntre(0, 25);
nosLettres.push(alphabetDesLettres.charAt(unePosition));
}
while (lesLettresProposees.length < 12)
{
if (nosLettres.length == 1) lesLettresProposees.push(nosLettres.pop());
else
{
var unePosition:Int = Main.GenereUnEntierEntre(0, nosLettres.length - 1);
lesLettresProposees.push(nosLettres.splice(unePosition, 1).pop());
}
}
for (lettre in lesLettresProposees)
{
lesLettresDansLaMainDuJoueur.push(lettre);
var uneLettre:ImageLettre = new ImageLettre(lettre);
this.DonnerUneLettreAuJoueur(uneLettre);
}
}
private static function GenereUnEntierEntre(from:Int, to:Int) : Int
{
return from + Math.floor((to - from + 1) * Math.random());
}
private function LibererUneCarte():Void
{
for (i in 0...laMainDuJoueur.numChildren)
{
var lettre = laMainDuJoueur.getChildAt(i);
if (lettre.x == Main.laPositionDeLaLettreSelectionneeEnX)
{
laMainDuJoueur.removeChildAt(i);
break;
}
}
}
private function ReprendreUneLettre():Void
{
for (i in 0...laPartieJeu.numChildren)
{
var lettre = laPartieJeu.getChildAt(i);
if (lettre.x == Main.laPositionDeLaLettreSelectionneeEnX)
{
laPartieJeu.removeChildAt(i);
break;
}
}
}
I have another class ImageLettre.hx that draws the different squares.
package;
import openfl.display.*;
import openfl.text.*;
import openfl.events.*;
import openfl.Lib;
/**
* ...
* @author ILAMBO
*/
class ImageLettre extends Sprite
{
public var LaLettre:String;
public function new(laLettre:String)
{
super();
var texteToPrint = new TextField();
texteToPrint.visible = true;
texteToPrint.selectable = false;
texteToPrint.text = this.LaLettre = laLettre;
texteToPrint.width = 30;
texteToPrint.height = 30;
texteToPrint.autoSize = TextFieldAutoSize.CENTER;
texteToPrint.textColor = 0xffffff;
this.graphics.beginFill (0x000000, 0.5);
this.graphics.drawRect (0, 0, 30, 30);
this.width = this.height = 30;
this.addChild(texteToPrint);
//Ajout d'events
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent)
{
Main.laLettreSeletionnee = this.LaLettre;
Main.leContenneurDeLaLettreSelectionnee = this.parent;
Main.laPositionDeLaLettreSelectionneeEnX = this.x;
}
}
Thanks for your help