2014-11-21 51 views
0

我正在嘗試使用AS3中XML文件中的數據創建按鈕。在AS3中使用XML文件創建按鈕?

但是,當我運行我的代碼時,我沒有得到任何錯誤,但沒有任何東西會被加載到我的flash文件中!

我正在使用PHP文件創建XML文件。因此,當我點擊我的Flash文件中的按鈕時,我應該將數據發送到XML(使用PHP生成),然後將數據加載到我的Flash文件中的按鈕形式的XML文件中!

這是我的AS3代碼:

// Set the "y" location on stage where the first btn will live 
var yPlacement:int = 130; 
// Set the "x" location on stage where all btns will line up vertically 
var xPlacement:int = 10; 
// Set the distance each btn should be apart here 
var distance:int = 18; 
// Format the text appearance here 
var format:TextFormat = new TextFormat(); 
format.color = 0x000000; 
format.font = "Verdana"; 
format.size = 11; 
//format.bold = true; 
format.kerning = false; 



getitems.addEventListener(MouseEvent.MOUSE_DOWN, checkLoginL); 




    function checkLoginL (e:Event):void { 

      /* 
      check fields before sending request to php 
      */ 

      if (myText2.text == "") { 

       /* 
       if username or password fields are empty set error messages 
       */ 

       if (myText2.text == "") { 

       myText2.text = "Enter your email"; 

       } 


      } else { 

       /* 
       init function to process login 
       */ 

       processLoginL(); 

      } 

     } 


    function processLoginL():void { 


     var phpVarsL:URLVariables = new URLVariables(); 

      var myXML:XML = new XML(); 
      var XML_URL:String = "php/MYPHPFILE.php"; 
      var myXMLURL:URLRequest = new URLRequest(XML_URL); 
      myXMLURL.method = URLRequestMethod.POST; 
      var myLoader:URLLoader = new URLLoader(myXMLURL); 


      myXMLURL.data = phpVarsL; 


      myLoader.addEventListener("complete", xmlLoaded); 

      phpVarsL.systemCallL = "checkLoginL"; 
      phpVarsL.myText2 = myText2.text; 





function xmlLoaded(event:Event):void { 

    // Place the xml data into the myXML object 
    myXML = XML(myLoader.data); 


    // Initialize and give var name to the new external XMLDocument 
    var xmlDoc:XMLDocument = new XMLDocument(); 
    // Ignore spacing around nodes 
    xmlDoc.ignoreWhite = true; 
    // Define a new name for the loaded XML that is the data in myLoader 
    var menuXML:XML = XML(myLoader.data); 
    // Parse the XML data into a readable format 
    xmlDoc.parseXML(menuXML.toXMLString()); 

    // Set the index number of our loop, increments automatically 
    var i:Number = 0; 
    // Run the "for each" loop to iterate through all of the menu items listed in the external XML file 
    for each (var MenuItem:XML in myXML..MenuItem) { 

    // Access the value of the "pagenum" node in our external XML file 
    var pagenum:String = MenuItem.pagenum.toString(); 
    // Access the value of the "pagetext" node in our external XML file 
    var pagetext:String = MenuItem.pagetext.toString(); 
    // Access the value of the "pagepicture" node in our external XML file 
    var pagepicture:String = MenuItem.pagepicture.toString(); 

    // Just some trace I used while testiing 
    //trace(pagenum); 

          // This all pertains to the style of the button, alter values to your liking 
          var type:String = GradientType.LINEAR; 
          var colors:Array = [0xFFFFFF, 0xCCCCCC]; 
          var alphas:Array = [1, 1]; 
          var ratios:Array = [0, 255]; 
          var spreadMethod:String = SpreadMethod.PAD; 
          var interp:String = InterpolationMethod.LINEAR_RGB; 
          var focalPtRatio:Number = 0; 
          var matrix:Matrix = new Matrix(); 
          var boxWidth:Number = 16; 
          var boxHeight:Number = 20; 
          var boxRotation:Number = Math.PI/2; // 90˚ 
          var tx:Number = 0; 
          var ty:Number = 0; 
          matrix.createGradientBox(boxWidth, boxHeight, boxRotation, tx, ty); 
          var rect:Shape = new Shape; 
          rect.graphics.beginGradientFill(type, colors, alphas, ratios, matrix, spreadMethod, interp, focalPtRatio); 
          rect.graphics.lineStyle(1, 0x999999); 
          rect.graphics.drawRect(0, 0, boxWidth, boxHeight); 

          // This all pertains to the text fields that give our buttons their label, alter values to your liking 
          var myText:TextField = new TextField(); 
          //myText.embedFonts = true; 
          myText.autoSize = TextFieldAutoSize.LEFT; 
          myText.antiAliasType = AntiAliasType.ADVANCED; 
          myText.defaultTextFormat = format; 
          myText.selectable = false; 
          myText.mouseEnabled = false; 
          myText.text = pagenum; 
          myText.x = 2; 
          myText.y = 2; 
          addChild(myText); 

          // Create MovieClip holder for each button graphic and text label 
          var clip_mc = new MovieClip(); 
          // Add the rectangle graphic 
          clip_mc.addChild(rect); 
          // Add the text field 
          clip_mc.addChild(myText); 
          // Put the new movieClip on stage now 
          addChild(clip_mc); 
          // Make the mouse button mode true for the movieclip so user knows it is a button 
          clip_mc.buttonMode = true;      

          // Offset each one in the loop to make sure they don't just get put right on top of each other 
          xPlacement = xPlacement + distance; 
          // Now apply it in its offset Y position to the stage 
          clip_mc.y = yPlacement; 
          // X position it will be placed on stage 
          clip_mc.x = xPlacement; 

          var pictLdr:Loader = new Loader(); 
          var pictURL:String = pagepicture; 
          var pictURLReq:URLRequest = new URLRequest(pictURL); 
          pictLdr.load(pictURLReq); 
          // Access the pic value and ready it for setting up the Click listener, and function 
          clip_mc.clickToPic = pictLdr; 
          // Access the text value and ready it for setting up the Click listener, and function 
          clip_mc.clickToText = pagetext; 
          // Add the mouse event listener to the moviClip button 
          clip_mc.addEventListener (MouseEvent.CLICK, clipClick); 
          //Add event listeners (used for animating the buttons) 
          clip_mc.addEventListener (MouseEvent.MOUSE_OVER, onMouseOver); 
          clip_mc.addEventListener (MouseEvent.MOUSE_OUT, onMouseOut); 

          // Set the function for what happens when that button gets clicked 
          function clipClick(e:Event):void { 

           pagewindow.pageText_txt.text = e.target.clickToText; 


           pagewindow.pagepic.addChild(e.target.clickToPic); 

          } 

          // increment the loop index number for trace testing 
          i++; 
          //trace(i); 


    } 




} 

// Set the mouse over function for all movieclip buttons 
function onMouseOver (e:Event):void { 

// Create the filters and add them to an array 
var bevel:BevelFilter = new BevelFilter(); 
bevel.distance = 1; 
bevel.angle = 45; 
bevel.shadowColor = 0x666666; 
bevel.shadowAlpha = 0.5; 
bevel.strength = 4; 
bevel.quality = BitmapFilterQuality.MEDIUM; 
bevel.type = BitmapFilterType.INNER; 
bevel.knockout = false; 
var glow:GlowFilter = new GlowFilter(); 
glow.color = 0x79B3E1; 
glow.alpha = 1; 
glow.quality = BitmapFilterQuality.MEDIUM; 
// Pack the filter paramters into the array variable 
var filtersArray:Array = new Array(bevel, glow); 

// Assign the filters array to the display object to apply the filter 
e.target.filters = filtersArray; 

} 

// Set the mouse out function for all movieclip buttons 
function onMouseOut (e:Event):void { 

    // Remove the Filters on Mouse Out 
    e.target.filters = null; 

} 

} 

