简体   繁体   中英

AS3 - READ text from external .txt and CHANGE color of an object IF there is MATCH

I am working on a project that uses a batch script to check availability (ping) of the computers in a specific Network. It also changes the color of an object in Flash-based UI when there's an exact match from the script's results.

EXAMPLE:

1) Script runs checks and outputs into results.txt:

192.168.0.1=UP

192.168.0.2=DOWN

192.168.0.3=UP

2) Using AS3, I would like to read this file and search for a string like "192.168.0.1=UP". If there is a match it would change the object's color to GREEN. Otherwise, it would set as RED.

So far, I have managed to import the output file from the script inside Flash, using:

var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
trace(e.target.data);
}
textLoader.load(new URLRequest("Path:/To/result.txt"));

I understand I cannot just make AS3 search the file only for "192.168.0.1=UP" and change color if there is match.

How I can make use of the imported text? Do I have to make a string, that uses only the parts of the text I need and then make an IF statement to color the object if there is match?

Thanks in advance.

UPDATE:

Thanks to Gunther Fox , I was able to put the whole code together:

import flash.geom.ColorTransform;

var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
    trace(e.target.data);
    if (e.target.data.indexOf("192.168.0.1=UP") != -1) {
        var colorGREEN:ColorTransform = <instance_name>.transform.colorTransform;
        colorGREEN.color = 0x00FF00;
        <instance_name>.transform.colorTransform = colorGREEN;
    }
    else {
        var colorRED:ColorTransform = <instance_name>.transform.colorTransform;
        colorRED.color = 0xFF0000;
        <instance_name>.transform.colorTransform = colorRED;
    }
}
textLoader.load(new URLRequest("Path:/To/result.txt"));

Now my AS3 changes color of a specific object, based on the batch script output.

UPDATE 2

I solved the problem with reloading of the text file:

var timer:Timer = new Timer(10000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

function onTimer(evt:TimerEvent):void {
var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(Event.COMPLETE, onLoaded);
textLoader.load(new URLRequest("C:/Folder/output/result.txt"));

}

If you're looking for something very specific like the string "192.168.0.1=UP" you can use indexOf() and check if the result is -1 or not:

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

    if (e.target.data.indexOf("192.168.0.1=UP") != -1) {
        // Change to green
    }
    else {
        // Change to red
    }
}

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