简体   繁体   English

使用AS3访问PHP变量而不使用OO

[英]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文件,其中包含一个变量,我试图访问该变量以将其放置在Flash舞台上的动态文本框中。

PHP code: PHP代码:

$returnVars = array();

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

$returnString = http_build_query($returnVars);

//send variables back to Flash

echo $returnString;

AS3 code: AS3代码:

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. 错误#1009:无法访问空对象引用的属性或方法。

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? 首先,确定要使用http_build_query吗?

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: 编辑2:

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 . 如您所见,这里没有提到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). 在查看您的代码时,我真的没有发现您在AS3中的工作方式有什么问题(不了解Php,但我想也可以)。 It's just that you add an eventListener to loader, when you should be adding it to loader2. 只是将eventListener添加到loader时,应该将其添加到loader2中。

And second of all, Why are you naming a variable just the same way as a textBox? 其次,为什么命名变量的方式与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;

        }

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

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