這該是多麼我創建XML文件使用PHP:

<?php 
session_start(); 
header ("content-type: text/xml"); 
/* 
connect to our database 
*/ 
/* 
we post the variables we recieve from flash 
*/ 
$myText2 = $_POST['myText2']; 
/* 
if flash called the login system the code below runs 
*/ 
$myText2 = $_POST['myText2']; 
if ($_POST['systemCallL'] == "checkLoginL") { 
// Initialize the xmlOutput variable 
$xmlBody = '<?xml version="1.0" encoding="iso-8859-1"?>'; 
$xmlBody .= "<XML>"; 
$xmlBody .= "<MenuList>"; 
// Run a select query to get my letest 6 items 
// Connect to the MySQL database 
include "connectxml.php"; 
//$sql = "SELECT DISTINCT product_name FROM `ctegories_and`"; 
$sql = "SELECT * FROM `restItems` WHERE restarantName='$myText2'"; 
$query = mysqli_query($db_conx, $sql); 
$productCount = mysqli_num_rows($query); // count the output amount 
if ($productCount > 0) { 
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
      $id = $row["id"]; 
      $product_name = $row["product_name"]; 
$xmlBody .= '<MenuItem> 
       <pagenum>'.$product_name.'</pagenum> 
     <pagepicture>http://babygifts4u.co.uk/austinApp/php/pageThumbs/page1thumb.jpg</pagepicture> 
     <pagetext> 
     Page 1 text is blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah 
     </pagetext> 
     </MenuItem>'; 
} 

}// End while loop 
$xmlBody .= "</MenuList>"; 
$xmlBody .= "</XML>"; 

} 
echo $xmlBody; 

?> 

,如果我從我的PHP代碼中刪除if ($_POST['systemCallL'] == "checkLoginL") { }的AS3代碼正確加載數據並創建按鈕!但我不想這樣做!

任何幫助,將不勝感激。

在此先感謝。

+0

我沒有看到'myLoader.load( )'在發佈之前,你是否已經從代碼中取出了它? – Daniel 2014-11-21 03:56:09

+0

@丹尼爾,不,我從來沒有在我的代碼中有過!當你提到它時,我確實把它放在我的代碼中,但是我得到以下錯誤:場景1,圖層'圖層1',圖框1,線84 \t 1136:參數的數量不正確。預計1. – user3806613 2014-11-21 04:02:03

+0

我不確定如何在沒有加載請求的情況下加載數據。也似乎'processLoginL'函數沒有正確關閉 – Daniel 2014-11-21 04:25:46

回答

1

這裏是POST爲例請求

var loader : URLLoader = new URLLoader(); 
var request : URLRequest = new URLRequest("/index.php"); 

request.method = URLRequestMethod.POST; 
var variables : URLVariables = new URLVariables(); 
variables.key1 = "value1"; 
variables.key2 = "value2"; 
request.data = variables; 

// Handlers 
loader.addEventListener(Event.COMPLETE, on_complete); 
loader.load(request); 
private function on_complete(e : Event):void{ 
     // process 
} 

源:http://scriptcult.com/subcategory_9/article_442-post-request-data-in-as.htm

注意,Loader.load方法需要請求傳遞