0

I am kind of new to AS3 and programming itself.

I have this issue. I want to be able to use the Pl_array and En_array outside of AfterLoad function, but I always get undefined value. I am writing the code inside the timeline not .as file. Does it matter?

I was trying to return them from function but since it's related to listener i just dont know how to do it also i was trying to make them public.

Here is code on first frame:

import flash.events.MouseEvent;
stop();
Btn_start.addEventListener(MouseEvent.CLICK, onStartClick);

function onStartClick(me:MouseEvent):void{
    gotoAndStop("Dictionary");
}

and here is on the second called Dictionary:

import flash.events.Event;
import flash.utils.Timer;
import flash.events.MouseEvent;

stop();

var myTextLoader:URLLoader = new URLLoader();
var Txt_array:Array=new Array(); //tablica wczytanych zwrotów
var Pl_array:Array=new  Array();
var En_array:Array=new Array();
var myTimer:Timer = new Timer(1000);

myTextLoader.addEventListener(Event.COMPLETE, onLoaded); //listener na koniec wczytywania pliku tekstowego

function onLoaded(e:Event):void { //funkcja wywoływana przez listener na koniec wczytywania pliku
    Txt_array = e.target.data.split(/\n/); //
    dispatchEvent(new Event("Load_END"));
}
myTextLoader.load(new URLRequest("Zwroty.txt"));

this.addEventListener("Load_END", AfterLoad); //kod wykonywano po wczytaniu pliku tekstowego 
function AfterLoad(e:Event):void{   
    for each (var s:String in Txt_array){// pętla która rozdziela tekst na polski i angielski
        var i:int=0;
      En_array[i]=s.substr(0, s.indexOf("-")-1);
      Pl_array[i]=s.substr(s.indexOf("-")+2, s.length);
      i++;
    } //koniec fora
}//koniec funkcji

Begin.addEventListener(MouseEvent.CLICK, test);

function test(e:Event):void{
    trace(En_array[1]);
}

//funkcja wyświetlająca string w txt_load
function ShowString (txt_show:String):void{
    load_txt.text = txt_show;
}

function ShowOpinion(txt_opinion:String):void{
    opinion_txt.text=txt_opinion;
}   

function HideOpinion():void{
    opinion_txt.text=" ";
}

//funkcja porównująca łańcuchy
function Compare(txt_a:String,txt_b:String):Boolean{
    if (txt_a==txt_b){ 
      return true;
    } 
    return false;
}

//up_btn.useHandCursor=true;
//up_btn.addEventListener(MouseEvent.MOUSE_OVER, switch_bg);

//function switch_bg(me:MouseEvent):void{
    //var newColor:ColorTransform = me.target.transform.colorTransform;
    //newColor.color = 0x1000C6;
    //me.target.transform.colorTransform = newColor;
//}

on test function i always get undefined while tracing. I was trying to find solution in Google but couldn't.

1
  • It is good practice to use Event.EVENT_TYPE. Commented Apr 19, 2013 at 13:45

3 Answers 3

1

This code looks like it should work but if you're trying to access the first element in En_array you need to remember indexing starts at 0, not 1. You might also want to make sure En_array is not empty before reading any of its values. Try this:

if (En_array.length > 0)
    trace(En_array[0]);
Sign up to request clarification or add additional context in comments.

Comments

0

it sometimes take some seconds to load data first. After loading then read data from array. In timeline, loading must not be at the same frame with the reading process, unless otherwise, explicitly loaded in first line of code then followed by reading.

Comments

0

You have mess in your code. Lets make it simpler:

import flash.events.Event;
import flash.utils.Timer;
import flash.events.MouseEvent;

stop();

var myTextLoader:URLLoader = new URLLoader();
var Txt_array:Array;
var Pl_array:Array;
var En_array:Array;
/* Good practice is to create object when you relay need it */
var myTimer:Timer = new Timer(1000);

myTextLoader.addEventListener(Event.COMPLETE, onLoaded); 

function onLoaded(e:Event):void {
   Txt_array = e.target.data.split(/\n/); // now we create new array
   afterLoad(Txt_array);
}

function afterLoad(array):void{ // it is good habit to start function names from small letter
    En_array = [];// create arrays
    Pl_array = [];

    for each (var s:String in array){
      En_array.push(s.substr(0, s.indexOf("-")-1));
      Pl_array.push(s.substr(s.indexOf("-")+2, s.length));
      // push let you add items more efficient and you don't need index
    }
}

myTextLoader.load(new URLRequest("Zwroty.txt"));

Now everything should be fine :) Just remember to check if objects (in your case arrays) have elements you want to use (etc. array.lenght>0 or array=null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.