简体   繁体   中英

Accessing PHP variables using AS3 without using OO

Really stumped on this one.

I have the following PHP file with a variable I am trying to access to place in a dynamic text box on my flash stage.

PHP code:

$returnVars = array();

$returnVars['username'] = "test";

$returnString = http_build_query($returnVars);

//send variables back to Flash

echo $returnString;

AS3 code:

var request:URLRequest = new URLRequest("http://www.mysite.com/flash.php");
            request.method = URLRequestMethod.GET;

            var loader2:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader2.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader2.load(request);


            function completeHandler(event:Event) :void{

                var username = event.target.data.username;

            // dynamic text box called username
            username.text=event.target.data.username;

            }

The error:

Error #1009: Cannot access a property or method of a null object reference.

The code was adapted from a tutorial using a class. However I do not get on with classes so wont be using any.

Any ideas will be most welcome.

-Rob.

The code looks all wrong

First of, are you sure http_build_query is what you need?

Secondly, I'm not an AS veteran, but defining a variable named "username" and then setting it's property "text" in that way looks horribly bad.

Edit: In fact, my observation explains the error. When you define "username", it's obviously not an object. When you try setting the property "text", it won't work for this same reason.

I really think you need to learn the basics of object-oriented programming...

Edit2:

This is the relevant code from the tutorial:

var username = evt.target.data.username;
var email = evt.target.data.email;

trace ('username is ' + username);

trace ('email is ' + email);

As you see, there's no mention of username.text .

When looking at your code, I don't really see anything wrong with the way you are working in AS3 (don't know about Php, but that will be okay too i guess). It's just that you add an eventListener to loader, when you should be adding it to loader2.

And second of all, Why are you naming a variable just the same way as a textBox? that is asking for problems :)

        function completeHandler(event:Event) :void{

        //Change the variable name of this to something else
        var _someOtherVariableName = event.target.data.username;

        //dynamic text box called username
        username.text = event.target.data.username;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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