静态变量显示为空 [英] Static Variable showing as Null

查看:206
本文介绍了静态变量显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用的大地定位一个用户名为级定位。有一个在这个类的静态方法被称为定位()。此方法然后调用它设置一个被称为LOC静态变量给LngLat类的实例的另一种方法。

我遇到的问题是,当我打电话找到方法在现场第1帧,然后尝试访问禄变量它显示为空......但是当我跟踪禄变量后,它被设置定位器类它显示为一个对象。

当我这样做(在第1帧):

  Locator.locate();
跟踪(Locator.loc.getCity());
 

空被跟踪。

但是,当我有这个在第1帧:

  Locator.locate();
 

,然后跟踪在定位器类的静态方法结束的变量它显示为一个对象,这个城市被追踪的预期。

任何想法?

PS我做了解释,对做得不好,对不起。

在此先感谢。

解决方案
  

这答案是最好的猜测使用的的信息,你所提供的东西。

基于上下文被捕获的地点

,很可能是你正在与一些服务器端技术进行通信(即一个完整的坐标系数据库)。

要做到这一点,你就必须利用的URLLoader

在初始化的URLLoader 的实例,并试图从第三方收集信息,所要求的信息将在一个时间,这是的同步。这意味着函数等待时闪光接收你可以在不久的将来的任何地方称为后的数据被调用,但不能立即

下面是一个实验:

  VAR数据:对象= NULL;
VAR装载机:的URLLoader;

功能测试():无效
{
    装载机=新的URLLoader();
    VAR要求:的URLRequest =新的URLRequest(https://projectavian.com/files/example.php);

    loader.load(要求);
    loader.addEventListener(引发Event.COMPLETE,_done);
    stage.addEventListener(Event.ENTER_FRAME,_check);
}


功能_done(五:事件):无效
{
    数据= e.target.data;
    跟踪(数据);

    loader.removeEventListener(引发Event.COMPLETE,_done);
    stage.removeEventListener(Event.ENTER_FRAME,_check);
}

功能_check(五:事件):无效
{
    跟踪(数据);
}


测试();
跟踪(数据); //即使在上述所有工作这将始终为空。
 

如果你运行它,你会发现,数据总是最初

我的的猜测的是,你的 Locator.locate()需要一些时间来收集所需的信息,然后分配一个值的任何 Locator.loc.getCity()的回报。

I have a class named Locator that I use to GeoLocate a user. There's a static method in that class called "locate()". This method then calls another method which sets a static variable called "loc" to an instance of a LngLat class.

The issue I'm having is, when I call the locate method at Frame 1 on the scene, and then try to access the loc variable it shows as null... but when I trace the loc variable after it's set in the Locator class it shows as an object.

When I do this (at frame 1):

Locator.locate();
trace(Locator.loc.getCity());

null gets traced.

But when I have this in Frame 1:

Locator.locate();

and then trace the variable at the end of the static method in the Locator class it shows as a object and the city is traced as expected.

Any ideas?

P.S I did a bad job of explaining that, sorry.

Thanks in advance.

解决方案

This answer is a best-guess using what little information you've provided.

Based on the context being capturing a location, it's likely that you're communicating with some server-side technology (i.e. a database full of coordinates).

To achieve this, you would have to be making use of URLLoader.

When you initialize an instance of URLLoader and attempt to collect information from a third party, the requested information will be received at a time which is asynchronous. This means that a function waiting to be called when flash receives the data you're after could be called anywhere in the near future, but not immediately.

Here's an experiment:

var data:Object = null;
var loader:URLLoader;

function test():void
{
    loader = new URLLoader();
    var request:URLRequest = new URLRequest("https://projectavian.com/files/example.php");

    loader.load(request);
    loader.addEventListener(Event.COMPLETE, _done);
    stage.addEventListener(Event.ENTER_FRAME, _check);
}


function _done(e:Event):void
{
    data = e.target.data;
    trace(data);

    loader.removeEventListener(Event.COMPLETE, _done);
    stage.removeEventListener(Event.ENTER_FRAME, _check);
}

function _check(e:Event):void
{
    trace(data);
}


test();
trace(data); // This will always be null even after all the work above.

If you run this, you'll notice that data is always initially null.

My guess is that your Locator.locate() needs some time to gather the information it needs, then assigns a value to whatever Locator.loc.getCity() returns.

这篇关于静态变量显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