Actionscript .appendText在GeolocationEvent.UPDATE示例中写入最后一个文本,但未替换它
刚接触actionscript,正在查看GeolocationEvent.UPDATE示例,在.appendText和array.push中出现了一些意想不到的结果-我不知道它们是否都只是手机没有跟上更新 首先,文本问题是它正在覆盖而不是替换上一次写入,因此在手机上运行应用程序几分钟后,您将无法再读取数字-使用this.removeChild和addChild是为了让它在再次写入之前删除最后一次写入 其次,数组的问题是它在跟踪中输出的是随机的。长度数字-在再次计数之前,长度看起来偶尔会重置为2,并且计数到看似随机的数字。我知道我不想在最终版本中增加数组的开销,但我正试图从中了解它不起作用的原因 我已经注释掉了我尝试过的不同的东西-如果我错过了一些基本的东西,我很抱歉Actionscript .appendText在GeolocationEvent.UPDATE示例中写入最后一个文本,但未替换它,actionscript,array-push,appendtext,Actionscript,Array Push,Appendtext,刚接触actionscript,正在查看GeolocationEvent.UPDATE示例,在.appendText和array.push中出现了一些意想不到的结果-我不知道它们是否都只是手机没有跟上更新 首先,文本问题是它正在覆盖而不是替换上一次写入,因此在手机上运行应用程序几分钟后,您将无法再读取数字-使用this.removeChild和addChild是为了让它在再次写入之前删除最后一次写入 其次,数组的问题是它在跟踪中输出的是随机的。长度数字-在再次计数之前,长度看起来偶尔会重置为2,
var format:TextFormat = new TextFormat();
format.color = 0xff0066;
format.font = "Lucida Console";
format.size = 20;
var fl_GeolocationDisplay:TextField = new TextField();
fl_GeolocationDisplay.defaultTextFormat = format;
fl_GeolocationDisplay.x = 10;
fl_GeolocationDisplay.y = 20;
fl_GeolocationDisplay.selectable = false;
fl_GeolocationDisplay.autoSize = TextFieldAutoSize.LEFT;
//fl_GeolocationDisplay.text = "Geolocation is not responding. Verify the device's location settings.";
fl_GeolocationDisplay.text = " ";
addChild(fl_GeolocationDisplay);
var gpsArray:Array = [42.09646417];
if(!Geolocation.isSupported)
{
fl_GeolocationDisplay.text = "Geolocation is not supported on this device.";
}
else
{
var fl_Geolocation:Geolocation = new Geolocation();
fl_Geolocation.setRequestedUpdateInterval(60000); //android overrides setRequestedUpdateInterval()
fl_Geolocation.addEventListener(GeolocationEvent.UPDATE, fl_UpdateGeolocation);
fl_Geolocation.addEventListener(StatusEvent.STATUS, gpsStatusHandler);
}
function fl_UpdateGeolocation(event:GeolocationEvent):void
{
//gpsArray.push(event.latitude);
//gpsArray[gpsArray.length] = event.latitude;
gpsArray.unshift(event.latitude);
var speed:Number = event.speed * 2.23693629;
if (gpsArray[gpsArray.length - 2] != gpsArray[gpsArray.length - 1])
{
trace(gpsArray.length + "|" + gpsArray[gpsArray.length - 2] + "|" + gpsArray[gpsArray.length - 1]);
trace(gpsArray[1] + "|" + gpsArray[0]);
trace(gpsArray[gpsArray.length - 2] - gpsArray[gpsArray.length - 1]);
}
//this.removeChild(fl_GeolocationDisplay);
fl_GeolocationDisplay.parent.removeChild(fl_GeolocationDisplay);
//fl_GeolocationDisplay = null; //TypeError: Error #2007: Parameter child must be non-null.
addChild(fl_GeolocationDisplay);
fl_GeolocationDisplay.text = (event.latitude.toString() + " | " + event.timestamp.toString());
//fl_GeolocationDisplay.text = (event.latitude.toString() + "\n");
//fl_GeolocationDisplay.appendText(event.latitude.toString() + "\n");
//fl_GeolocationDisplay.appendText(event.longitude.toString() + "\n");
}
function gpsStatusHandler(event:StatusEvent):void {
if (fl_Geolocation.muted) {
fl_GeolocationDisplay.text = "Please verify the device's location settings.";
}
}
我真的不明白你想做什么,我的意思是你说了一件事,但你的代码似乎说了一些不同的东西 还有一个严重的问题是不同的代码段位于何处?它的顶部似乎位于构造函数内部。然后底部部分是它们自己的功能?如果是这种情况,请确保构造函数没有多次运行,这似乎是问题所在,并解释为什么项会被覆盖在彼此之上 另外,您的问题说明了一些关于appendText的内容,但似乎要替换textfield中的文本?AppendText将在该文本字段内添加额外文本 无论如何,我从您的代码中执行了一个实现,从更新事件中获取经度| latitude,然后将其附加到新行的textfield。也许这就是你想做的?我注释掉了gps阵列,因为我不知道您尝试通过这样做来实现什么:
package {
import flash.events.GeolocationEvent;
import flash.events.StatusEvent;
import flash.sensors.Geolocation;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class Foobar extends MovieClip {
var gpsArray:Array = [42.09646417];
var format:TextFormat = new TextFormat();
var fl_GeolocationDisplay:TextField = new TextField();
var fl_Geolocation:Geolocation = new Geolocation();
public function Foobar() {
format.color = 0xff0066;
format.font = "Lucida Console";
format.size = 20;
fl_GeolocationDisplay.defaultTextFormat = format;
fl_GeolocationDisplay.x = 10;
fl_GeolocationDisplay.y = 20;
fl_GeolocationDisplay.selectable = false;
fl_GeolocationDisplay.autoSize = TextFieldAutoSize.LEFT;
//fl_GeolocationDisplay.text = "Geolocation is not responding. Verify the device's location settings.";
fl_GeolocationDisplay.text = " ";
addChild(fl_GeolocationDisplay);
if(!Geolocation.isSupported) {
trace("unsupported");
fl_GeolocationDisplay.text = "Geolocation is not supported on this device.";
} else {
trace("supported");
fl_Geolocation.setRequestedUpdateInterval(500); //android overrides setRequestedUpdateInterval()
fl_Geolocation.addEventListener(GeolocationEvent.UPDATE, fl_UpdateGeolocation);
fl_Geolocation.addEventListener(StatusEvent.STATUS, gpsStatusHandler);
}
}
function fl_UpdateGeolocation(event:GeolocationEvent):void {
/*gpsArray.unshift(event.latitude);
var speed:Number = event.speed * 2.23693629;
if (gpsArray[gpsArray.length - 2] != gpsArray[gpsArray.length - 1]) {
trace(gpsArray.length + "|" + gpsArray[gpsArray.length - 2] + "|" + gpsArray[gpsArray.length - 1]);
trace(gpsArray[1] + "|" + gpsArray[0]);
trace(gpsArray[gpsArray.length - 2] - gpsArray[gpsArray.length - 1]);
}*/
fl_GeolocationDisplay.appendText(event.latitude.toString() + "|" + event.longitude.toString() + "\n");
}
function gpsStatusHandler(event:StatusEvent):void {
if (fl_Geolocation.muted) {
fl_GeolocationDisplay.text = "Please verify the device's location settings.";
}
}
}
}
你能在段落中重新格式化你的文字墙吗?因为这样很难理解和抓住你的问题。谢谢-希望我说得更清楚你完全正确-被多次调用,所以你也解决了数组问题。很抱歉,我是新来的。非常感谢