简体   繁体   English

将变量从Flash传递到PHP

[英]Passing Variables from Flash to PHP

Creating a flash project where users can visit the site, and turn off/on objects in a house (ie. lights, tv, computer, etc.) The next user who will visit the house in the website, will see what lights or house appliances were left on. 创建一个Flash项目,用户可以访问该站点,并关闭/打开房屋中的对象(例如,灯,电视,计算机等)。下一个将在网站上访问房屋的用户将看到什么灯或房屋电器被留下。 Flash variables are passed to PHP, and those variables are saved in an XML file. 将Flash变量传递给PHP,并将这些变量保存在XML文件中。 (For testing to see what is being saved to the XML file, on each click --vars.xml opens.) In the vars.xml file, I see that the house objects that were last turned on--are saved in the XML file--BUT in the SWF file, ONLY one of the objects that are listed in the XML are turned ON. (为进行测试以查看要保存到XML文件的内容,每次单击时都会打开--vars.xml。) 在vars.xml文件中,我看到上次打开的内部对象已保存在XML中文件-但在SWF文件中,仅将XML中列出的对象之一打开。 Only the last object that was clicked on would show ON--not all the objects in the XML file.) 只有最后一个被单击的对象才会显示为ON,而不是XML文件中的所有对象。)

package {
  import flash.display.MovieClip;
  import flash.events.MouseEvent;
  import flash.events.Event;
  import flash.text.*;
  import flash.net.*;

  public class House extends MovieClip {

    var request:URLRequest = new URLRequest("vars.xml")
    var loader:URLLoader = new URLLoader();


    // Constructor--------------------------------------------------------------------
    public function House()


    {

        loader.addEventListener(Event.COMPLETE, parseXML);
        loader.load(request); 

}

// function sendPhp ------------------------------------------------------------------
    function sendPhp():void
    {

        // send vars to php
        var request:URLRequest = new URLRequest("write_xml.php"); // the php file to send data to
        var variables:URLVariables = new URLVariables(); // create an array of POST vars


        for (var i:int=0; i<onList.length; i++) {
            variables["v"+i] = onList[i];
        }

        //variables['powerUsage'] = totalTxt.text;

        request.data = variables; // send the vars to the data property of the requested url (our php file)
        request.method = URLRequestMethod.POST; // use POST as the send method
        try
        {
            var sender:URLLoader = new URLLoader();
            sender.load(request); // load the php file and send it the variable data
            navigateToURL(new URLRequest("vars.xml"), '_blank'); //show me the xml
        } 
        catch (e:Error) 
        {
            trace(e); // trace error if there is a problem
        }
    }


// function parseXML ------------------------------------------------------------------

function parseXML(evt:Event)
{
    var xdata:XML = new XML(loader.data); // using E4x

    //xdata.child(0);

    for (var j:int=0; j<xdata.length(); j++) {
        onList[j] = xdata.child(j);

        for (var k:int=0; k<HouseObjects.length;k++) {
            //root[onList[j]].gotoAndStop(3);
            if (onList[j] == HouseObjects[k].name) {
                HouseObjects[k].gotoAndStop(3);
                //trace("tracing house objects"+ HouseObjects[k]);
                trace("onList[j]: " + onList[j]);
                trace("Array onList: " + onList);

            }

        }

    }

}



 } //end of class

} // end of package

You actually don't need to put all this code here, and it's true that you should make your app more secure , especially after showing all this info! 实际上,您不需要将所有这些代码都放在这里,并且确实应该使您的应用程序更安全,尤其是在显示所有这些信息之后! If your XML is not the problem , check the Actionscript part, particularly the parseXML() function. 如果XML不是问题,请检查Actionscript部分,尤其是parseXML()函数。

Are you able to trace the names of the components that are switched on? 您是否可以跟踪已打开的组件的名称? If yes , concentrate on what's happening in your loop. 如果是,请专注于循环中发生的事情。 If your xml is fine, the problem is not passing data from PHP to Flash. 如果您的xml没问题,则问题不在于将数据从PHP传递到Flash。

I like the tree house! 我喜欢树屋! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM